hcl_edit/
raw_string.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
use crate::encode::EncodeState;
use hcl_primitives::InternalString;
use std::borrow::Cow;
use std::fmt::Write;
use std::ops::{self, Range};

/// Opaque string storage for raw HCL.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct RawString(RawStringInner);

#[derive(Debug, Clone, PartialEq, Eq)]
enum RawStringInner {
    Empty,
    Spanned(Range<usize>),
    Explicit(InternalString),
}

impl RawString {
    pub(crate) fn from_span(span: Range<usize>) -> Self {
        if span.is_empty() {
            RawString(RawStringInner::Empty)
        } else {
            RawString(RawStringInner::Spanned(span))
        }
    }

    pub(crate) fn span(&self) -> Option<Range<usize>> {
        match &self.0 {
            RawStringInner::Empty | RawStringInner::Explicit(_) => None,
            RawStringInner::Spanned(span) => Some(span.clone()),
        }
    }

    /// Returns the `RawString` as a `&str`.
    pub(crate) fn as_str(&self) -> &str {
        match &self.0 {
            RawStringInner::Empty | RawStringInner::Spanned(_) => "",
            RawStringInner::Explicit(s) => s.as_str(),
        }
    }

    pub(crate) fn encode_with_default(
        &self,
        buf: &mut EncodeState,
        default: &str,
    ) -> std::fmt::Result {
        if let RawStringInner::Spanned(_) = self.0 {
            buf.write_str(default)
        } else {
            buf.write_str(self.as_str())
        }
    }

    pub(crate) fn despan(&mut self, input: &str) {
        match &self.0 {
            RawStringInner::Empty | RawStringInner::Explicit(_) => {}
            RawStringInner::Spanned(span) => {
                *self = RawString::from(input.get(span.clone()).unwrap_or_else(|| {
                    panic!("span {span:?} should be in input:\n```\n{input}\n```")
                }));
            }
        }
    }
}

impl Default for RawString {
    fn default() -> Self {
        RawString(RawStringInner::Empty)
    }
}

impl ops::Deref for RawString {
    type Target = str;

    #[inline]
    fn deref(&self) -> &Self::Target {
        self.as_str()
    }
}

impl From<&str> for RawString {
    #[inline]
    fn from(s: &str) -> Self {
        if s.is_empty() {
            RawString(RawStringInner::Empty)
        } else {
            RawString::from(InternalString::from(s))
        }
    }
}

impl<'a> From<Cow<'a, str>> for RawString {
    #[inline]
    fn from(s: Cow<'a, str>) -> Self {
        if s.is_empty() {
            RawString(RawStringInner::Empty)
        } else {
            RawString::from(InternalString::from(s))
        }
    }
}

impl From<String> for RawString {
    #[inline]
    fn from(s: String) -> Self {
        if s.is_empty() {
            RawString(RawStringInner::Empty)
        } else {
            RawString::from(InternalString::from(s))
        }
    }
}

impl From<InternalString> for RawString {
    #[inline]
    fn from(inner: InternalString) -> Self {
        RawString(RawStringInner::Explicit(inner))
    }
}

impl<'a> From<RawString> for Cow<'a, str> {
    #[inline]
    fn from(s: RawString) -> Self {
        match s.0 {
            RawStringInner::Empty | RawStringInner::Spanned(_) => Cow::Borrowed(""),
            RawStringInner::Explicit(s) => s.into(),
        }
    }
}

impl<'a> From<&'a RawString> for Cow<'a, str> {
    #[inline]
    fn from(s: &'a RawString) -> Self {
        match &s.0 {
            RawStringInner::Empty | RawStringInner::Spanned(_) => Cow::Borrowed(""),
            RawStringInner::Explicit(s) => s.into(),
        }
    }
}