Skip to content

Commit

Permalink
refactor: make repr more public
Browse files Browse the repository at this point in the history
To create keys in custom parsers.

Signed-off-by: Igor Pashev <pashev.igor@gmail.com>
  • Loading branch information
ip1981 committed Jul 14, 2024
1 parent c766756 commit 7f9d960
Show file tree
Hide file tree
Showing 6 changed files with 21 additions and 15 deletions.
10 changes: 5 additions & 5 deletions crates/toml_edit/src/encode.rs
Original file line number Diff line number Diff line change
Expand Up @@ -368,7 +368,7 @@ pub(crate) fn to_string_repr(
output.push_str(style.standard_end());
}

Repr::new_unchecked(output)
Repr::new(output)
}

#[derive(Copy, Clone, Debug, PartialEq, Eq)]
Expand Down Expand Up @@ -489,7 +489,7 @@ fn infer_style(value: &str) -> (StringStyle, bool) {

impl ValueRepr for i64 {
fn to_repr(&self) -> Repr {
Repr::new_unchecked(self.to_string())
Repr::new(self.to_string())
}
}

Expand All @@ -513,17 +513,17 @@ fn to_f64_repr(f: f64) -> Repr {
}
}
};
Repr::new_unchecked(repr)
Repr::new(repr)
}

impl ValueRepr for bool {
fn to_repr(&self) -> Repr {
Repr::new_unchecked(self.to_string())
Repr::new(self.to_string())
}
}

impl ValueRepr for Datetime {
fn to_repr(&self) -> Repr {
Repr::new_unchecked(self.to_string())
Repr::new(self.to_string())
}
}
7 changes: 5 additions & 2 deletions crates/toml_edit/src/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,10 @@ impl Key {
Self::try_parse_path(repr)
}

pub(crate) fn with_repr_unchecked(mut self, repr: Repr) -> Self {
/// While creating the `Key`, add `Repr` to it
///
/// Useful in custom parsers.
pub fn with_repr(mut self, repr: Repr) -> Self {
self.repr = Some(repr);
self
}
Expand Down Expand Up @@ -284,7 +287,7 @@ fn to_key_repr(key: &str) -> Repr {
.all(crate::parser::key::is_unquoted_char)
&& !key.is_empty()
{
Repr::new_unchecked(key)
Repr::new(key)
} else {
crate::encode::to_string_repr(
key,
Expand Down
2 changes: 1 addition & 1 deletion crates/toml_edit/src/parser/key.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ pub(crate) fn key(input: &mut Input<'_>) -> PResult<Vec<Key>> {
1..,
(ws.span(), simple_key, ws.span()).map(|(pre, (raw, key), suffix)| {
Key::new(key)
.with_repr_unchecked(Repr::new_unchecked(raw))
.with_repr(Repr::new(raw))
.with_dotted_decor(Decor::new(
RawString::with_span(pre),
RawString::with_span(suffix),
Expand Down
2 changes: 1 addition & 1 deletion crates/toml_edit/src/parser/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub(crate) fn parse_key(raw: &str) -> Result<crate::Key, TomlError> {
let result = key::simple_key.parse(b);
match result {
Ok((raw, key)) => {
Ok(crate::Key::new(key).with_repr_unchecked(crate::Repr::new_unchecked(raw)))
Ok(crate::Key::new(key).with_repr(crate::Repr::new(raw)))
}
Err(e) => Err(TomlError::new(e, b)),
}
Expand Down
10 changes: 5 additions & 5 deletions crates/toml_edit/src/parser/value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,23 +90,23 @@ fn apply_raw(mut val: Value, span: std::ops::Range<usize>) -> Result<Value, std:
match val {
Value::String(ref mut f) => {
let raw = RawString::with_span(span);
f.set_repr_unchecked(Repr::new_unchecked(raw));
f.set_repr_unchecked(Repr::new(raw));
}
Value::Integer(ref mut f) => {
let raw = RawString::with_span(span);
f.set_repr_unchecked(Repr::new_unchecked(raw));
f.set_repr_unchecked(Repr::new(raw));
}
Value::Float(ref mut f) => {
let raw = RawString::with_span(span);
f.set_repr_unchecked(Repr::new_unchecked(raw));
f.set_repr_unchecked(Repr::new(raw));
}
Value::Boolean(ref mut f) => {
let raw = RawString::with_span(span);
f.set_repr_unchecked(Repr::new_unchecked(raw));
f.set_repr_unchecked(Repr::new(raw));
}
Value::Datetime(ref mut f) => {
let raw = RawString::with_span(span);
f.set_repr_unchecked(Repr::new_unchecked(raw));
f.set_repr_unchecked(Repr::new(raw));
}
Value::Array(ref mut arr) => {
arr.span = Some(span);
Expand Down
5 changes: 4 additions & 1 deletion crates/toml_edit/src/repr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -141,7 +141,10 @@ pub struct Repr {
}

impl Repr {
pub(crate) fn new_unchecked(raw: impl Into<RawString>) -> Self {
/// Create a new encoded value
///
/// Useful in custom parsers.
pub fn new(raw: impl Into<RawString>) -> Self {
Repr {
raw_value: raw.into(),
}
Expand Down

0 comments on commit 7f9d960

Please sign in to comment.