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
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
//! Op

use std::fmt;

use smallvec::SmallVec;
use Atom;
use UUID;

/// Op terminator
#[derive(Clone, Copy, PartialEq, Eq)]
pub enum Terminator {
    /// Raw ops are stand-alone within a frame.
    Raw,
    /// Query ops, as well as reduced ops following them, create a chunk in a frame.
    Query,
    /// Header ops, as well as reduced ops following them, create a chunk in a frame.
    Header,
    /// Reduced ops belong to the query/header op before them.
    Reduced,
}

impl fmt::Debug for Terminator {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self)
    }
}

impl fmt::Display for Terminator {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match self {
            Terminator::Raw => f.write_str(";"),
            Terminator::Reduced => f.write_str(","),
            Terminator::Header => f.write_str("!"),
            Terminator::Query => f.write_str("?"),
        }
    }
}

impl Default for Terminator {
    fn default() -> Terminator {
        Terminator::Reduced
    }
}

impl Terminator {
    /// Parse a terminator from `inp`.
    pub fn from_string(inp: &str) -> Result<Terminator, &'static str> {
        match &inp {
            &";" => Ok(Terminator::Raw),
            &"?" => Ok(Terminator::Query),
            &"!" => Ok(Terminator::Header),
            &"," => Ok(Terminator::Reduced),
            _ => Err("invalid terminator"),
        }
    }

    /// Parse a terminator from `inp`.
    pub fn from_char(inp: char) -> Result<Terminator, &'static str> {
        Terminator::from_string(&inp.to_string())
    }
}

/// An Op (operation) in RON describes part of the initial state of an object, or a specific
/// action on an object, or some other part related to the Swarm protocol (such as a query or
/// handshake).
///
/// Every op consists of four UUIDs (type, object, event and re), a (possibly empty) sequence of
/// atoms, and a terminator.
#[derive(Clone, PartialEq)]
pub struct Op {
    /// Op type.
    pub ty: UUID,
    /// Object this Op modifies.
    pub object: UUID,
    /// Event timestamp
    pub event: UUID,
    /// Location/field inside the object.
    pub location: UUID,
    /// Op payload.
    pub atoms: SmallVec<[Atom; 3]>,
    /// Op terminator.
    pub term: Terminator,
}

