sxyazi/yazi · error

not a string

Error message

not a string

What it means

Type-conversion guard in `TryFrom<&Data> for &str`: the `Data` value being converted is not the `String` variant, so it cannot be borrowed as a string slice. The faulting input is the Data value (e.g. a Boolean, Number, or Table passed where text was required).

Solutions

  1. Ensure the producer stores a `Data::String` (convert with `.to_string()` at the source)
  2. Match on the Data variant yourself and handle non-string cases explicitly
  3. Use a helper like `as_str()`/try_into with a fallback value

Example fix

// before
let s: &str = field.try_into()?;
// after
let s: &str = field.try_into().map_err(|_| anyhow::anyhow!("expected string field, got {field:?}"))?;
Defensive patterns

Strategy: type-guard

Validate before calling

fn as_str(d: &Data) -> Option<&str> {
    match d { Data::String(s) => Some(s), _ => None }
}

Type guard

fn is_string(d: &Data) -> bool { matches!(d, Data::String(_)) }

Try / catch

let s = <&str>::try_from(&data).map_err(|_| anyhow!("expected string, got {data:?}"))?;

Prevention

When it happens

Trigger: Converting a Data value to `&str` when it holds a Boolean, a number, or a container variant instead of a String — commonly after `data.get("field")` where the field is not a string.

Common situations: Reading dynamic action/plugin payload fields assuming they are strings while callers passed numbers or booleans; Lua numbers (e.g. ids) that were not converted to strings.

Related errors


AI-assisted analysis of sxyazi/yazi@5f901b886b (2026-09-02). Data as JSON: /api/errors/853fe6923c884202. Report an issue: GitHub.

Appendix: source

Thrown at yazi-shared/src/data/data.rs:113

	type Error = anyhow::Error;

	fn try_from(value: &Data) -> Result<Self, Self::Error> {
		match value {
			Data::Boolean(b) => Ok(*b),
			Data::String(s) if s == "no" => Ok(false),
			Data::String(s) if s == "yes" => Ok(true),
			_ => bail!("not a boolean"),
		}
	}
}

impl<'a> TryFrom<&'a Data> for &'a str {
	type Error = anyhow::Error;

	fn try_from(value: &'a Data) -> Result<Self, Self::Error> {
		match value {
			Data::String(s) => Ok(s),
			_ => bail!("not a string"),
		}
	}
}

impl<'a> TryFrom<Data> for Cow<'a, str> {
	type Error = anyhow::Error;

	fn try_from(value: Data) -> Result<Self, Self::Error> {
		match value {
			Data::String(Cow::Borrowed(s)) => Ok(Cow::Borrowed(s)),
			Data::String(Cow::Owned(s)) => Ok(Cow::Owned(s)),
			_ => Err(anyhow!("not a string")),
		}
	}
}

impl<'a> TryFrom<&'a Data> for Cow<'a, str> {
	type Error = anyhow::Error;

View on GitHub (pinned to 5f901b886b)