deltoid/
string.rs

1//!
2
3use crate::{Apply, Core, Delta, DeltaResult, FromDelta, IntoDelta};
4use std::borrow::Cow;
5
6impl Core for String {
7    type Delta = StringDelta;
8}
9
10impl Apply for String {
11    fn apply(&self, delta: Self::Delta) -> DeltaResult<Self> {
12        Self::from_delta(delta)
13    }
14}
15
16impl Delta for String {
17    fn delta(&self, rhs: &Self) -> DeltaResult<Self::Delta> {
18        rhs.clone().into_delta()
19    }
20}
21
22impl FromDelta for String {
23    fn from_delta(delta: Self::Delta) -> DeltaResult<Self> {
24        delta.0.ok_or_else(|| ExpectedValue!("StringDelta<T>"))
25    }
26}
27
28impl IntoDelta for String {
29    fn into_delta(self) -> DeltaResult<Self::Delta> {
30        Ok(StringDelta(Some(self)))
31    }
32}
33
34
35#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
36#[derive(serde_derive::Deserialize, serde_derive::Serialize)]
37pub struct StringDelta( // TODO: Improve delta space efficiency
38    #[doc(hidden)] pub Option<String>
39);
40
41impl std::fmt::Debug for StringDelta {
42    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
43        match &self.0 {
44            Some(field) => write!(f, "StringDelta({:#?})", field),
45            None        => write!(f, "StringDelta(None)"),
46        }
47    }
48}
49
50
51
52#[derive(PartialEq, Eq, PartialOrd, Ord, Hash)]
53#[derive(serde_derive::Deserialize, serde_derive::Serialize)]
54pub struct Str<'s>(pub Cow<'s, str>);
55
56impl<'s> std::fmt::Debug for Str<'s> {
57    fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
58        write!(f, "{}", self.0)
59    }
60}
61
62impl<'s> std::ops::Deref for Str<'s> {
63    type Target = str;
64
65    fn deref(&self) -> &Self::Target { &self.0 }
66}
67
68impl<'s> Default for Str<'s> {
69    fn default() -> Self { Self(Cow::Borrowed("")) }
70}
71
72impl<'s> std::clone::Clone for Str<'s> {
73    fn clone(&self) -> Self { Self(self.0.to_owned()) }
74}
75
76impl<'s> From<&'s str> for Str<'s> {
77    fn from(s: &'s str) -> Self { Self(Cow::Borrowed(s)) }
78}
79
80impl<'s> From<String> for Str<'s> {
81    fn from(s: String) -> Self { Self(Cow::Owned(s)) }
82}
83
84
85impl<'s> Core for Str<'s> {
86    type Delta = StrDelta;
87}
88
89impl<'s> Apply for Str<'s> {
90    fn apply(&self, delta: Self::Delta) -> DeltaResult<Self> {
91        Ok(match delta.0 {
92            Some(d) => Self(std::borrow::Cow::Owned(d)),
93            None => self.clone(),
94        })
95    }
96}
97
98impl<'s> Delta for Str<'s> {
99    fn delta(&self, rhs: &Self) -> DeltaResult<Self::Delta> {
100        rhs.clone().into_delta()
101    }
102}
103
104impl<'s> FromDelta for Str<'s> {
105    fn from_delta(delta: Self::Delta) -> DeltaResult<Self> {
106        delta.0
107            .map(|s| Self(Cow::Owned(s)))
108            .ok_or_else(|| ExpectedValue!("StrDelta"))
109    }
110}
111
112impl<'s> IntoDelta for Str<'s> {
113    fn into_delta(self) -> DeltaResult<Self::Delta> {
114        match self.0 {
115            Cow::Borrowed(b) => Ok(StrDelta(Some(b.to_owned()))),
116            Cow::Owned(o)    => Ok(StrDelta(Some(o))),
117        }
118    }
119}
120
121
122#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
123#[derive(serde_derive::Deserialize, serde_derive::Serialize)]
124pub struct StrDelta( // TODO: Improve delta space efficiency
125    #[doc(hidden)] pub Option<String>
126);
127
128impl std::fmt::Debug for StrDelta {
129    fn fmt(&self, f: &mut std::fmt::Formatter) -> Result<(), std::fmt::Error> {
130        match &self.0 {
131            Some(field) => write!(f, "StrDelta({:#?})", field),
132            None        => write!(f, "StrDelta(None)"),
133        }
134    }
135}
136
137
138
139#[allow(non_snake_case)]
140#[cfg(test)]
141mod tests {
142    use serde_json;
143    use super::*;
144
145    #[test]
146    fn String__delta__same_values() -> DeltaResult<()> {
147        let s0 = String::from("foo");
148        let s1 = String::from("foo");
149        let delta: <String as Core>::Delta = s0.delta(&s1)?;
150        let json_string = serde_json::to_string(&delta)
151            .expect("Could not serialize to json");
152        println!("json_string: {}", json_string);
153        assert_eq!(json_string, "\"foo\"");
154        let delta1: <String as Core>::Delta = serde_json::from_str(
155            &json_string
156        ).expect("Could not deserialize from json");
157        assert_eq!(delta, delta1);
158        assert_eq!(delta, String::from("foo").into_delta()?);
159        Ok(())
160    }
161
162    #[test]
163    fn String__delta__different_values() -> DeltaResult<()> {
164        let s0 = String::from("foo");
165        let s1 = String::from("bar");
166        let delta: <String as Core>::Delta = s0.delta(&s1)?;
167        let json_string = serde_json::to_string(&delta)
168            .expect("Could not serialize to json");
169        println!("json_string: {}", json_string);
170        assert_eq!(json_string, "\"bar\"");
171        let delta1: <String as Core>::Delta = serde_json::from_str(
172            &json_string
173        ).expect("Could not deserialize from json");
174        assert_eq!(delta, delta1);
175        assert_eq!(delta, String::from("bar").into_delta()?);
176        Ok(())
177    }
178
179    #[test]
180    fn String__apply__same_values() -> DeltaResult<()> {
181        let s0 = String::from("foo");
182        let s1 = String::from("foo");
183        let delta: <String as Core>::Delta = s0.delta(&s1)?;
184        let s2 = s0.apply(delta)?;
185        assert_eq!(s1, s2);
186        Ok(())
187    }
188
189    #[test]
190    fn String__apply__different_values() -> DeltaResult<()> {
191        let s0 = String::from("foo");
192        let s1 = String::from("bar");
193        let delta: <String as Core>::Delta = s0.delta(&s1)?;
194        let s2 = s0.apply(delta)?;
195        assert_eq!(s1, s2);
196        Ok(())
197    }
198
199
200    #[test]
201    fn Str__delta__same_values() -> DeltaResult<()> {
202        let s0: Str<'static> = Str::from("foo");
203        let s1: Str<'static> = Str::from("foo");
204        let delta: <Str as Core>::Delta = s0.delta(&s1)?;
205        let json_string = serde_json::to_string(&delta)
206            .expect("Could not serialize to json");
207        println!("json_string: {}", json_string);
208        assert_eq!(json_string, "\"foo\"");
209        let delta1: <Str as Core>::Delta = serde_json::from_str(
210            &json_string
211        ).expect("Could not deserialize from json");
212        assert_eq!(delta, delta1);
213        assert_eq!(delta, Str::from("foo").into_delta()?);
214        Ok(())
215    }
216
217    #[test]
218    fn Str__delta__different_values() -> DeltaResult<()> {
219        let s0: Str<'static> = Str::from("foo");
220        let s1: Str<'static> = Str::from("bar");
221        let delta: <Str as Core>::Delta = s0.delta(&s1)?;
222        let json_string = serde_json::to_string(&delta)
223            .expect("Could not serialize to json");
224        println!("json_string: {}", json_string);
225        assert_eq!(json_string, "\"bar\"");
226        let delta1: <Str as Core>::Delta = serde_json::from_str(
227            &json_string
228        ).expect("Could not deserialize from json");
229        assert_eq!(delta, delta1);
230        assert_eq!(delta, Str::from("bar").into_delta()?);
231        Ok(())
232    }
233
234    #[test]
235    fn Str__apply__same_values() -> DeltaResult<()> {
236        let s0: Str<'static> = Str::from("foo");
237        let s1: Str<'static> = Str::from("foo");
238        let delta: <Str as Core>::Delta = s0.delta(&s1)?;
239        let s2 = s0.apply(delta)?;
240        assert_eq!(s1, s2);
241        Ok(())
242    }
243
244    #[test]
245    fn Str__apply__different_values() -> DeltaResult<()> {
246        let s0: Str<'static> = Str::from("foo");
247        let s1: Str<'static> = Str::from("bar");
248        let delta: <Str as Core>::Delta = s0.delta(&s1)?;
249        let s2 = s0.apply(delta)?;
250        assert_eq!(s1, s2);
251        Ok(())
252    }
253}