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
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
// Rust language amplification derive library providing multiple generic trait
// implementations, type wrappers, derive macros and other language enhancements
//
// Written in 2019-2021 by
//     Dr. Maxim Orlovsky <orlovsky@pandoracore.com>
//
// To the extent possible under law, the author(s) have dedicated all
// copyright and related and neighboring rights to this software to
// the public domain worldwide. This software is distributed without
// any warranty.
//
// You should have received a copy of the MIT License
// along with this software.
// If not, see <https://opensource.org/licenses/MIT>.

use std::hash::Hash;
use std::fmt::{Debug};
use syn::{Type, Lit};

use crate::{Error, ArgValue};

/// Constrains for attribute value type
#[derive(Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub enum ValueClass {
    /// The value must be a literal matching given literal constraints (see
    /// [`ConstrainedLit`])
    Literal(LiteralClass),

    /// The value must be of a native rust type matching given type constraints
    /// (see [`ConstrainedType`])
    Type(TypeClass),
}

impl From<Lit> for ValueClass {
    fn from(lit: Lit) -> Self {
        ValueClass::Literal(LiteralClass::from(lit))
    }
}

impl From<&Lit> for ValueClass {
    fn from(lit: &Lit) -> Self {
        ValueClass::Literal(LiteralClass::from(lit))
    }
}

impl From<Type> for ValueClass {
    fn from(ty: Type) -> Self {
        ValueClass::Type(TypeClass::from(ty))
    }
}

impl From<&Type> for ValueClass {
    fn from(ty: &Type) -> Self {
        ValueClass::Type(TypeClass::from(ty))
    }
}

impl ValueClass {
    /// Convenience constructor creating
    /// `ValueClass::Literal(LiteralClass::Str)`
    pub fn str() -> ValueClass {
        ValueClass::Literal(LiteralClass::Str)
    }

    /// Convenience constructor creating
    /// `ValueClass::Literal(LiteralClass::ByteStr)`
    pub fn byte_str() -> ValueClass {
        ValueClass::Literal(LiteralClass::ByteStr)
    }

    /// Convenience constructor creating
    /// `ValueClass::Literal(LiteralClass::Byte)`
    pub fn byte() -> ValueClass {
        ValueClass::Literal(LiteralClass::Byte)
    }

    /// Convenience constructor creating
    /// `ValueClass::Literal(LiteralClass::Int)`
    pub fn int() -> ValueClass {
        ValueClass::Literal(LiteralClass::Int)
    }

    /// Convenience constructor creating
    /// `ValueClass::Literal(LiteralClass::Float)`
    pub fn float() -> ValueClass {
        ValueClass::Literal(LiteralClass::Float)
    }

    /// Convenience constructor creating
    /// `ValueClass::Literal(LiteralClass::Char)`
    pub fn char() -> ValueClass {
        ValueClass::Literal(LiteralClass::Char)
    }

    /// Convenience constructor creating
    /// `ValueClass::Literal(LiteralClass::Bool)`
    pub fn bool() -> ValueClass {
        ValueClass::Literal(LiteralClass::Bool)
    }
}

impl ValueClass {
    /// Checks the value against value class requirements, generating [`Error`]
    /// if the requirements are not met.
    pub fn check(
        self,
        value: &ArgValue,
        attr: impl ToString,
        arg: impl ToString,
    ) -> Result<(), Error> {
        match (self, value) {
            (ValueClass::Literal(lit), ArgValue::Literal(ref value)) => lit.check(value, attr, arg),
            (ValueClass::Type(ty), ArgValue::Type(ref value)) => ty.check(value, attr, arg),
            _ => Err(Error::ArgValueTypeMismatch {
                attr: attr.to_string(),
                arg: arg.to_string(),
            }),
        }
    }
}

/// Constrains for literal value type
#[derive(Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub enum LiteralClass {
    /// Literal must be a string
    Str,

    /// Literal must be a byte string
    ByteStr,

    /// Literal must be a byte (in form of `b'f'`)
    Byte,

    /// Literal must be a character
    Char,

    /// Literal must be an integer
    Int,

    /// Literal must be a float
    Float,

    /// Literal must be a boolean
    Bool,

    /// Literal must be a verbatim form
    Verbatim,
}

