1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
use std::string::String as StdString;

use nvim_types::{Array, Dictionary, Object, String as NvimString};

use crate::Result;

/// Trait implemented for types that can be converted into [`Object`s](Object).
pub trait ToObject {
    fn to_obj(self) -> Result<Object>;
}

impl ToObject for Object {
    #[inline]
    fn to_obj(self) -> Result<Object> {
        Ok(self)
    }
}

/// Implements `ToObject` for `Into<Object>` types.
macro_rules! impl_into {
    ($type:ty) => {
        impl ToObject for $type {
            fn to_obj(self) -> Result<Object> {
                Ok(self.into())
            }
        }
    };
}

impl_into!(());
impl_into!(bool);
impl_into!(i8);
impl_into!(u8);
impl_into!(i16);
impl_into!(u16);
impl_into!(i32);
impl_into!(u32);
impl_into!(i64);
impl_into!(f64);
impl_into!(StdString);
impl_into!(NvimString);
impl_into!(Array);
impl_into!(Dictionary);

/// Implements `ToObject` for "big integer" types.
macro_rules! impl_bigint {
    ($type:ty) => {
        impl ToObject for $type {
            fn to_obj(self) -> Result<Object> {
                Ok(i64::try_from(self)?.into())
            }
        }
    };
}

impl_bigint!(u64);
impl_bigint!(i128);
impl_bigint!(u128);
impl_bigint!(isize);
impl_bigint!(usize);

impl ToObject for &str {
    fn to_obj(self) -> Result<Object> {
        Ok(NvimString::from(self).into())
    }
}

impl ToObject for std::borrow::Cow<'_, str> {
    fn to_obj(self) -> Result<Object> {
        Ok(NvimString::from(self).into())
    }
}

impl<T> ToObject for Option<T>
where
    T: ToObject,
{
    fn to_obj(self) -> Result<Object> {
        self.map(ToObject::to_obj).transpose().map(Option::unwrap_or_default)
    }
}

impl<T> ToObject for Vec<T>
where
    T: ToObject,
{
    fn to_obj(self) -> Result<Object> {
        Ok(self
            .into_iter()
            .map(ToObject::to_obj)
            .collect::<Result<Array>>()?
            .into())
    }
}

// impl<K, V, I> ToObject for I
// where
//     K: Into<NvimString>,
//     V: ToObject,
//     I: IntoIterator<Item = (NvimString, V)>,
// {
//     fn to_obj(iter: I) -> Object {
//         todo!()
//     }
// }

impl<K, V> ToObject for std::collections::HashMap<K, V>
where
    K: Into<NvimString>,
    V: ToObject,
{
    fn to_obj(self) -> Result<Object> {
        self.into_iter()
            .map(|(k, v)| Ok((k, v.to_obj()?)))
            .collect::<Result<Dictionary>>()
            .map(Into::into)
    }
}