From 7f9d96077b2cfd5fc30377304958be081c9e7b7f Mon Sep 17 00:00:00 2001 From: Igor Pashev Date: Thu, 11 Jul 2024 11:01:10 +0200 Subject: [PATCH] refactor: make repr more public To create keys in custom parsers. Signed-off-by: Igor Pashev --- crates/toml_edit/src/encode.rs | 10 +++++----- crates/toml_edit/src/key.rs | 7 +++++-- crates/toml_edit/src/parser/key.rs | 2 +- crates/toml_edit/src/parser/mod.rs | 2 +- crates/toml_edit/src/parser/value.rs | 10 +++++----- crates/toml_edit/src/repr.rs | 5 ++++- 6 files changed, 21 insertions(+), 15 deletions(-) diff --git a/crates/toml_edit/src/encode.rs b/crates/toml_edit/src/encode.rs index b6142a6c..b475226d 100644 --- a/crates/toml_edit/src/encode.rs +++ b/crates/toml_edit/src/encode.rs @@ -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)] @@ -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()) } } @@ -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()) } } diff --git a/crates/toml_edit/src/key.rs b/crates/toml_edit/src/key.rs index 15fdd4de..fb6e5972 100644 --- a/crates/toml_edit/src/key.rs +++ b/crates/toml_edit/src/key.rs @@ -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 } @@ -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, diff --git a/crates/toml_edit/src/parser/key.rs b/crates/toml_edit/src/parser/key.rs index ee729fda..8a4c976c 100644 --- a/crates/toml_edit/src/parser/key.rs +++ b/crates/toml_edit/src/parser/key.rs @@ -24,7 +24,7 @@ pub(crate) fn key(input: &mut Input<'_>) -> PResult> { 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), diff --git a/crates/toml_edit/src/parser/mod.rs b/crates/toml_edit/src/parser/mod.rs index 4c64dfcd..e3449b99 100644 --- a/crates/toml_edit/src/parser/mod.rs +++ b/crates/toml_edit/src/parser/mod.rs @@ -39,7 +39,7 @@ pub(crate) fn parse_key(raw: &str) -> Result { 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)), } diff --git a/crates/toml_edit/src/parser/value.rs b/crates/toml_edit/src/parser/value.rs index 776ab4f3..19009ac2 100644 --- a/crates/toml_edit/src/parser/value.rs +++ b/crates/toml_edit/src/parser/value.rs @@ -90,23 +90,23 @@ fn apply_raw(mut val: Value, span: std::ops::Range) -> Result { 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); diff --git a/crates/toml_edit/src/repr.rs b/crates/toml_edit/src/repr.rs index 80d78c38..8454683d 100644 --- a/crates/toml_edit/src/repr.rs +++ b/crates/toml_edit/src/repr.rs @@ -141,7 +141,10 @@ pub struct Repr { } impl Repr { - pub(crate) fn new_unchecked(raw: impl Into) -> Self { + /// Create a new encoded value + /// + /// Useful in custom parsers. + pub fn new(raw: impl Into) -> Self { Repr { raw_value: raw.into(), }