impl From<Lit> for LiteralClass {
    #[inline]
    fn from(lit: Lit) -> Self {
        LiteralClass::from(&lit)
    }
}

impl From<&Lit> for LiteralClass {
    fn from(lit: &Lit) -> Self {
        match lit {
            Lit::Str(_) => LiteralClass::Str,
            Lit::ByteStr(_) => LiteralClass::ByteStr,
            Lit::Byte(_) => LiteralClass::Byte,
            Lit::Char(_) => LiteralClass::Char,
            Lit::Int(_) => LiteralClass::Int,
            Lit::Float(_) => LiteralClass::Float,
            Lit::Bool(_) => LiteralClass::Bool,
            Lit::Verbatim(_) => LiteralClass::Verbatim,
        }
    }
}

impl LiteralClass {
    /// Checks the literal against current requirements, generating [`Error`] if
    /// the requirements are not met.
    pub fn check(self, lit: &Lit, attr: impl ToString, arg: impl ToString) -> Result<(), Error> {
        if self != LiteralClass::from(lit) {
            Err(Error::ArgValueTypeMismatch {
                attr: attr.to_string(),
                arg: arg.to_string(),
            })
        } else {
            Ok(())
        }
    }
}

/// Constrains for the possible types that a Rust value could have.
#[derive(Clone, Copy, Ord, PartialOrd, Eq, PartialEq, Hash, Debug)]
pub enum TypeClass {
    /// A fixed size array type: `[T; n]`.
    Array,

    /// A bare function type: `fn(usize) -> bool`.
    BareFn,

    /// A type contained within invisible delimiters.
    Group,

    /// An `impl Bound1 + Bound2 + Bound3` type where `Bound` is a trait or
    /// a lifetime.
    ImplTrait,

    /// Indication that a type should be inferred by the compiler: `_`.
    Infer,

    /// A macro in the type position.
    Macro,

    /// The never type: `!`.
    Never,

    /// A parenthesized type equivalent to the inner type.
    Paren,

    /// A path like `std::slice::Iter`, optionally qualified with a
    /// self-type as in `<Vec<T> as SomeTrait>::Associated`.
    Path,

    /// A raw pointer type: `*const T` or `*mut T`.
    Ptr,

    /// A reference type: `&'a T` or `&'a mut T`.
    Reference,

    /// A dynamically sized slice type: `[T]`.
    Slice,

    /// A trait object type `Bound1 + Bound2 + Bound3` where `Bound` is a
    /// trait or a lifetime.
    TraitObject,

    /// A tuple type: `(A, B, C, String)`.
    Tuple,

    /// Tokens in type position not interpreted by Syn.
    Verbatim,
}

impl From<Type> for TypeClass {
    #[inline]
    fn from(ty: Type) -> Self {
        TypeClass::from(&ty)
    }
}

impl From<&Type> for TypeClass {
    fn from(ty: &Type) -> Self {
        match ty {
            Type::Array(_) => TypeClass::Array,
            Type::BareFn(_) => TypeClass::BareFn,
            Type::Group(_) => TypeClass::Group,
            Type::ImplTrait(_) => TypeClass::ImplTrait,
            Type::Infer(_) => TypeClass::Infer,
            Type::Macro(_) => TypeClass::Macro,
            Type::Never(_) => TypeClass::Never,
            Type::Paren(_) => TypeClass::Paren,
            Type::Path(_) => TypeClass::Path,
            Type::Ptr(_) => TypeClass::Ptr,
            Type::Reference(_) => TypeClass::Reference,
            Type::Slice(_) => TypeClass::Slice,
            Type::TraitObject(_) => TypeClass::TraitObject,
            Type::Tuple(_) => TypeClass::Tuple,
            Type::Verbatim(_) => TypeClass::Verbatim,
            _ => unreachable!(),
        }
    }
}

impl TypeClass {
    /// Checks the [`Type`] against current requirements, generating [`Error`]
    /// if the requirements are not met.
    pub fn check(self, ty: &Type, attr: impl ToString, arg: impl ToString) -> Result<(), Error> {
        if self != TypeClass::from(ty) {
            Err(Error::ArgValueTypeMismatch {
                attr: attr.to_string(),
                arg: arg.to_string(),
            })
        } else {
            Ok(())
        }
    }
}