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
use syn::{self, Path};

/// Bindings to be used by the generated code.
#[derive(Debug, Clone, Copy, Default)]
pub struct Bindings {
    /// Whether the generated code should comply with `#![no_std]`.
    pub no_std: bool,
}

impl Bindings {
    /// String type.
    pub fn string_ty(&self) -> Path {
        syn::parse_str(if self.no_std {
            "::alloc::string::String"
        } else {
            "::std::string::String"
        }).unwrap()
    }

    /// Result type.
    pub fn result_ty(&self) -> Path {
        syn::parse_str(if self.no_std {
            "::core::result::Result"
        } else {
            "::std::result::Result"
        }).unwrap()
    }

    /// Option type.
    pub fn option_ty(&self) -> Path {
        syn::parse_str(if self.no_std {
            "::core::option::Option"
        } else {
            "::std::option::Option"
        }).unwrap()
    }

    /// PhantomData type.
    pub fn phantom_data_ty(&self) -> Path {
        syn::parse_str(if self.no_std {
            "::core::marker::PhantomData"
        } else {
            "::std::marker::PhantomData"
        }).unwrap()
    }

    /// Default trait.
    pub fn default_trait(&self) -> Path {
        syn::parse_str(if self.no_std {
            "::core::default::Default"
        } else {
            "::std::default::Default"
        }).unwrap()
    }

    /// Clone trait.
    pub fn clone_trait(&self) -> Path {
        syn::parse_str(if self.no_std {
            "::core::clone::Clone"
        } else {
            "::std::clone::Clone"
        }).unwrap()
    }

    /// Into trait.
    #[cfg_attr(feature = "cargo-clippy", allow(wrong_self_convention))]
    pub fn into_trait(&self) -> Path {
        syn::parse_str(if self.no_std {
            "::core::convert::Into"
        } else {
            "::std::convert::Into"
        }).unwrap()
    }

    /// TryInto trait.
    pub fn try_into_trait(&self) -> Path {
        syn::parse_str(if self.no_std {
            "::core::convert::TryInto"
        } else {
            "::std::convert::TryInto"
        }).unwrap()
    }
}