tiger_lib/report/
error_loc.rs

1use crate::block::{BV, Block, BlockItem, Field};
2use crate::fileset::FileEntry;
3use crate::token::{Loc, Token};
4use crate::trigger::Part;
5use crate::validator::ValueValidator;
6
7/// This trait lets the error reporting functions accept a variety of things as the error locator.
8pub trait ErrorLoc {
9    fn loc_length(&self) -> usize {
10        1
11    }
12    fn into_loc(self) -> Loc;
13}
14
15impl ErrorLoc for ValueValidator<'_> {
16    fn loc_length(&self) -> usize {
17        self.value().loc_length()
18    }
19
20    fn into_loc(self) -> Loc {
21        self.value().into_loc()
22    }
23}
24
25impl ErrorLoc for &ValueValidator<'_> {
26    fn loc_length(&self) -> usize {
27        self.value().loc_length()
28    }
29
30    fn into_loc(self) -> Loc {
31        self.value().into_loc()
32    }
33}
34
35impl ErrorLoc for &mut ValueValidator<'_> {
36    fn loc_length(&self) -> usize {
37        self.value().loc_length()
38    }
39
40    fn into_loc(self) -> Loc {
41        self.value().into_loc()
42    }
43}
44
45impl ErrorLoc for BlockItem {
46    fn loc_length(&self) -> usize {
47        match self {
48            BlockItem::Value(token) => token.loc_length(),
49            BlockItem::Block(block) => block.loc_length(),
50            BlockItem::Field(field) => field.loc_length(),
51        }
52    }
53
54    fn into_loc(self) -> Loc {
55        match self {
56            BlockItem::Value(token) => token.into_loc(),
57            BlockItem::Block(block) => block.into_loc(),
58            BlockItem::Field(field) => field.into_loc(),
59        }
60    }
61}
62
63impl ErrorLoc for &BlockItem {
64    fn loc_length(&self) -> usize {
65        match self {
66            BlockItem::Value(token) => token.loc_length(),
67            BlockItem::Block(block) => block.loc_length(),
68            BlockItem::Field(field) => field.loc_length(),
69        }
70    }
71
72    fn into_loc(self) -> Loc {
73        match self {
74            BlockItem::Value(token) => token.into_loc(),
75            BlockItem::Block(block) => block.into_loc(),
76            BlockItem::Field(field) => field.into_loc(),
77        }
78    }
79}
80
81impl ErrorLoc for Field {
82    fn into_loc(self) -> Loc {
83        self.into_key().into_loc()
84    }
85}
86
87impl ErrorLoc for &Field {
88    fn into_loc(self) -> Loc {
89        self.key().into_loc()
90    }
91}
92
93impl ErrorLoc for BV {
94    fn loc_length(&self) -> usize {
95        match self {
96            BV::Value(token) => token.loc_length(),
97            BV::Block(block) => block.loc_length(),
98        }
99    }
100
101    fn into_loc(self) -> Loc {
102        match self {
103            BV::Value(token) => token.into_loc(),
104            BV::Block(block) => block.into_loc(),
105        }
106    }
107}
108
109impl ErrorLoc for &BV {
110    fn loc_length(&self) -> usize {
111        match self {
112            BV::Value(token) => token.loc_length(),
113            BV::Block(block) => block.loc_length(),
114        }
115    }
116
117    fn into_loc(self) -> Loc {
118        match self {
119            BV::Value(t) => t.into_loc(),
120            BV::Block(s) => s.into_loc(),
121        }
122    }
123}
124
125impl ErrorLoc for FileEntry {
126    fn into_loc(self) -> Loc {
127        Loc::from(&self)
128    }
129}
130
131impl ErrorLoc for &FileEntry {
132    fn into_loc(self) -> Loc {
133        Loc::from(self)
134    }
135}
136
137impl ErrorLoc for Loc {
138    fn into_loc(self) -> Loc {
139        self
140    }
141}
142
143impl ErrorLoc for Token {
144    fn loc_length(&self) -> usize {
145        self.as_str().chars().count().max(1)
146    }
147
148    fn into_loc(self) -> Loc {
149        self.loc
150    }
151}
152
153impl ErrorLoc for &Token {
154    fn loc_length(&self) -> usize {
155        self.as_str().chars().count().max(1)
156    }
157
158    fn into_loc(self) -> Loc {
159        self.loc
160    }
161}
162
163impl ErrorLoc for Block {
164    fn into_loc(self) -> Loc {
165        self.loc
166    }
167}
168
169impl ErrorLoc for &Block {
170    fn into_loc(self) -> Loc {
171        self.loc
172    }
173}
174
175impl ErrorLoc for Part {
176    fn into_loc(self) -> Loc {
177        match self {
178            Part::Token(t) | Part::TokenArgument(t, _, _) => t.loc,
179        }
180    }
181
182    fn loc_length(&self) -> usize {
183        match self {
184            Part::Token(t) => t.loc_length(),
185            Part::TokenArgument(part, _, _) => part.loc_length(),
186        }
187    }
188}
189
190impl ErrorLoc for &Part {
191    fn into_loc(self) -> Loc {
192        match self {
193            Part::Token(t) | Part::TokenArgument(t, _, _) => t.loc,
194        }
195    }
196
197    fn loc_length(&self) -> usize {
198        match self {
199            Part::Token(t) => t.loc_length(),
200            Part::TokenArgument(part, _, _) => part.loc_length(),
201        }
202    }
203}