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
pub(crate) use core::hash::Hash;
use std::{
    borrow::Borrow,
    fmt::Debug,
    marker::PhantomData,
    ops::{Deref, Index},
};
use thiserror::Error;

/// Default validation error. It is used for [`define!`](prae_macro::define) macro with `ensure`
/// keyword.
#[derive(Debug, PartialEq, Error)]
#[error("provided value is not valid")]
pub struct ValidationError;

/// A trait that represents a guard bound, e.g. a type that is being guarded, `adjust`/`validate`
/// functions and a possible validation error.
pub trait Guard {
    /// The type that is being guarded.
    type Target;
    /// An error that will be returned in case of failed validation.
    type Error;
    /// A function that can make small adjustments of the
    /// provided value before validation.
    fn adjust(v: &mut Self::Target);
    /// A function that validates provided value. If the value
    /// is not valid, it returns `Some(Self::Error)`.
    fn validate(v: &Self::Target) -> Option<Self::Error>;
}

/// A thin wrapper around an underlying type and a [`Guard`](Guard) bounded to it. It guarantees
/// to always hold specified invariants and act as close as possible to the underlying type.
#[derive(Debug)]
pub struct Guarded<T, E, G: Guard<Target = T, Error = E>>(T, PhantomData<E>, PhantomData<G>);

impl<T, E, G> Guarded<T, E, G>
where
    E: std::fmt::Debug,
    G: Guard<Target = T, Error = E>,
{
    /// Constructor. Will return an error if the provided argument `v`
    /// doesn't pass the validation.
    pub fn new<V: Into<T>>(v: V) -> Result<Self, E> {
        let mut v: T = v.into();
        G::adjust(&mut v);
        G::validate(&v).map_or(Ok(()), Err)?;
        Ok(Self(v, Default::default(), Default::default()))
    }

    /// Returns a shared reference to the inner value.
    pub fn get(&self) -> &T {
        &self.0
    }

    /// Mutates current value using provided closure. Will panic if
    /// the result of the mutation is invalid.
    pub fn mutate(&mut self, f: impl FnOnce(&mut T)) {
        f(&mut self.0);
        G::adjust(&mut self.0);
        // We have to match here because Option.expect_none is unstable.
        // See: https://github.com/rust-lang/rust/issues/62633
        match G::validate(&self.0) {
            None => {}
            Some(e) => panic!("validation failed with error {:?}", e),
        };
    }

    /// Mutates current value using provided closure. Will return an error if
    /// the result of the mutation is invalid.
    pub fn try_mutate(&mut self, f: impl FnOnce(&mut T)) -> Result<(), E>
    where
        T: Clone,
    {
        let mut cloned = self.0.clone();
        f(&mut cloned);
        G::adjust(&mut cloned);
        G::validate(&cloned).map_or(Ok(()), Err)?;
        self.0 = cloned;
        Ok(())
    }
}

impl<T: Clone, E, G: Guard<Target = T, Error = E>> Clone for Guarded<T, E, G> {
    fn clone(&self) -> Self {
        Self(self.0.clone(), Default::default(), Default::default())
    }
}

impl<T, E, G: Guard<Target = T, Error = E>> Borrow<T> for Guarded<T, E, G> {
    fn borrow(&self) -> &T {
        &self.0
    }
}

impl<T, E, G: Guard<Target = T, Error = E>> AsRef<T> for Guarded<T, E, G> {
    fn as_ref(&self) -> &T {
        &self.0
    }
}

impl<T, E, G: Guard<Target = T, Error = E>> Deref for Guarded<T, E, G> {
    type Target = T;
    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl<T: PartialEq, E, G: Guard<Target = T, Error = E>> PartialEq for Guarded<T, E, G> {
    fn eq(&self, other: &Self) -> bool {
        self.0.eq(&other.0)
    }
}

impl<T: Eq, E, G: Guard<Target = T, Error = E>> Eq for Guarded<T, E, G> {}

impl<T: PartialOrd, E, G: Guard<Target = T, Error = E>> PartialOrd for Guarded<T, E, G> {
    fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
        self.0.partial_cmp(&other.0)
    }
}

