1use color_eyre::eyre::Context;
4use nvim_oxi::Dictionary;
5use nvim_oxi::Object;
6use nvim_oxi::ObjectKind;
7
8pub trait OxiExtract {
10 type Out;
11
12 fn extract_from_dict(key: &str, value: &Object, dict: &Dictionary) -> color_eyre::Result<Self::Out>;
17}
18
19impl OxiExtract for nvim_oxi::String {
21 type Out = String;
22
23 fn extract_from_dict(key: &str, value: &Object, dict: &Dictionary) -> color_eyre::Result<Self::Out> {
25 let out = Self::try_from(value.clone())
26 .with_context(|| unexpected_kind_error_msg(value, key, dict, ObjectKind::String))?;
27 Ok(out.to_string())
28 }
29}
30
31impl OxiExtract for nvim_oxi::Integer {
33 type Out = Self;
34
35 fn extract_from_dict(key: &str, value: &Object, dict: &Dictionary) -> color_eyre::Result<Self::Out> {
37 let out = Self::try_from(value.clone())
38 .with_context(|| unexpected_kind_error_msg(value, key, dict, ObjectKind::Integer))?;
39 Ok(out)
40 }
41}
42
43pub fn unexpected_kind_error_msg(obj: &Object, key: &str, dict: &Dictionary, expected_kind: ObjectKind) -> String {
45 format!(
46 "value {obj:#?} of key {key:?} in dict {dict:#?} is {0:#?} but {expected_kind:?} was expected",
47 obj.kind()
48 )
49}