automerge/legacy/utility_impls/
key.rs

1use std::cmp::{Ordering, PartialOrd};
2
3use smol_str::SmolStr;
4
5use crate::legacy::{ElementId, Key, OpId};
6
7impl PartialOrd for Key {
8    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
9        Some(self.cmp(other))
10    }
11}
12
13impl Ord for Key {
14    fn cmp(&self, other: &Self) -> Ordering {
15        match (self, other) {
16            (Key::Map(a), Key::Map(b)) => a.cmp(b),
17            (Key::Seq(a), Key::Seq(b)) => a.cmp(b),
18            (Key::Map(_), _) => Ordering::Less,
19            (_, Key::Map(_)) => Ordering::Greater,
20        }
21    }
22}
23
24impl From<OpId> for Key {
25    fn from(id: OpId) -> Self {
26        Key::Seq(ElementId::Id(id))
27    }
28}
29
30impl From<&OpId> for Key {
31    fn from(id: &OpId) -> Self {
32        Key::Seq(ElementId::Id(id.clone()))
33    }
34}
35
36impl From<ElementId> for Key {
37    fn from(id: ElementId) -> Self {
38        Key::Seq(id)
39    }
40}
41
42impl<S> From<S> for Key
43where
44    S: AsRef<str>,
45{
46    fn from(s: S) -> Self {
47        Key::Map(SmolStr::new(s))
48    }
49}