cedar_policy_core/parser/
loc.rs

1/*
2 * Copyright Cedar Contributors
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      https://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17use std::sync::Arc;
18
19/// Represents a source location: index/range, and a reference to the source
20/// code which that index/range indexes into
21#[derive(Debug, Clone, PartialEq, Eq, Hash, PartialOrd, Ord)]
22pub struct Loc {
23    /// `SourceSpan` indicating a specific source code location or range
24    pub span: miette::SourceSpan,
25
26    /// Original source code (which the above source span indexes into)
27    pub src: Arc<str>,
28}
29
30impl Loc {
31    /// Create a new `Loc`
32    pub fn new(span: impl Into<miette::SourceSpan>, src: Arc<str>) -> Self {
33        Self {
34            span: span.into(),
35            src,
36        }
37    }
38
39    /// Create a new `Loc` with the same source code but a different span
40    pub fn span(&self, span: impl Into<miette::SourceSpan>) -> Self {
41        Self {
42            span: span.into(),
43            src: Arc::clone(&self.src),
44        }
45    }
46
47    /// Get the index representing the start of the source span
48    pub fn start(&self) -> usize {
49        self.span.offset()
50    }
51
52    /// Get the index representing the end of the source span
53    pub fn end(&self) -> usize {
54        self.span.offset() + self.span.len()
55    }
56
57    /// Get the actual source snippet indicated, or `None` if the `Loc` isn't
58    /// internally consistent (its `SourceSpan` isn't a valid index into its
59    /// `src`)
60    pub fn snippet(&self) -> Option<&str> {
61        self.src.get(self.start()..self.end())
62    }
63}
64
65impl From<Loc> for miette::SourceSpan {
66    fn from(loc: Loc) -> Self {
67        loc.span
68    }
69}
70
71impl From<&Loc> for miette::SourceSpan {
72    fn from(loc: &Loc) -> Self {
73        loc.span
74    }
75}
76
77impl miette::SourceCode for Loc {
78    fn read_span<'a>(
79        &'a self,
80        span: &miette::SourceSpan,
81        context_lines_before: usize,
82        context_lines_after: usize,
83    ) -> Result<Box<dyn miette::SpanContents<'a> + 'a>, miette::MietteError> {
84        self.src
85            .read_span(span, context_lines_before, context_lines_after)
86    }
87}
88
89impl miette::SourceCode for &Loc {
90    fn read_span<'a>(
91        &'a self,
92        span: &miette::SourceSpan,
93        context_lines_before: usize,
94        context_lines_after: usize,
95    ) -> Result<Box<dyn miette::SpanContents<'a> + 'a>, miette::MietteError> {
96        self.src
97            .read_span(span, context_lines_before, context_lines_after)
98    }
99}