Skip to content

Commit 9b58643

Browse files
committed
move sats::de::serde::NoneAccess to sats::de::NoneAccess
1 parent c3cfadb commit 9b58643

2 files changed

Lines changed: 37 additions & 24 deletions

File tree

crates/sats/src/de.rs

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -735,3 +735,39 @@ fn one_of_names(names: impl Fn(&mut dyn ValidNames)) -> Option<impl fmt::Display
735735
// There was at least one name; render those names.
736736
(count != 0).then(|| fmt_fn(move |fmt| OneOfNames::new(count, fmt).run(&names).f.map(drop)))
737737
}
738+
739+
/// Deserializes `none` variant of an optional value.
740+
pub struct NoneAccess<E>(PhantomData<E>);
741+
742+
impl<E: Error> NoneAccess<E> {
743+
/// Returns a new [`NoneAccess`].
744+
pub fn new() -> Self {
745+
Self(PhantomData)
746+
}
747+
}
748+
749+
impl<E: Error> Default for NoneAccess<E> {
750+
fn default() -> Self {
751+
Self::new()
752+
}
753+
}
754+
755+
impl<E: Error> SumAccess<'_> for NoneAccess<E> {
756+
type Error = E;
757+
type Variant = Self;
758+
759+
fn variant<V: VariantVisitor>(self, visitor: V) -> Result<(V::Output, Self::Variant), Self::Error> {
760+
visitor.visit_name("none").map(|var| (var, self))
761+
}
762+
}
763+
impl<'de, E: Error> VariantAccess<'de> for NoneAccess<E> {
764+
type Error = E;
765+
fn deserialize_seed<T: DeserializeSeed<'de>>(self, seed: T) -> Result<T::Output, Self::Error> {
766+
use crate::algebraic_value::de::*;
767+
seed.deserialize(ValueDeserializer::new(crate::AlgebraicValue::unit()))
768+
.map_err(|err| match err {
769+
ValueDeserializeError::MismatchedType => E::custom("mismatched type"),
770+
ValueDeserializeError::Custom(err) => E::custom(err),
771+
})
772+
}
773+
}

crates/sats/src/de/serde.rs

Lines changed: 1 addition & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use super::Deserializer;
22
use crate::serde::{SerdeError, SerdeWrapper};
33
use crate::{i256, u256};
44
use core::fmt;
5-
use core::marker::PhantomData;
65
use serde::de as serde;
76

87
/// Converts any [`serde::Deserializer`] to a SATS [`Deserializer`]
@@ -235,28 +234,6 @@ impl<'de, A: serde::SeqAccess<'de>> super::SeqProductAccess<'de> for SeqTupleAcc
235234
}
236235
}
237236

238-
/// Deserializes `none` variant of an optional value.
239-
struct NoneAccess<E>(PhantomData<E>);
240-
impl<E: super::Error> super::SumAccess<'_> for NoneAccess<E> {
241-
type Error = E;
242-
type Variant = Self;
243-
244-
fn variant<V: super::VariantVisitor>(self, visitor: V) -> Result<(V::Output, Self::Variant), Self::Error> {
245-
visitor.visit_name("none").map(|var| (var, self))
246-
}
247-
}
248-
impl<'de, E: super::Error> super::VariantAccess<'de> for NoneAccess<E> {
249-
type Error = E;
250-
fn deserialize_seed<T: super::DeserializeSeed<'de>>(self, seed: T) -> Result<T::Output, Self::Error> {
251-
use crate::algebraic_value::de::*;
252-
seed.deserialize(ValueDeserializer::new(crate::AlgebraicValue::unit()))
253-
.map_err(|err| match err {
254-
ValueDeserializeError::MismatchedType => E::custom("mismatched type"),
255-
ValueDeserializeError::Custom(err) => E::custom(err),
256-
})
257-
}
258-
}
259-
260237
/// Converts a SATS `SumVisitor` to `serde::Visitor`.
261238
struct EnumVisitor<V> {
262239
/// The `SumVisitor`.
@@ -289,7 +266,7 @@ impl<'de, V: super::SumVisitor<'de>> serde::Visitor<'de> for EnumVisitor<V> {
289266

290267
fn visit_unit<E: serde::Error>(self) -> Result<Self::Value, E> {
291268
if self.visitor.is_option() {
292-
self.visitor.visit_sum(NoneAccess(PhantomData)).map_err(unwrap_error)
269+
self.visitor.visit_sum(super::NoneAccess::new()).map_err(unwrap_error)
293270
} else {
294271
Err(E::invalid_type(serde::Unexpected::Unit, &self))
295272
}

0 commit comments

Comments
 (0)