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