impl Op {
    /// Parse a single Op from string `input` into `prev`. If `prev` is not `None` the Op may be
    /// compressed against `prev`.
    pub fn parse_inplace<'a>(
        prev: &mut Option<Op>, input: &'a str,
    ) -> Option<&'a str> {
        let mut ret = input;
        let mut ty = None;
        let mut event = None;
        let mut object = None;
        let mut location = None;

        {
            let mut prev_uu = prev.as_ref().map(|p| &p.location);

            // type
            ret = ret.trim_start();
            if ret.starts_with('*') {
                let ctx = prev.as_ref().map(|p| (&p.ty, prev_uu.unwrap()));

                match UUID::parse(&ret[1..], ctx) {
                    Some((t, rest)) => {
                        ret = rest;
                        ty = Some(t);
                        prev_uu = ty.as_ref();
                    }
                    None => {
                        ret = &ret[1..];
                        ty = prev.as_ref().map(|p| p.ty.clone());
                        prev_uu = prev.as_ref().map(|p| &p.ty);
                    }
                }
            } else {
                prev_uu = prev.as_ref().map(|p| &p.ty);
            }

            // object
            ret = ret.trim_start();
            if ret.starts_with('#') {
                let ctx = prev.as_ref().map(|p| (&p.object, prev_uu.unwrap()));

                match UUID::parse(&ret[1..], ctx) {
                    Some((obj, rest)) => {
                        ret = rest;
                        object = Some(obj);
                        prev_uu = object.as_ref();
                    }
                    None => {
                        ret = &ret[1..];
                        object = prev.as_ref().map(|p| p.object.clone());
                        prev_uu = prev.as_ref().map(|p| &p.object);
                    }
                }
            } else {
                prev_uu = prev.as_ref().map(|p| &p.object);
            }

            // event
            ret = ret.trim_start();
            if ret.starts_with('@') {
                let ctx = prev.as_ref().map(|p| (&p.event, prev_uu.unwrap()));

                match UUID::parse(&ret[1..], ctx) {
                    Some((ev, rest)) => {
                        ret = rest;
                        event = Some(ev);
                        prev_uu = event.as_ref();
                    }
                    None => {
                        ret = &ret[1..];
                        event = prev.as_ref().map(|p| p.event.clone());
                        prev_uu = prev.as_ref().map(|p| &p.event);
                    }
                }
            } else {
                prev_uu = prev.as_ref().map(|p| &p.event);
            }

            // location
            ret = ret.trim_start();
            if ret.starts_with(':') {
                let ctx =
                    prev.as_ref().map(|p| (&p.location, prev_uu.unwrap()));

                match UUID::parse(&ret[1..], ctx) {
                    Some((loc, rest)) => {
                        ret = rest;
                        location = Some(loc);
                    }
                    None => {
                        ret = &ret[1..];
                        location = prev.as_ref().map(|p| p.location.clone());
                    }
                }
            }
        }

        let no_spec = ty.is_none()
            && event.is_none()
            && object.is_none()
            && location.is_none();

        let prev = match prev {
            &mut Some(ref mut prev) => {
                if let Some(ty) = ty {
                    prev.ty = ty;
                }
                if let Some(event) = event {
                    prev.event = event;
                }
                if let Some(object) = object {
                    prev.object = object;
                }
                if let Some(location) = location {
                    prev.location = location;
                }

                prev
            }
            &mut None
                if ty.is_some() && object.is_some() && event.is_some() =>
            {
                *prev = Some(Op {
                    ty: ty.unwrap(),
                    object: object.unwrap(),
                    event: event.unwrap(),
                    location: location.unwrap_or_else(UUID::zero),
                    atoms: Default::default(),
                    term: Terminator::Header,
                });
                prev.as_mut().unwrap()
            }
            &mut None => {
                return None;
            }
        };

        // atoms
        prev.atoms.clear();

        {
            let mut prev_uu = prev.object.clone();

            while let Some((atm, rest)) =
                Atom::parse(ret, Some((&prev.object, &prev_uu)))
            {
                match &atm {
                    &Atom::UUID(ref uu) => {
                        prev_uu = uu.clone();
                    }
                    _ => {}
                }

                ret = rest;
                prev.atoms.push(atm);
            }
        }

        // Terminator
        ret = ret.trim_start();
        match ret.chars().next() {
            Some('!') => {
                prev.term = Terminator::Header;
                ret = &ret[1..];
            }
            Some('?') => {
                prev.term = Terminator::Query;
                ret = &ret[1..];
            }
            Some(',') => {
                prev.term = Terminator::Reduced;
                ret = &ret[1..];
            }
            Some(';') => {
                prev.term = Terminator::Raw;
                ret = &ret[1..];
            }
            _ if !no_spec || !prev.atoms.is_empty() => {
                prev.term = Terminator::Reduced;
            }
            _ => {
                return None;
            }
        }

        Some(ret)
    }

    /// Serialize Op to text optionally compressing it against `context`.
    pub fn compress(&self, context: Option<&Op>) -> String {
        let mut ret = String::default();

        if context.map(|o| o.ty != self.ty).unwrap_or(true) {
            let ctx = context.map(|p| (&p.ty, &p.ty));

            ret += "*";
            ret += &self.ty.compress(ctx);
        }

        if context.map(|o| o.object != self.object).unwrap_or(true) {
            let ctx = context.map(|p| (&p.object, &p.ty));

            ret += "#";
            ret += &self.object.compress(ctx);
        }

        if context.map(|o| o.event != self.event).unwrap_or(true) {
            let ctx = context.map(|p| (&p.event, &p.object));

            ret += "@";
            ret += &self.event.compress(ctx);
        }

        if context.map(|o| o.location != self.location).unwrap_or(true) {
            let ctx = context.map(|p| (&p.location, &p.event));

            ret += ":";
            ret += &self.location.compress(ctx);
        }

        if ret.is_empty() {
            ret = "@".to_string();
        }

        let mut prev = &self.object;
        for atm in self.atoms.iter() {
            match atm {
                &Atom::Integer(i) => {
                    ret += &format!("={}", i);
                }
                &Atom::String(ref i) => {
                    ret += &format!("'{}'", i);
                }
                &Atom::Float(i) => {
                    ret += &format!("^{}", i);
                }
                &Atom::UUID(ref i) => {
                    ret += ">";
                    ret += &i.compress(Some((prev, prev)));
                    prev = i;
                }
            }
        }

        ret + &format!("{}", self.term)
    }
}
impl fmt::Debug for Op {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(f, "{}", self)
    }
}

impl fmt::Display for Op {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        write!(
            f,
            "*{}#{}@{}:{}",
            self.ty, self.object, self.event, self.location
        )?;

        match self.atoms.len() {
            0 => write!(f, "{}", self.term),
            1 => write!(f, "{}{}", self.atoms[0], self.term),
            _ => {
                write!(f, "{}", self.atoms[0])?;
                for atm in self.atoms[1..].iter() {
                    write!(f, ", {}", atm)?;
                }
                write!(f, "{}", self.term)
            }
        }
    }
}

#[test]
fn compress_roundtrip() {
    use Frame;
    let f = Frame::parse(
        "
        *set#mice@1YKDXO3201+1YKDXO!
                 @>mouse$1YKDXO
                 @(WBF901(WBY>mouse$1YKDWBY
                 @[67H01[6>mouse$1YKDW6
                 @(Uh4j01(Uh>mouse$1YKDUh
                 @(S67V01(S6>mouse$1YKDS6
                 @(Of(N3:1YKDN3DS01+1YKDN3,
                 @(MvBV01(IuJ:0>mouse$1YKDIuJ
                 @(LF:1YKDIuEY01+1YKDIuJ,
                 :{A601,
                 @(Io5l01[oA:0>mouse$1YKDIoA
                 @[l7_01[l>mouse$1YKDIl
                 @(57(4B:1YKD4B3f01+1YKD4B,
                 @(0bB401+1YKCsd:0>mouse$1Y",
    );

    let ops = f.into_iter().collect::<Vec<_>>();
    for win in ops.windows(2) {
        let ctx = &win[0];
        let op = &win[1];
        let txt = op.compress(Some(ctx));
        let mut back = Some(ctx.clone());

        Op::parse_inplace(&mut back, &txt[..]);

        eprintln!("ctx: {}", ctx);
        eprintln!("op : {}", op);
        eprintln!("txt: {}", txt);
        eprintln!("rt : {}", back.as_ref().unwrap());
        eprintln!("");
        assert_eq!(back.unwrap(), *op);
    }
}