graphql_toolkit_ast/
pos.rs1use std::{
2 borrow::{Borrow, BorrowMut},
3 cmp::Ordering,
4 fmt,
5 hash::{Hash, Hasher},
6};
7
8use serde::{Deserialize, Serialize};
9
10#[derive(PartialOrd, Ord, PartialEq, Eq, Clone, Copy, Default, Hash, Serialize, Deserialize)]
15pub struct Pos {
16 pub line: usize,
18
19 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#[derive(Debug, Clone, Copy, Default, Serialize, Deserialize)]
43pub struct Positioned<T: ?Sized> {
44 pub pos: Pos,
46 pub node: T,
48}
49
50impl<T> Positioned<T> {
51 #[must_use]
53 pub const fn new(node: T, pos: Pos) -> Positioned<T> {
54 Positioned { pos, node }
55 }
56
57 #[inline]
62 pub fn into_inner(self) -> T {
63 self.node
64 }
65
66 #[must_use]
68 pub fn position_node<U>(&self, other: U) -> Positioned<U> {
69 Positioned::new(other, self.pos)
70 }
71
72 #[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}