procmeta_core/
converter.rs1use syn::{LitBool, LitFloat, LitInt, LitStr, Result};
2
3pub trait Converter<T>: syn::parse::Parse {
4 fn convert(stream: &syn::parse::ParseStream) -> Result<T> {
5 let origin: Self = stream.parse()?;
6 Self::into(origin)
7 }
8 fn into(self) -> Result<T>;
9}
10
11impl<T, C: Converter<T>> Converter<Option<T>> for C {
12 fn into(self) -> Result<Option<T>> {
13 let result = C::into(self);
14 result.map(|v| Some(v))
15 }
16}
17
18impl Converter<String> for LitStr {
19 fn into(self) -> Result<String> {
20 Ok(self.value())
21 }
22}
23
24impl Converter<bool> for LitBool {
25 fn into(self) -> Result<bool> {
26 Ok(self.value)
27 }
28}
29
30impl Converter<f32> for LitFloat {
31 fn into(self) -> Result<f32> {
32 self.base10_parse()
33 }
34}
35
36impl Converter<f64> for LitFloat {
37 fn into(self) -> Result<f64> {
38 self.base10_parse()
39 }
40}
41
42impl Converter<i8> for LitInt {
43 fn into(self) -> Result<i8> {
44 self.base10_parse()
45 }
46}
47
48impl Converter<i16> for LitInt {
49 fn into(self) -> Result<i16> {
50 self.base10_parse()
51 }
52}
53
54impl Converter<i32> for LitInt {
55 fn into(self) -> Result<i32> {
56 self.base10_parse()
57 }
58}
59
60impl Converter<i64> for LitInt {
61 fn into(self) -> Result<i64> {
62 self.base10_parse()
63 }
64}
65
66impl Converter<usize> for LitInt {
67 fn into(self) -> Result<usize> {
68 self.base10_parse()
69 }
70}