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
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
use std::error::Error as StdError;
use std::iter::FusedIterator;

use luajit::{Poppable, Pushable};
use types::{Array, Function, LuaRef, Object};
#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
use types::{HlGroupId, Integer};

use crate::IntoResult;

/// A super trait of most common traits implemented on iterators.
pub trait SuperIterator<I>:
    Iterator<Item = I> + ExactSizeIterator + DoubleEndedIterator + FusedIterator
{
}

impl<I, T> SuperIterator<I> for T where
    T: Iterator<Item = I>
        + ExactSizeIterator
        + DoubleEndedIterator
        + FusedIterator
{
}

macro_rules! impl_into {
    ($trait:ident, $type:ty) => {
        impl $trait for $type {
            fn to_object(self) -> Object {
                self.into()
            }
        }
    };
}

/// A trait implemented by strings and integers.
pub trait StringOrInt {
    fn to_object(self) -> Object;
}

impl_into!(StringOrInt, &str);
impl_into!(StringOrInt, String);
impl_into!(StringOrInt, i8);
impl_into!(StringOrInt, u8);
impl_into!(StringOrInt, i16);
impl_into!(StringOrInt, u16);
impl_into!(StringOrInt, i32);
impl_into!(StringOrInt, u32);
impl_into!(StringOrInt, i64);

/// A trait implemented by strings and list of strings.
pub trait StringOrListOfStrings {
    fn to_object(self) -> Object;
}

impl_into!(StringOrListOfStrings, &str);
impl_into!(StringOrListOfStrings, String);

// Here I'd like to use `IntoIterator` instead of `Vec`, but without
// specilization that'd cause conflicting impls.
impl<S: Into<String>> StringOrListOfStrings for Vec<S> {
    #[inline]
    fn to_object(self) -> Object {
        Array::from_iter(self.into_iter().map(Into::into)).into()
    }
}

/// A trait implemented by closures and [`Function`]s.
pub trait ToFunction<A, R> {
    fn into_luaref(self) -> LuaRef;
}

impl<A, R, F, O> ToFunction<A, R> for F
where
    A: Poppable,
    R: Pushable,
    F: FnMut(A) -> O + 'static,
    O: IntoResult<R>,
    O::Error: StdError + 'static,
{
    #[inline]
    fn into_luaref(self) -> LuaRef {
        Function::from_fn_mut(self).lua_ref()
    }
}

impl<A, R> ToFunction<A, R> for Function<A, R> {
    #[inline]
    fn into_luaref(self) -> LuaRef {
        self.lua_ref()
    }
}

/// A trait implemented by closures, [`Function`]s and strings.
pub trait StringOrFunction<A, R> {
    fn to_object(self) -> Object;
}

impl<A, R> StringOrFunction<A, R> for &str {
    #[inline]
    fn to_object(self) -> Object {
        self.into()
    }
}

impl<A, R> StringOrFunction<A, R> for String {
    #[inline]
    fn to_object(self) -> Object {
        self.into()
    }
}

impl<F, A, R, O> StringOrFunction<A, R> for F
where
    F: FnMut(A) -> O + 'static,
    A: Poppable,
    R: Pushable,
    O: IntoResult<R>,
    O::Error: StdError + 'static,
{
    #[inline]
    fn to_object(self) -> Object {
        Function::from_fn_mut(self).into()
    }
}

impl<A, R> StringOrFunction<A, R> for Function<A, R> {
    #[inline]
    fn to_object(self) -> Object {
        self.into()
    }
}

/// A trait implemented by types that can be converted to a highlight group ID.
#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
#[cfg_attr(
    docsrs,
    doc(cfg(any(feature = "neovim-0-10", feature = "neovim-nightly")))
)]
pub trait HlGroup: sealed::Sealed {
    type Error;

    fn to_hl_id(&self) -> Result<HlGroupId, Self::Error>;
}

#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
impl HlGroup for Integer {
    type Error = core::convert::Infallible;

    #[inline(always)]
    fn to_hl_id(&self) -> Result<HlGroupId, Self::Error> {
        Ok(*self)
    }
}

#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
impl HlGroup for &str {
    type Error = crate::Error;

    #[inline]
    fn to_hl_id(&self) -> Result<HlGroupId, Self::Error> {
        let obj = types::String::from(*self).into();
        let mut err = types::Error::default();
        let hl_id = unsafe {
            crate::ffi::helpers::object_to_hl_id(
                obj,
                b"hl_group\0".as_ptr() as *const _,
                &mut err,
            )
        };
        if err.is_err() {
            Err(err.into())
        } else {
            Ok(hl_id)
        }
    }
}

#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
mod sealed {
    pub trait Sealed {}

    impl Sealed for types::Integer {}

    impl Sealed for &str {}
}