{"record":{"id":"f6bc8595ade09392","repo":"sxyazi/yazi","slug":"not-a-string-f6bc85","errorCode":null,"errorMessage":"not a string","messagePattern":"not a string","errorType":"validation","errorClass":null,"httpStatus":null,"severity":"error","filePath":"yazi-shared/src/data/key.rs","lineNumber":81,"sourceCode":"}\n\nimpl From<usize> for DataKey {\n\tfn from(value: usize) -> Self { Self::Integer(value as i64) }\n}\n\nimpl From<&'static str> for DataKey {\n\tfn from(value: &'static str) -> Self { Self::String(Cow::Borrowed(value)) }\n}\n\nimpl From<String> for DataKey {\n\tfn from(value: String) -> Self { Self::String(Cow::Owned(value)) }\n}\n\nimpl TryFrom<DataKey> for String {\n\ttype Error = anyhow::Error;\n\n\tfn try_from(value: DataKey) -> Result<Self, Self::Error> {\n\t\tlet DataKey::String(s) = value else { bail!(\"not a string\") };\n\t\tOk(s.into_owned())\n\t}\n}\n\nimpl_into_integer!(DataKey, i8, i16, i32, i64, isize, u8, u16, u32, u64, usize, crate::id::Id);\nimpl_into_number!(DataKey, f32, f64);\n","sourceCodeStart":63,"sourceCodeEnd":88,"githubUrl":"https://github.com/sxyazi/yazi/blob/8ebf930f1796ae2076b7d6b0c5e93a1002c18134/yazi-shared/src/data/key.rs#L63-L88","documentation":"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.","triggerScenarios":"`String::try_from(data_key)` (or `?`/`.unwrap()` on such a conversion) where the DataKey holds a numeric or other non-string value.","commonSituations":"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.","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)."],"exampleFix":"// before\nlet s = String::try_from(key)?;\n// after\nlet s = match key {\n    DataKey::String(s) => s.into_owned(),\n    DataKey::Integer(i) => i.to_string(),\n    other => bail!(\"unexpected key: {other:?}\"),\n};","handlingStrategy":"type-guard","validationCode":"if !matches!(key, DataKey::String(_)) { bail!(\"expected string key\"); }","typeGuard":"fn as_str_key(key: &DataKey) -> Option<&str> { match key { DataKey::String(s) => Some(s), _ => None } }","tryCatchPattern":"// use match instead of TryFrom to avoid the panic-prone conversion\nlet s = as_str_key(&key).ok_or_else(|| anyhow!(\"expected string key\"))?;","preventionTips":["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."],"tags":["data","type-mismatch","rust","conversion"],"backgroundTag":"type-mismatch","analyzedSha":"8ebf930f1796ae2076b7d6b0c5e93a1002c18134","analyzedAt":"2026-09-09T22:26:16.379Z","contentChangedAt":"2026-09-09T22:26:16.379Z","schemaVersion":2},"datasetVersion":"2026-09-14T05:17:10.506Z"}