impl<T: Ord, E, G: Guard<Target = T, Error = E>> Ord for Guarded<T, E, G> {
    fn cmp(&self, other: &Self) -> std::cmp::Ordering {
        self.0.cmp(&other.0)
    }
}

impl<T: Copy, E, G: Guard<Target = T, Error = E>> Copy for Guarded<T, E, G> {}

impl<T: Hash, E, G: Guard<Target = T, Error = E>> Hash for Guarded<T, E, G> {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        self.0.hash(state)
    }
}

impl<T: Index<U>, U, E, G: Guard<Target = T, Error = E>> Index<U> for Guarded<T, E, G> {
    type Output = T::Output;
    fn index(&self, index: U) -> &Self::Output {
        self.0.index(index)
    }
}

#[cfg(feature = "serde")]
impl<'de, T, E, G> serde::Deserialize<'de> for Guarded<T, E, G>
where
    T: serde::Deserialize<'de>,
    E: std::fmt::Display + std::fmt::Debug,
    G: Guard<Target = T, Error = E>,
{
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: serde::Deserializer<'de>,
    {
        Self::new(T::deserialize(deserializer)?).map_err(|e: E| serde::de::Error::custom(e))
    }
}

#[cfg(feature = "serde")]
impl<T, E, G> serde::Serialize for Guarded<T, E, G>
where
    T: serde::Serialize,
    E: std::fmt::Display + std::fmt::Debug,
    G: Guard<Target = T, Error = E>,
{
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        T::serialize(self.get(), serializer)
    }
}

#[cfg(test)]
mod tests {
    mod validate_guard {
        use crate::core::*;

        #[derive(Debug)]
        struct UsernameGuard;
        impl Guard for UsernameGuard {
            type Target = String;
            type Error = &'static str;
            fn adjust(v: &mut Self::Target) {
                *v = v.trim().to_owned();
            }
            fn validate(v: &Self::Target) -> Option<Self::Error> {
                if v.is_empty() {
                    Some("username is empty")
                } else {
                    None
                }
            }
        }
        type Username = Guarded<String, &'static str, UsernameGuard>;

        #[test]
        fn construction_with_valid_value_succeeds() {
            let un = Username::new(" username\n").unwrap();
            assert_eq!(un.get(), "username");
        }

        #[test]
        fn construction_with_invalid_value_fails() {
            Username::new("   \n").unwrap_err();
        }

        #[test]
        fn mutation_with_valid_value_succeeds() {
            let mut un = Username::new("username").unwrap();
            un.mutate(|v| *v = format!(" new {}\n", v));
            assert_eq!(un.get(), "new username");
        }

        #[test]
        #[should_panic]
        fn mutation_with_invalid_value_panics() {
            let mut un = Username::new("username").unwrap();
            un.mutate(|v| *v = "   \n".to_owned());
        }

        #[test]
        fn falliable_mutation_with_valid_value_succeds() {
            let mut un = Username::new("username").unwrap();
            un.try_mutate(|v| *v = format!(" new {}\n", v)).unwrap();
            assert_eq!(un.get(), "new username");
        }

        #[test]
        fn falliable_mutation_with_valid_value_fails() {
            let mut un = Username::new("username").unwrap();
            un.try_mutate(|v| *v = "   \n".to_owned()).unwrap_err();
            assert_eq!(un.get(), "username");
        }

        #[cfg(feature = "serde")]
        mod serde {
            use super::*;
            use ::serde::{Deserialize, Serialize};

            #[derive(Debug, Deserialize, Serialize)]
            struct User {
                username: Username,
            }

            #[test]
            fn serialization_succeeds() {
                let u = User {
                    username: Username::new("  john doe  ").unwrap(),
                };
                let j = serde_json::to_string(&u).unwrap();
                assert_eq!(j, r#"{"username":"john doe"}"#)
            }

            #[test]
            fn deserialization_fails_with_invalid_value() {
                let e = serde_json::from_str::<User>(r#"{ "username": "  " }"#).unwrap_err();
                assert_eq!(e.to_string(), "username is empty at line 1 column 20");
            }

            #[test]
            fn deserialization_succeeds_with_valid_value() {
                let u = serde_json::from_str::<User>(r#"{ "username": "  john doe  " }"#).unwrap();
                assert_eq!(u.username.get(), "john doe");
            }
        }
    }
}