Skip to content

Commit

Permalink
Move the TypeName helper into serdes::utils
Browse files Browse the repository at this point in the history
  • Loading branch information
myrrlyn committed Sep 3, 2022
1 parent dd3aa64 commit 2e75d5e
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 50 deletions.
50 changes: 3 additions & 47 deletions src/serdes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,19 +5,14 @@ mod array;
mod slice;
mod utils;

use core::{
any,
fmt::{
self,
Formatter,
},
marker::PhantomData,
use core::fmt::{
self,
Formatter,
};

use serde::de::{
Deserialize,
Deserializer,
Unexpected,
Visitor,
};

Expand Down Expand Up @@ -71,45 +66,6 @@ impl<'de> Visitor<'de> for FieldVisitor {
}
}

/// A zero-sized type that deserializes from any string as long as it is equal
/// to `any::type_name::<T>()`.
struct TypeName<T>(PhantomData<T>);

impl<T> TypeName<T> {
/// Creates a type-name ghost for any type.
fn new() -> Self {
TypeName(PhantomData)
}
}

impl<'de, T> Deserialize<'de> for TypeName<T> {
fn deserialize<D>(deserializer: D) -> core::result::Result<Self, D::Error>
where D: Deserializer<'de> {
deserializer.deserialize_str(Self::new())
}
}

impl<'de, T> Visitor<'de> for TypeName<T> {
type Value = Self;

fn expecting(&self, fmt: &mut Formatter) -> fmt::Result {
write!(fmt, "the string {:?}", any::type_name::<T>())
}

fn visit_str<E>(self, value: &str) -> core::result::Result<Self::Value, E>
where E: serde::de::Error {
if value == any::type_name::<T>() {
Ok(self)
}
else {
Err(serde::de::Error::invalid_value(
Unexpected::Str(value),
&self,
))
}
}
}

#[cfg(test)]
mod tests {
use serde::{
Expand Down
6 changes: 4 additions & 2 deletions src/serdes/array.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,9 +26,11 @@ use serde::{
};

use super::{
utils::Array,
utils::{
Array,
TypeName,
},
Field,
TypeName,
FIELDS,
};
use crate::{
Expand Down
2 changes: 1 addition & 1 deletion src/serdes/slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ use serde::{
use wyz::comu::Const;

use super::{
utils::TypeName,
Field,
TypeName,
FIELDS,
};
#[cfg(feature = "alloc")]
Expand Down
43 changes: 43 additions & 0 deletions src/serdes/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,57 @@ use crate::{
view::BitViewSized,
};

/// A zero-sized type that deserializes from any string as long as it is equal
/// to `any::type_name::<T>()`.
pub(super) struct TypeName<T>(PhantomData<T>);

impl<T> TypeName<T> {
/// Creates a type-name ghost for any type.
fn new() -> Self {
TypeName(PhantomData)
}
}

impl<'de, T> Deserialize<'de> for TypeName<T> {
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
where D: Deserializer<'de> {
deserializer.deserialize_str(Self::new())
}
}

impl<'de, T> Visitor<'de> for TypeName<T> {
type Value = Self;

fn expecting(&self, fmt: &mut Formatter) -> fmt::Result {
write!(fmt, "the string {:?}", any::type_name::<T>())
}

fn visit_str<E>(self, value: &str) -> Result<Self::Value, E>
where E: serde::de::Error {
if value == any::type_name::<T>() {
Ok(self)
}
else {
Err(serde::de::Error::invalid_value(
Unexpected::Str(value),
&self,
))
}
}
}

/// Fields used in the `BitIdx` transport format.
static FIELDS: &[&str] = &["width", "index"];

/// The components of a bit-idx in wire format.
enum Field {
/// Denotes the maximum allowable value of the bit-idx.
Width,
/// Denotes the value of the bit-idx.
Index,
}

/// Visits field tokens of a bit-idx wire format.
struct FieldVisitor;

impl<'de> Deserialize<'de> for Field {
Expand Down

0 comments on commit 2e75d5e

Please sign in to comment.