bluejay_validator/executable/document/
path.rs

1use bluejay_core::{executable::ExecutableDocument, Indexable};
2use std::cmp::{Eq, Ord, Ordering, PartialEq, PartialOrd};
3use std::hash::{Hash, Hasher};
4
5pub struct Path<'a, E: ExecutableDocument> {
6    root: PathRoot<'a, E>,
7    members: Vec<&'a E::Selection>,
8}
9
10impl<'a, E: ExecutableDocument> Clone for Path<'a, E> {
11    fn clone(&self) -> Self {
12        Self {
13            root: self.root,
14            members: self.members.clone(),
15        }
16    }
17}
18
19impl<'a, E: ExecutableDocument> Path<'a, E> {
20    pub fn new(root: PathRoot<'a, E>) -> Self {
21        Self {
22            root,
23            members: Vec::new(),
24        }
25    }
26
27    pub fn root(&self) -> &PathRoot<'a, E> {
28        &self.root
29    }
30
31    pub fn with_selection(&self, selection: &'a E::Selection) -> Self {
32        let mut clone = self.clone();
33        clone.members.push(selection);
34        clone
35    }
36}
37
38pub enum PathRoot<'a, E: ExecutableDocument> {
39    Operation(&'a E::OperationDefinition),
40    Fragment(&'a E::FragmentDefinition),
41}
42
43impl<'a, E: ExecutableDocument> Clone for PathRoot<'a, E> {
44    fn clone(&self) -> Self {
45        *self
46    }
47}
48
49impl<'a, E: ExecutableDocument> Copy for PathRoot<'a, E> {}
50
51impl<'a, E: ExecutableDocument> Hash for PathRoot<'a, E> {
52    fn hash<H: Hasher>(&self, state: &mut H) {
53        match self {
54            Self::Operation(o) => o.id().hash(state),
55            Self::Fragment(f) => f.id().hash(state),
56        }
57    }
58}
59
60impl<'a, E: ExecutableDocument> PartialEq for PathRoot<'a, E> {
61    fn eq(&self, other: &Self) -> bool {
62        match (self, other) {
63            (Self::Operation(l), Self::Operation(r)) => l.id() == r.id(),
64            (Self::Fragment(l), Self::Fragment(r)) => l.id() == r.id(),
65            _ => false,
66        }
67    }
68}
69
70impl<'a, E: ExecutableDocument> Eq for PathRoot<'a, E> {}
71
72impl<'a, E: ExecutableDocument> Ord for PathRoot<'a, E> {
73    fn cmp(&self, other: &Self) -> Ordering {
74        match (self, other) {
75            (Self::Fragment(l), Self::Fragment(r)) => l.id().cmp(r.id()),
76            (Self::Fragment(_), Self::Operation(_)) => Ordering::Greater,
77            (Self::Operation(_), Self::Fragment(_)) => Ordering::Less,
78            (Self::Operation(l), Self::Operation(r)) => l.id().cmp(r.id()),
79        }
80    }
81}
82
83impl<'a, E: ExecutableDocument> PartialOrd for PathRoot<'a, E> {
84    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
85        Some(self.cmp(other))
86    }
87}