sql_parse/
sstring.rs

1// Licensed under the Apache License, Version 2.0 (the "License");
2// you may not use this file except in compliance with the License.
3// You may obtain a copy of the License at
4//
5// http://www.apache.org/licenses/LICENSE-2.0
6//
7// Unless required by applicable law or agreed to in writing, software
8// distributed under the License is distributed on an "AS IS" BASIS,
9// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
10// See the License for the specific language governing permissions and
11// limitations under the License.
12
13use alloc::borrow::Cow;
14
15use crate::{Span, Spanned};
16
17/// A string with attached span
18#[derive(Clone, Debug)]
19pub struct SString<'a> {
20    /// The underlying string
21    pub value: Cow<'a, str>,
22    /// The span the string originated from
23    pub span: Span,
24}
25
26impl<'a> SString<'a> {
27    /// Construct new SString with given value an span
28    pub fn new(value: Cow<'a, str>, span: Span) -> Self {
29        Self { value, span }
30    }
31
32    /// Return the str value
33    pub fn as_str(&self) -> &str {
34        self.value.as_ref()
35    }
36}
37
38impl<'a> core::ops::Deref for SString<'a> {
39    type Target = str;
40
41    fn deref(&self) -> &Self::Target {
42        self.value.deref()
43    }
44}
45
46impl<'a> Spanned for SString<'a> {
47    fn span(&self) -> Span {
48        self.span.span()
49    }
50}