Skip to main content

graphrt_core/
source_map.rs

1//! Source positions of the values in a workflow document.
2//!
3//! A [`SourceMap`] maps document paths (`graph.nodes[1].template`, see
4//! [`DocPath`]) to the line and column of the YAML or JSON node they came
5//! from, so validation errors can point at the offending text. It is built by
6//! a second, span-aware parse of the same text and is best effort: when the
7//! text cannot be parsed, or the `yaml` feature (which provides the span-aware
8//! parser, also used for JSON) is disabled, the map is empty and diagnostics
9//! carry a path but no position.
10
11use std::collections::HashMap;
12use std::fmt;
13
14use serde::Serialize;
15
16/// A position in the source text. Lines and columns are 1-based and count
17/// characters, not bytes.
18#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize)]
19pub struct SourceSpan {
20    pub line: u32,
21    pub column: u32,
22    /// Length of the node in characters (at least 1). For multi-line nodes
23    /// this runs past the end of the first line.
24    pub len: u32,
25}
26
27/// One segment of a [`DocPath`].
28#[derive(Clone, Debug, PartialEq, Eq, Hash)]
29pub enum PathSegment {
30    Key(String),
31    Index(usize),
32}
33
34/// Path of a value inside a workflow document, e.g. `graph.nodes[1].template`.
35#[derive(Clone, Debug, Default, PartialEq, Eq, Hash)]
36pub struct DocPath(Vec<PathSegment>);
37
38impl DocPath {
39    /// The document root.
40    pub fn root() -> Self {
41        Self::default()
42    }
43
44    /// This path extended by an object key.
45    #[must_use]
46    pub fn key(&self, key: &str) -> Self {
47        let mut segments = self.0.clone();
48        segments.push(PathSegment::Key(key.to_owned()));
49        Self(segments)
50    }
51
52    /// This path extended by an array index.
53    #[must_use]
54    pub fn index(&self, index: usize) -> Self {
55        let mut segments = self.0.clone();
56        segments.push(PathSegment::Index(index));
57        Self(segments)
58    }
59
60    /// The enclosing path, `None` at the root.
61    pub fn parent(&self) -> Option<Self> {
62        let (_, rest) = self.0.split_last()?;
63        Some(Self(rest.to_vec()))
64    }
65
66    /// The segments, outermost first.
67    pub fn segments(&self) -> &[PathSegment] {
68        &self.0
69    }
70
71    /// The last key, if the path ends in a key.
72    pub fn last_key(&self) -> Option<&str> {
73        match self.0.last() {
74            Some(PathSegment::Key(key)) => Some(key),
75            _ => None,
76        }
77    }
78
79    pub fn is_root(&self) -> bool {
80        self.0.is_empty()
81    }
82}
83
84impl fmt::Display for DocPath {
85    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
86        if self.0.is_empty() {
87            return f.write_str("<document>");
88        }
89        for (i, segment) in self.0.iter().enumerate() {
90            match segment {
91                PathSegment::Index(ix) => write!(f, "[{ix}]")?,
92                PathSegment::Key(key) if is_plain_key(key) => {
93                    if i > 0 {
94                        f.write_str(".")?;
95                    }
96                    f.write_str(key)?;
97                }
98                PathSegment::Key(key) => write!(f, "[{key:?}]")?,
99            }
100        }
101        Ok(())
102    }
103}
104
105impl Serialize for DocPath {
106    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
107        serializer.collect_str(self)
108    }
109}
110
111fn is_plain_key(key: &str) -> bool {
112    !key.is_empty() && key.chars().all(|c| c.is_ascii_alphanumeric() || c == '_' || c == '-')
113}
114
115/// Positions of keys and values, by document path.
116#[derive(Clone, Debug, Default)]
117pub struct SourceMap {
118    /// Position of the value at a path.
119    values: HashMap<DocPath, SourceSpan>,
120    /// Position of the mapping key introducing a path (object members only).
121    keys: HashMap<DocPath, SourceSpan>,
122}
123
124impl SourceMap {
125    /// An empty map (no positions known).
126    pub fn empty() -> Self {
127        Self::default()
128    }
129
130    /// Positions for a JSON or YAML document. Empty when the text cannot be
131    /// parsed or the `yaml` feature is disabled.
132    pub fn from_text(text: &str) -> Self {
133        #[cfg(feature = "yaml")]
134        {
135            spanned::parse(text).unwrap_or_default()
136        }
137        #[cfg(not(feature = "yaml"))]
138        {
139            let _ = text;
140            Self::default()
141        }
142    }
143
144    /// Position of the value at `path`.
145    pub fn value(&self, path: &DocPath) -> Option<SourceSpan> {
146        self.values.get(path).copied()
147    }
148
149    /// Position of the key that introduces `path` (for object members).
150    pub fn key(&self, path: &DocPath) -> Option<SourceSpan> {
151        self.keys.get(path).copied()
152    }
153
154    /// Best position for a diagnostic about `path`: the key (when `prefer_key`)
155    /// or the value at the path, else the closest enclosing value that has one.
156    pub fn locate(&self, path: &DocPath, prefer_key: bool) -> Option<SourceSpan> {
157        if prefer_key && let Some(span) = self.key(path) {
158            return Some(span);
159        }
160        let mut current = Some(path.clone());
161        while let Some(p) = current {
162            if let Some(span) = self.value(&p).or_else(|| self.key(&p)) {
163                return Some(span);
164            }
165            current = p.parent();
166        }
167        None
168    }
169
170    pub fn is_empty(&self) -> bool {
171        self.values.is_empty()
172    }
173}
174
175#[cfg(feature = "yaml")]
176mod spanned {
177    //! A span-aware parse with `serde_saphyr::Spanned`. JSON is parsed by the
178    //! same parser (JSON documents are YAML 1.2 flow documents).
179
180    use std::fmt;
181
182    use serde::de::{self, Deserialize, Deserializer, MapAccess, SeqAccess, Visitor};
183    use serde_saphyr::{Location, Spanned};
184
185    use super::{DocPath, SourceMap, SourceSpan};
186
187    /// A document tree that keeps the location of every node.
188    enum Tree {
189        Scalar,
190        Seq(Vec<Spanned<Tree>>),
191        Map(Vec<(Spanned<Key>, Spanned<Tree>)>),
192    }
193
194    /// A mapping key rendered as text.
195    struct Key(String);
196
197    pub(super) fn parse(text: &str) -> Option<SourceMap> {
198        let options = serde_saphyr::options! { strict_booleans: true };
199        let tree: Spanned<Tree> = serde_saphyr::from_str_with_options(text, options).ok()?;
200        let mut map = SourceMap::default();
201        record(&tree, &DocPath::root(), &mut map);
202        Some(map)
203    }
204
205    fn span(location: &Location) -> Option<SourceSpan> {
206        let line = u32::try_from(location.line()).ok().filter(|l| *l > 0)?;
207        let column = u32::try_from(location.column()).ok().filter(|c| *c > 0)?;
208        let len = u32::try_from(location.span().len()).unwrap_or(u32::MAX).max(1);
209        Some(SourceSpan { line, column, len })
210    }
211
212    fn record(node: &Spanned<Tree>, path: &DocPath, map: &mut SourceMap) {
213        if let Some(s) = span(&node.referenced) {
214            map.values.insert(path.clone(), s);
215        }
216        match &node.value {
217            Tree::Scalar => {}
218            Tree::Seq(items) => {
219                for (ix, item) in items.iter().enumerate() {
220                    record(item, &path.index(ix), map);
221                }
222            }
223            Tree::Map(entries) => {
224                for (key, value) in entries {
225                    let child = path.key(&key.value.0);
226                    if let Some(s) = span(&key.referenced) {
227                        map.keys.insert(child.clone(), s);
228                    }
229                    record(value, &child, map);
230                }
231            }
232        }
233    }
234
235    struct TreeVisitor;
236
237    impl<'de> Visitor<'de> for TreeVisitor {
238        type Value = Tree;
239
240        fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
241            f.write_str("any YAML value")
242        }
243
244        fn visit_bool<E: de::Error>(self, _: bool) -> Result<Tree, E> {
245            Ok(Tree::Scalar)
246        }
247        fn visit_i64<E: de::Error>(self, _: i64) -> Result<Tree, E> {
248            Ok(Tree::Scalar)
249        }
250        fn visit_i128<E: de::Error>(self, _: i128) -> Result<Tree, E> {
251            Ok(Tree::Scalar)
252        }
253        fn visit_u64<E: de::Error>(self, _: u64) -> Result<Tree, E> {
254            Ok(Tree::Scalar)
255        }
256        fn visit_u128<E: de::Error>(self, _: u128) -> Result<Tree, E> {
257            Ok(Tree::Scalar)
258        }
259        fn visit_f64<E: de::Error>(self, _: f64) -> Result<Tree, E> {
260            Ok(Tree::Scalar)
261        }
262        fn visit_str<E: de::Error>(self, _: &str) -> Result<Tree, E> {
263            Ok(Tree::Scalar)
264        }
265        fn visit_bytes<E: de::Error>(self, _: &[u8]) -> Result<Tree, E> {
266            Ok(Tree::Scalar)
267        }
268        fn visit_unit<E: de::Error>(self) -> Result<Tree, E> {
269            Ok(Tree::Scalar)
270        }
271        fn visit_none<E: de::Error>(self) -> Result<Tree, E> {
272            Ok(Tree::Scalar)
273        }
274        fn visit_some<D: Deserializer<'de>>(self, d: D) -> Result<Tree, D::Error> {
275            Tree::deserialize(d)
276        }
277        fn visit_seq<A: SeqAccess<'de>>(self, mut seq: A) -> Result<Tree, A::Error> {
278            let mut items = Vec::new();
279            while let Some(item) = seq.next_element::<Spanned<Tree>>()? {
280                items.push(item);
281            }
282            Ok(Tree::Seq(items))
283        }
284        fn visit_map<A: MapAccess<'de>>(self, mut access: A) -> Result<Tree, A::Error> {
285            let mut entries = Vec::new();
286            while let Some(key) = access.next_key::<Spanned<Key>>()? {
287                let value = access.next_value::<Spanned<Tree>>()?;
288                entries.push((key, value));
289            }
290            Ok(Tree::Map(entries))
291        }
292    }
293
294    impl<'de> Deserialize<'de> for Tree {
295        fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
296            d.deserialize_any(TreeVisitor)
297        }
298    }
299
300    struct KeyVisitor;
301
302    impl Visitor<'_> for KeyVisitor {
303        type Value = Key;
304
305        fn expecting(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
306            f.write_str("a scalar mapping key")
307        }
308        fn visit_str<E: de::Error>(self, v: &str) -> Result<Key, E> {
309            Ok(Key(v.to_owned()))
310        }
311        fn visit_bool<E: de::Error>(self, v: bool) -> Result<Key, E> {
312            Ok(Key(v.to_string()))
313        }
314        fn visit_i64<E: de::Error>(self, v: i64) -> Result<Key, E> {
315            Ok(Key(v.to_string()))
316        }
317        fn visit_u64<E: de::Error>(self, v: u64) -> Result<Key, E> {
318            Ok(Key(v.to_string()))
319        }
320        fn visit_f64<E: de::Error>(self, v: f64) -> Result<Key, E> {
321            Ok(Key(v.to_string()))
322        }
323        fn visit_unit<E: de::Error>(self) -> Result<Key, E> {
324            Ok(Key("null".to_owned()))
325        }
326    }
327
328    impl<'de> Deserialize<'de> for Key {
329        fn deserialize<D: Deserializer<'de>>(d: D) -> Result<Self, D::Error> {
330            d.deserialize_any(KeyVisitor)
331        }
332    }
333}
334
335#[cfg(all(test, feature = "yaml"))]
336mod tests {
337    use super::*;
338
339    #[test]
340    fn records_key_and_value_positions() {
341        let text = "graph:\n  nodes:\n    - id: a\n      templat: hi\n";
342        let map = SourceMap::from_text(text);
343        let path = DocPath::root().key("graph").key("nodes").index(0).key("templat");
344        assert_eq!(map.key(&path), Some(SourceSpan { line: 4, column: 7, len: 7 }));
345        assert_eq!(map.value(&path).map(|s| (s.line, s.column)), Some((4, 16)));
346        assert_eq!(path.to_string(), "graph.nodes[0].templat");
347    }
348
349    #[test]
350    fn json_positions() {
351        let text = "{\"graph\": {\"nodes\": [\n  {\"id\": \"a\", \"type\": \"start\"}\n]}}";
352        let map = SourceMap::from_text(text);
353        let path = DocPath::root().key("graph").key("nodes").index(0).key("type");
354        assert_eq!(map.key(&path).map(|s| (s.line, s.column)), Some((2, 15)));
355    }
356
357    #[test]
358    fn locate_falls_back_to_ancestors() {
359        let map = SourceMap::from_text("graph:\n  nodes: []\n");
360        let path = DocPath::root().key("graph").key("nodes").index(3).key("x");
361        assert_eq!(map.locate(&path, true).map(|s| s.line), Some(2));
362        assert_eq!(DocPath::root().key("a b").index(1).to_string(), "[\"a b\"][1]");
363    }
364}