sxyazi/yazi · error
not a string
Error message
not a string
What it means
The TryFrom<DataKey> for String conversion only succeeds when the DataKey is the String variant; any other key variant (integer, number, etc.) bails with "not a string". It signals a caller assumption that a dynamic data value is textual when it is not.
Source
Thrown at yazi-shared/src/data/key.rs:81
}
impl From<usize> for DataKey {
fn from(value: usize) -> Self { Self::Integer(value as i64) }
}
impl From<&'static str> for DataKey {
fn from(value: &'static str) -> Self { Self::String(Cow::Borrowed(value)) }
}
impl From<String> for DataKey {
fn from(value: String) -> Self { Self::String(Cow::Owned(value)) }
}
impl TryFrom<DataKey> for String {
type Error = anyhow::Error;
fn try_from(value: DataKey) -> Result<Self, Self::Error> {
let DataKey::String(s) = value else { bail!("not a string") };
Ok(s.into_owned())
}
}
impl_into_integer!(DataKey, i8, i16, i32, i64, isize, u8, u16, u32, u64, usize, crate::id::Id);
impl_into_number!(DataKey, f32, f64);
View on GitHub (pinned to 8ebf930f17)
Solutions
- Match on the DataKey and handle non-string variants explicitly instead of force-converting.
- Convert with the appropriate TryFrom impl (e.g. integer impls) for the actual variant.
- At the producer side, ensure the value is emitted as DataKey::String (e.g. tostring in Lua before passing across).
Example fix
// before
let s = String::try_from(key)?;
// after
let s = match key {
DataKey::String(s) => s.into_owned(),
DataKey::Integer(i) => i.to_string(),
other => bail!("unexpected key: {other:?}"),
}; Defensive patterns
Strategy: type-guard
Validate before calling
if !matches!(key, DataKey::String(_)) { bail!("expected string key"); } Type guard
fn as_str_key(key: &DataKey) -> Option<&str> { match key { DataKey::String(s) => Some(s), _ => None } } Try / catch
// use match instead of TryFrom to avoid the panic-prone conversion
let s = as_str_key(&key).ok_or_else(|| anyhow!("expected string key"))?; Prevention
- Prefer matching on DataKey variants over blanket TryFrom conversions.
- Coerce values to strings at the producer boundary (e.g. Lua tostring).
- Document expected value types for dynamic data maps.
When it happens
Trigger: `String::try_from(data_key)` (or `?`/`.unwrap()` on such a conversion) where the DataKey holds a numeric or other non-string value.
Common situations: Reading dynamic data (e.g. from Lua-bound data or fetch results) and assuming a field is a string when it was produced as a number; indexing data maps with mixed-type keys.
Understand the failure class
Background: Type mismatch errors: IllegalArgumentException, TypeError and type guards across 150 open-source libraries — this error's family across 150 libraries.
Related errors
AI-assisted analysis of sxyazi/yazi@8ebf930f17 (2026-09-09).
Data as JSON: /api/errors/f6bc8595ade09392.
Report an issue: GitHub.