toml_parser/decoder/
mod.rs

1//! Decode [raw][crate::Raw] TOML values into Rust native types
2//!
3//! See
4//! - [`Raw::decode_key`][crate::Raw::decode_key]
5//! - [`Raw::decode_scalar`][crate::Raw::decode_scalar]
6//! - [`Raw::decode_whitespace`][crate::Raw::decode_whitespace]
7//! - [`Raw::decode_comment`][crate::Raw::decode_comment]
8//! - [`Raw::decode_newline`][crate::Raw::decode_newline]
9
10#[cfg(feature = "alloc")]
11use alloc::borrow::Cow;
12#[cfg(feature = "alloc")]
13use alloc::string::String;
14
15pub(crate) mod scalar;
16pub(crate) mod string;
17pub(crate) mod ws;
18
19pub use scalar::IntegerRadix;
20pub use scalar::ScalarKind;
21
22#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash, Debug)]
23#[repr(u8)]
24pub enum Encoding {
25    LiteralString = crate::lexer::APOSTROPHE,
26    BasicString = crate::lexer::QUOTATION_MARK,
27    MlLiteralString = 1,
28    MlBasicString,
29}
30
31impl Encoding {
32    pub const fn description(&self) -> &'static str {
33        match self {
34            Self::LiteralString => crate::lexer::TokenKind::LiteralString.description(),
35            Self::BasicString => crate::lexer::TokenKind::BasicString.description(),
36            Self::MlLiteralString => crate::lexer::TokenKind::MlLiteralString.description(),
37            Self::MlBasicString => crate::lexer::TokenKind::MlBasicString.description(),
38        }
39    }
40}
41
42pub trait StringBuilder<'s> {
43    fn clear(&mut self);
44    #[must_use]
45    fn push_str(&mut self, append: &'s str) -> bool;
46    #[must_use]
47    fn push_char(&mut self, append: char) -> bool;
48}
49
50impl<'s> StringBuilder<'s> for () {
51    fn clear(&mut self) {}
52    fn push_str(&mut self, _append: &'s str) -> bool {
53        true
54    }
55    fn push_char(&mut self, _append: char) -> bool {
56        true
57    }
58}
59
60impl<'s> StringBuilder<'s> for &'s str {
61    fn clear(&mut self) {
62        *self = &self[0..0];
63    }
64    fn push_str(&mut self, append: &'s str) -> bool {
65        if self.is_empty() {
66            *self = append;
67            true
68        } else {
69            false
70        }
71    }
72    fn push_char(&mut self, _append: char) -> bool {
73        false
74    }
75}
76
77#[cfg(feature = "alloc")]
78impl<'s> StringBuilder<'s> for Cow<'s, str> {
79    fn clear(&mut self) {
80        match self {
81            Cow::Borrowed(s) => {
82                s.clear();
83            }
84            Cow::Owned(s) => s.clear(),
85        }
86    }
87    fn push_str(&mut self, append: &'s str) -> bool {
88        match self {
89            Cow::Borrowed(s) => {
90                if !s.push_str(append) {
91                    self.to_mut().push_str(append);
92                }
93            }
94            Cow::Owned(s) => s.push_str(append),
95        }
96        true
97    }
98    fn push_char(&mut self, append: char) -> bool {
99        self.to_mut().push(append);
100        true
101    }
102}
103
104#[cfg(feature = "alloc")]
105impl<'s> StringBuilder<'s> for String {
106    fn clear(&mut self) {
107        self.clear();
108    }
109    fn push_str(&mut self, append: &'s str) -> bool {
110        self.push_str(append);
111        true
112    }
113    fn push_char(&mut self, append: char) -> bool {
114        self.push(append);
115        true
116    }
117}