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
use event::request::InvokeArgument;

pub trait Destructurable: Sized {
  fn destructure<T: FromInvokeArg>(self) -> Result<T, T::Err>;
}

impl Destructurable for InvokeArgument {
  fn destructure<T: FromInvokeArg>(self) -> Result<T, T::Err> {
    T::from_invoke_arg(self)
  }
}

pub trait FromInvokeArg: Sized {
  type Err;
  fn from_invoke_arg(InvokeArgument) -> Result<Self, Self::Err>;
}

#[derive(Debug, Clone, PartialEq)]
pub struct ImproperInvokeArgError {
  _priv: (),
}

// NOTE: This is a stopgap for impl specialization
//   The underlying issue is that FromInvokeArg can't be impld for all FromStr
//   because it creates a conflicting implementation with String
//   Issue: https://github.com/rust-lang/rust/issues/31844
macro_rules! impl_for_from_str {
  ($t: ty) => {
    impl FromInvokeArg for $t {
      type Err = ImproperInvokeArgError;

      fn from_invoke_arg(arg: InvokeArgument) -> Result<$t, ImproperInvokeArgError> {
        match arg {
          InvokeArgument::String(val) => {
            val.parse().map_err(|_| ImproperInvokeArgError { _priv: ()})
          },
          _ => Err(ImproperInvokeArgError { _priv: () })
        }
      }
    }
  }
}

impl<T> FromInvokeArg for Option<T>
  where T: FromInvokeArg
{
  type Err = ImproperInvokeArgError;

  fn from_invoke_arg(arg: InvokeArgument) -> Result<Option<T>, ImproperInvokeArgError> {
    match arg {
      InvokeArgument::None => Ok(None),
      e @ InvokeArgument::String(_) => {
        T::from_invoke_arg(e)
          .map(|r| Some(r))
          .map_err(|_| ImproperInvokeArgError { _priv: () })
      },
      _ => Err(ImproperInvokeArgError { _priv: () }),
    }
  }
}

impl_for_from_str!(f32);
impl_for_from_str!(f64);
impl_for_from_str!(isize);
impl_for_from_str!(i16);
impl_for_from_str!(i32);
impl_for_from_str!(i64);
impl_for_from_str!(usize);
impl_for_from_str!(u8);
impl_for_from_str!(u16);
impl_for_from_str!(u32);

impl FromInvokeArg for String {
  type Err = ImproperInvokeArgError;

  fn from_invoke_arg(arg: InvokeArgument) -> Result<String, ImproperInvokeArgError> {
    match arg {
      InvokeArgument::String(val) => Ok(val),
      _ => Err(ImproperInvokeArgError { _priv: () }),
    }
  }
}

// NOTE: Rules for booleans are a bit unconventional to facilitiate easy use of
// optional captures:
impl FromInvokeArg for bool {
  type Err = ImproperInvokeArgError;

  fn from_invoke_arg(arg: InvokeArgument) -> Result<bool, ImproperInvokeArgError> {
    match arg {
      InvokeArgument::None => Ok(false),
      InvokeArgument::String(val) => {
        match val.as_ref() {
          "false" => Ok(false),
          _ => Ok(true),
        }
      },
      _ => Err(ImproperInvokeArgError { _priv: () }),
    }
  }
}

impl FromInvokeArg for Vec<Vec<String>> {
  type Err = ImproperInvokeArgError;

  fn from_invoke_arg(arg: InvokeArgument) -> Result<Vec<Vec<String>>, ImproperInvokeArgError> {
    match arg {
      InvokeArgument::Table(val) => Ok(val),
      _ => Err(ImproperInvokeArgError { _priv: () }),
    }
  }
}

#[cfg(test)]
mod test {
  pub use super::*;
  pub use event::request::InvokeArgument;

  #[test]
  fn wrong_type_destructure_fails_correctly() {
    let res: Result<u32, ImproperInvokeArgError> = InvokeArgument::String("hello".to_owned())
      .destructure();

    assert_eq!(res, Err(ImproperInvokeArgError { _priv: () }));
  }

  #[test]
  fn string_can_be_destructured() {
    let res: String = InvokeArgument::String("hello".to_owned()).destructure().unwrap();

    assert_eq!(&res, "hello");
  }

  #[test]
  fn table_can_be_destructured() {
    let res: Vec<Vec<String>> =
      InvokeArgument::Table(vec![vec!["hello".to_owned()]]).destructure().unwrap();

    assert_eq!(res, vec![vec!["hello".to_owned()]]);
  }

  mod bool {
    use super::*;

    #[test]
    fn bool_doesnt_parse_from_table() {
      let res: Result<bool, ImproperInvokeArgError> =
        InvokeArgument::Table(vec![vec!["hello".to_owned()]]).destructure();

      assert_eq!(res, Err(ImproperInvokeArgError { _priv: () }));
    }

    #[test]
    fn bool_is_false_from_none() {
      let res: bool = InvokeArgument::None.destructure().unwrap();

      assert_eq!(res, false);
    }

    #[test]
    fn bool_is_false_string_false() {
      let res: bool = InvokeArgument::String("false".to_owned()).destructure().unwrap();

      assert_eq!(res, false);
    }

    #[test]
    fn bool_is_true_from_other_strings() {
      let res: bool = InvokeArgument::String("true".to_owned()).destructure().unwrap();
      assert_eq!(res, true);

      let res: bool = InvokeArgument::String("hello".to_owned()).destructure().unwrap();
      assert_eq!(res, true);
    }
  }

}