From 73388e18fc89d4f38668741af4d35f329b2f7b46 Mon Sep 17 00:00:00 2001 From: Ed Page Date: Tue, 24 Oct 2023 13:43:38 -0500 Subject: [PATCH] feat(spanned): Expose SpannedDeserializer Fixes #634 --- crates/serde_spanned/src/de.rs | 81 +++++++++++++++++++++++++++++ crates/serde_spanned/src/lib.rs | 2 + crates/serde_spanned/src/spanned.rs | 11 ++++ crates/toml_edit/src/de/array.rs | 4 +- crates/toml_edit/src/de/key.rs | 7 ++- crates/toml_edit/src/de/spanned.rs | 70 ------------------------- crates/toml_edit/src/de/table.rs | 4 +- crates/toml_edit/src/de/value.rs | 4 +- 8 files changed, 105 insertions(+), 78 deletions(-) create mode 100644 crates/serde_spanned/src/de.rs delete mode 100644 crates/toml_edit/src/de/spanned.rs diff --git a/crates/serde_spanned/src/de.rs b/crates/serde_spanned/src/de.rs new file mode 100644 index 00000000..aea2f109 --- /dev/null +++ b/crates/serde_spanned/src/de.rs @@ -0,0 +1,81 @@ +//! Deserialzation support for [`Spanned`] + +use serde::de::value::BorrowedStrDeserializer; +use serde::de::IntoDeserializer as _; + +use crate::Spanned; + +/// Deserializer / format support for emitting [`Spanned`] +pub struct SpannedDeserializer<'de, T, E> +where + T: serde::de::IntoDeserializer<'de, E>, + E: serde::de::Error, +{ + start: Option, + end: Option, + value: Option, + _lifetime: std::marker::PhantomData<&'de ()>, + _error: std::marker::PhantomData, +} + +impl<'de, T, E> SpannedDeserializer<'de, T, E> +where + T: serde::de::IntoDeserializer<'de, E>, + E: serde::de::Error, +{ + /// Create a deserializer to emit [`Spanned`] + pub fn new(value: T, span: std::ops::Range) -> Self { + Self { + start: Some(span.start), + end: Some(span.end), + value: Some(value), + _lifetime: Default::default(), + _error: Default::default(), + } + } + + /// Check if deserializing a [`Spanned`] + pub fn is_spanned(name: &'static str, fields: &'static [&'static str]) -> bool { + Spanned::is_spanned(name, fields) + } +} + +impl<'de, T, E> serde::de::MapAccess<'de> for SpannedDeserializer<'de, T, E> +where + T: serde::de::IntoDeserializer<'de, E>, + E: serde::de::Error, +{ + type Error = E; + fn next_key_seed(&mut self, seed: K) -> Result, Self::Error> + where + K: serde::de::DeserializeSeed<'de>, + { + if self.start.is_some() { + seed.deserialize(BorrowedStrDeserializer::new(Spanned::START_FIELD)) + .map(Some) + } else if self.end.is_some() { + seed.deserialize(BorrowedStrDeserializer::new(Spanned::END_FIELD)) + .map(Some) + } else if self.value.is_some() { + seed.deserialize(BorrowedStrDeserializer::new(Spanned::VALUE_FIELD)) + .map(Some) + } else { + Ok(None) + } + } + + fn next_value_seed(&mut self, seed: V) -> Result + where + V: serde::de::DeserializeSeed<'de>, + { + if let Some(start) = self.start.take() { + seed.deserialize(start.into_deserializer()) + } else if let Some(end) = self.end.take() { + seed.deserialize(end.into_deserializer()) + } else if let Some(value) = self.value.take() { + seed.deserialize(value.into_deserializer()) + } else { + panic!("next_value_seed called before next_key_seed") + } + } +} diff --git a/crates/serde_spanned/src/lib.rs b/crates/serde_spanned/src/lib.rs index 8ff723f4..3b0adbac 100644 --- a/crates/serde_spanned/src/lib.rs +++ b/crates/serde_spanned/src/lib.rs @@ -16,6 +16,8 @@ mod spanned; pub use crate::spanned::Spanned; +#[cfg(feature = "serde")] +pub mod de; #[doc(hidden)] #[cfg(feature = "serde")] diff --git a/crates/serde_spanned/src/spanned.rs b/crates/serde_spanned/src/spanned.rs index ac351ca8..b0467243 100644 --- a/crates/serde_spanned/src/spanned.rs +++ b/crates/serde_spanned/src/spanned.rs @@ -56,6 +56,17 @@ impl Spanned { } } +#[cfg(feature = "serde")] +impl Spanned { + pub(crate) const NAME: &str = NAME; + pub(crate) const START_FIELD: &str = START_FIELD; + pub(crate) const END_FIELD: &str = END_FIELD; + pub(crate) const VALUE_FIELD: &str = VALUE_FIELD; + pub(crate) fn is_spanned(name: &'static str, fields: &'static [&'static str]) -> bool { + is_spanned(name, fields) + } +} + impl std::borrow::Borrow for Spanned { fn borrow(&self) -> &str { self.get_ref() diff --git a/crates/toml_edit/src/de/array.rs b/crates/toml_edit/src/de/array.rs index adc54016..d00dc547 100644 --- a/crates/toml_edit/src/de/array.rs +++ b/crates/toml_edit/src/de/array.rs @@ -32,9 +32,9 @@ impl<'de> serde::Deserializer<'de> for ArrayDeserializer { where V: serde::de::Visitor<'de>, { - if serde_spanned::__unstable::is_spanned(name, fields) { + if serde_spanned::SpannedDeserializer::is_spanned(name, fields) { if let Some(span) = self.span.clone() { - return visitor.visit_map(super::SpannedDeserializer::new(self, span)); + return visitor.visit_map(serde_spanned::SpannedDeserializer::new(self, span)); } } diff --git a/crates/toml_edit/src/de/key.rs b/crates/toml_edit/src/de/key.rs index a3b28256..49806346 100644 --- a/crates/toml_edit/src/de/key.rs +++ b/crates/toml_edit/src/de/key.rs @@ -54,9 +54,12 @@ impl<'de> serde::de::Deserializer<'de> for KeyDeserializer { where V: serde::de::Visitor<'de>, { - if serde_spanned::__unstable::is_spanned(name, fields) { + if serde_spanned::SpannedDeserializer::is_spanned(name, fields) { if let Some(span) = self.span.clone() { - return visitor.visit_map(super::SpannedDeserializer::new(self.key.as_str(), span)); + return visitor.visit_map(serde_spanned::SpannedDeserializer::new( + self.key.as_str(), + span, + )); } } self.deserialize_any(visitor) diff --git a/crates/toml_edit/src/de/spanned.rs b/crates/toml_edit/src/de/spanned.rs deleted file mode 100644 index 7ce58640..00000000 --- a/crates/toml_edit/src/de/spanned.rs +++ /dev/null @@ -1,70 +0,0 @@ -use serde::de::value::BorrowedStrDeserializer; -use serde::de::IntoDeserializer as _; - -use super::Error; - -pub(crate) struct SpannedDeserializer<'de, T: serde::de::IntoDeserializer<'de, Error>> { - phantom_data: std::marker::PhantomData<&'de ()>, - start: Option, - end: Option, - value: Option, -} - -impl<'de, T> SpannedDeserializer<'de, T> -where - T: serde::de::IntoDeserializer<'de, Error>, -{ - pub(crate) fn new(value: T, span: std::ops::Range) -> Self { - Self { - phantom_data: Default::default(), - start: Some(span.start), - end: Some(span.end), - value: Some(value), - } - } -} - -impl<'de, T> serde::de::MapAccess<'de> for SpannedDeserializer<'de, T> -where - T: serde::de::IntoDeserializer<'de, Error>, -{ - type Error = Error; - fn next_key_seed(&mut self, seed: K) -> Result, Error> - where - K: serde::de::DeserializeSeed<'de>, - { - if self.start.is_some() { - seed.deserialize(BorrowedStrDeserializer::new( - serde_spanned::__unstable::START_FIELD, - )) - .map(Some) - } else if self.end.is_some() { - seed.deserialize(BorrowedStrDeserializer::new( - serde_spanned::__unstable::END_FIELD, - )) - .map(Some) - } else if self.value.is_some() { - seed.deserialize(BorrowedStrDeserializer::new( - serde_spanned::__unstable::VALUE_FIELD, - )) - .map(Some) - } else { - Ok(None) - } - } - - fn next_value_seed(&mut self, seed: V) -> Result - where - V: serde::de::DeserializeSeed<'de>, - { - if let Some(start) = self.start.take() { - seed.deserialize(start.into_deserializer()) - } else if let Some(end) = self.end.take() { - seed.deserialize(end.into_deserializer()) - } else if let Some(value) = self.value.take() { - seed.deserialize(value.into_deserializer()) - } else { - panic!("next_value_seed called before next_key_seed") - } - } -} diff --git a/crates/toml_edit/src/de/table.rs b/crates/toml_edit/src/de/table.rs index 0b6183e0..37ebe6f9 100644 --- a/crates/toml_edit/src/de/table.rs +++ b/crates/toml_edit/src/de/table.rs @@ -48,9 +48,9 @@ impl<'de> serde::Deserializer<'de> for TableDeserializer { where V: serde::de::Visitor<'de>, { - if serde_spanned::__unstable::is_spanned(name, fields) { + if serde_spanned::SpannedDeserializer::is_spanned(name, fields) { if let Some(span) = self.span.clone() { - return visitor.visit_map(super::SpannedDeserializer::new(self, span)); + return visitor.visit_map(serde_spanned::SpannedDeserializer::new(self, span)); } } diff --git a/crates/toml_edit/src/de/value.rs b/crates/toml_edit/src/de/value.rs index d3cf87fc..64ba65ad 100644 --- a/crates/toml_edit/src/de/value.rs +++ b/crates/toml_edit/src/de/value.rs @@ -128,9 +128,9 @@ impl<'de> serde::Deserializer<'de> for ValueDeserializer { where V: serde::de::Visitor<'de>, { - if serde_spanned::__unstable::is_spanned(name, fields) { + if serde_spanned::SpannedDeserializer::is_spanned(name, fields) { if let Some(span) = self.input.span() { - return visitor.visit_map(super::SpannedDeserializer::new(self, span)); + return visitor.visit_map(serde_spanned::SpannedDeserializer::new(self, span)); } }