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
use RawTokens;

/// 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) -> RawTokens<&'static str> {
        RawTokens(if self.no_std {
                      ":: collections :: string :: String"
                  } else {
                      ":: std :: string :: String"
                  })
    }

    /// Result type.
    pub fn result_ty(&self) -> RawTokens<&'static str> {
        RawTokens(if self.no_std {
                      ":: core :: result :: Result"
                  } else {
                      ":: std :: result :: Result"
                  })
    }

    /// Option type.
    pub fn option_ty(&self) -> RawTokens<&'static str> {
        RawTokens(if self.no_std {
                      ":: core :: option :: Option"
                  } else {
                      ":: std :: option :: Option"
                  })
    }

    /// PhantomData type.
    pub fn phantom_data_ty(&self) -> RawTokens<&'static str> {
        RawTokens(if self.no_std {
                      ":: core :: marker :: PhantomData"
                  } else {
                      ":: std :: marker :: PhantomData"
                  })
    }

    /// Default trait.
    pub fn default_trait(&self) -> RawTokens<&'static str> {
        RawTokens(if self.no_std {
                      ":: core :: default :: Default"
                  } else {
                      ":: std :: default :: Default"
                  })
    }

    /// Clone trait.
    pub fn clone_trait(&self) -> RawTokens<&'static str> {
        RawTokens(if self.no_std {
                      ":: core :: clone :: Clone"
                  } else {
                      ":: std :: clone :: Clone"
                  })
    }

    /// Into trait.
    pub fn into_trait(&self) -> RawTokens<&'static str> {
        RawTokens(if self.no_std {
                      ":: core :: convert :: Into"
                  } else {
                      ":: std :: convert :: Into"
                  })
    }

    /// TryInto trait.
    pub fn try_into_trait(&self) -> RawTokens<&'static str> {
        RawTokens(if self.no_std {
                      ":: core :: convert :: TryInto"
                  } else {
                      ":: std :: convert :: TryInto"
                  })
    }
}

#[test]
fn std() {
    let b = Bindings { no_std: false };

    assert_eq!(b.string_ty().to_tokens(), quote!(::std::string::String));

    assert_eq!(b.result_ty().to_tokens(), quote!(::std::result::Result));

    assert_eq!(b.option_ty().to_tokens(), quote!(::std::option::Option));

    assert_eq!(b.phantom_data_ty().to_tokens(),
               quote!(::std::marker::PhantomData));

    assert_eq!(b.default_trait().to_tokens(),
               quote!(::std::default::Default));

    assert_eq!(b.clone_trait().to_tokens(), quote!(::std::clone::Clone));

    assert_eq!(b.into_trait().to_tokens(), quote!(::std::convert::Into));
}

#[test]
fn no_std() {
    let b = Bindings { no_std: true };

    assert_eq!(b.string_ty().to_tokens(),
               quote!(::collections::string::String));

    assert_eq!(b.result_ty().to_tokens(), quote!(::core::result::Result));

    assert_eq!(b.option_ty().to_tokens(), quote!(::core::option::Option));

    assert_eq!(b.phantom_data_ty().to_tokens(),
               quote!(::core::marker::PhantomData));

    assert_eq!(b.default_trait().to_tokens(),
               quote!(::core::default::Default));

    assert_eq!(b.clone_trait().to_tokens(), quote!(::core::clone::Clone));

    assert_eq!(b.into_trait().to_tokens(), quote!(::core::convert::Into));
}