graphql_toolkit_ast/
pos.rs

1use std::{
2    borrow::{Borrow, BorrowMut},
3    cmp::Ordering,
4    fmt,
5    hash::{Hash, Hasher},
6};
7
8use serde::{Deserialize, Serialize};
9
10/// Original position of an element in source code.
11///
12/// You can serialize and deserialize it to the GraphQL `locations` format
13/// ([reference](https://spec.graphql.org/October2021/#sec-Errors)).
14#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Default, Hash, Serialize, Deserialize)]
15pub struct Pos {
16    /// One-based line number.
17    pub line: usize,
18
19    /// One-based column number.
20    pub column: usize,
21}
22
23impl fmt::Debug for Pos {
24    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
25        write!(f, "Pos({}:{})", self.line, self.column)
26    }
27}
28
29impl fmt::Display for Pos {
30    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
31        write!(f, "{}:{}", self.line, self.column)
32    }
33}
34
35impl From<(usize, usize)> for Pos {
36    fn from((line, column): (usize, usize)) -> Self {
37        Self { line, column }
38    }
39}
40
41/// An AST node that stores its original position.
42#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
43pub struct Positioned<T: ?Sized> {
44    /// The position of the node.
45    pub pos: Pos,
46    /// The node itself.
47    pub node: T,
48}
49
50impl<T> Positioned<T> {
51    /// Create a new positioned node from the node and its position.
52    #[must_use]
53    pub const fn new(node: T, pos: Pos) -> Positioned<T> {
54        Positioned { pos, node }
55    }
56
57    /// Get the inner node.
58    ///
59    /// This is most useful in callback chains where `Positioned::into_inner` is
60    /// easier to read than `|positioned| positioned.node`.
61    #[inline]
62    pub fn into_inner(self) -> T {
63        self.node
64    }
65
66    /// Create a new positioned node with the same position as this one.
67    #[must_use]
68    pub fn position_node<U>(&self, other: U) -> Positioned<U> {
69        Positioned::new(other, self.pos)
70    }
71
72    /// Map the inner value of this positioned node.
73    #[must_use]
74    pub fn map<U>(self, f: impl FnOnce(T) -> U) -> Positioned<U> {
75        Positioned::new(f(self.node), self.pos)
76    }
77}
78
79impl<T: fmt::Display> fmt::Display for Positioned<T> {
80    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
81        self.node.fmt(f)
82    }
83}
84impl<T: PartialEq> PartialEq for Positioned<T> {
85    fn eq(&self, other: &Self) -> bool {
86        self.node == other.node
87    }
88}
89impl<T: Eq> Eq for Positioned<T> {}
90impl<T: PartialOrd> PartialOrd for Positioned<T> {
91    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
92        self.node.partial_cmp(&other.node)
93    }
94}
95impl<T: Ord> Ord for Positioned<T> {
96    fn cmp(&self, other: &Self) -> Ordering {
97        self.node.cmp(&other.node)
98    }
99}
100impl<T: Hash> Hash for Positioned<T> {
101    fn hash<H: Hasher>(&self, state: &mut H) {
102        self.node.hash(state)
103    }
104}
105
106impl Borrow<str> for Positioned<String> {
107    fn borrow(&self) -> &str {
108        self.node.as_str()
109    }
110}
111
112impl BorrowMut<str> for Positioned<String> {
113    fn borrow_mut(&mut self) -> &mut str {
114        self.node.as_mut_str()
115    }
116}