diff --git a/src/base64_vec_type.rs b/src/base64_vec_type.rs deleted file mode 100644 index 3d80ffe..0000000 --- a/src/base64_vec_type.rs +++ /dev/null @@ -1,162 +0,0 @@ -// Copyright (c) The byte-wrapper Contributors -// SPDX-License-Identifier: MIT OR Apache-2.0 - -//! The [`Base64Vec`] newtype wrapper. - -extern crate alloc; - -use crate::base64_vec; -use alloc::vec::Vec; -use base64::Engine; -use core::fmt; -use serde_core::{Deserializer, Serializer}; - -/// A byte vector that serializes as base64 in human-readable formats. -/// -/// This type can be used in two ways: -/// -/// 1. Directly as a field type, with serde impls built in. -/// 2. With `#[serde(with = "Base64Vec")]` and `#[schemars(with = "Base64Vec")]` -/// on a `Vec` field. -#[derive(Clone, PartialEq, Eq, Hash, Default)] -pub struct Base64Vec(pub Vec); - -impl Base64Vec { - /// Creates a new `Base64Vec` from a byte vector. - #[inline] - pub fn new(bytes: Vec) -> Self { - Self(bytes) - } - - /// Returns the inner byte vector. - #[inline] - pub fn into_inner(self) -> Vec { - self.0 - } - - /// Serializes a byte vector as base64 (for `#[serde(with = "Base64Vec")]`). - pub fn serialize(bytes: &[u8], serializer: S) -> Result - where - S: Serializer, - { - base64_vec::serialize(bytes, serializer) - } - - /// Deserializes a byte vector from base64 (for `#[serde(with = "Base64Vec")]`). - pub fn deserialize<'de, D>(deserializer: D) -> Result, D::Error> - where - D: Deserializer<'de>, - { - base64_vec::deserialize(deserializer) - } -} - -impl fmt::Debug for Base64Vec { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!( - f, - "Base64Vec({})", - base64::engine::general_purpose::STANDARD.encode(&self.0) - ) - } -} - -impl fmt::Display for Base64Vec { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - base64::engine::general_purpose::STANDARD.encode(&self.0).fmt(f) - } -} - -impl core::ops::Deref for Base64Vec { - type Target = Vec; - - #[inline] - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl core::ops::DerefMut for Base64Vec { - #[inline] - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 - } -} - -impl AsRef<[u8]> for Base64Vec { - #[inline] - fn as_ref(&self) -> &[u8] { - &self.0 - } -} - -impl AsMut<[u8]> for Base64Vec { - #[inline] - fn as_mut(&mut self) -> &mut [u8] { - &mut self.0 - } -} - -impl From> for Base64Vec { - #[inline] - fn from(bytes: Vec) -> Self { - Self(bytes) - } -} - -impl From for Vec { - #[inline] - fn from(base64_vec: Base64Vec) -> Self { - base64_vec.0 - } -} - -impl serde_core::Serialize for Base64Vec { - fn serialize(&self, serializer: S) -> Result - where - S: Serializer, - { - base64_vec::serialize(&self.0, serializer) - } -} - -impl<'de> serde_core::Deserialize<'de> for Base64Vec { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - base64_vec::deserialize(deserializer).map(Self) - } -} - -#[cfg(feature = "schemars08")] -mod schemars_impls { - use super::Base64Vec; - use alloc::string::String; - use schemars08::{ - JsonSchema, - r#gen::SchemaGenerator, - schema::{InstanceType, Schema, SchemaObject}, - }; - - impl JsonSchema for Base64Vec { - fn schema_name() -> String { - "Base64Vec".into() - } - - fn is_referenceable() -> bool { - false - } - - fn json_schema(_generator: &mut SchemaGenerator) -> Schema { - Schema::Object(SchemaObject { - instance_type: Some(InstanceType::String.into()), - format: Some("byte".into()), - extensions: [("contentEncoding".into(), "base64".into())] - .into_iter() - .collect(), - ..Default::default() - }) - } - } -} diff --git a/src/hex_array_type.rs b/src/hex_array_type.rs deleted file mode 100644 index b38b9c5..0000000 --- a/src/hex_array_type.rs +++ /dev/null @@ -1,170 +0,0 @@ -// Copyright (c) The byte-wrapper Contributors -// SPDX-License-Identifier: MIT OR Apache-2.0 - -//! The [`HexArray`] newtype wrapper. - -use crate::hex_array; -#[cfg(feature = "alloc")] -use core::fmt; -use serde_core::Deserializer; - -/// A byte array that serializes as hex in human-readable formats. -/// -/// This type can be used in two ways: -/// -/// 1. Directly as a field type, with serde impls built in. -/// 2. With `#[serde(with = "HexArray::")]` and -/// `#[schemars(with = "HexArray")]` on a `[u8; N]` field. -#[derive(Copy, Clone, PartialEq, Eq, Hash)] -pub struct HexArray(pub [u8; N]); - -impl Default for HexArray { - fn default() -> Self { - Self([0u8; N]) - } -} - -impl HexArray { - /// Creates a new `HexArray` from a byte array. - #[inline] - pub const fn new(bytes: [u8; N]) -> Self { - Self(bytes) - } - - /// Returns the inner byte array. - #[inline] - pub const fn into_inner(self) -> [u8; N] { - self.0 - } - - /// Serializes a byte array as hex (for `#[serde(with = "HexArray::")]`). - #[cfg(feature = "alloc")] - pub fn serialize( - bytes: &[u8; N], - serializer: S, - ) -> Result - where - S: serde_core::Serializer, - { - hex_array::serialize(bytes, serializer) - } - - /// Deserializes a byte array from hex (for `#[serde(with = "HexArray::")]`). - pub fn deserialize<'de, D>(deserializer: D) -> Result<[u8; N], D::Error> - where - D: Deserializer<'de>, - { - hex_array::deserialize(deserializer) - } -} - -#[cfg(feature = "alloc")] -impl fmt::Debug for HexArray { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - write!(f, "HexArray({})", hex::encode(self.0)) - } -} - -#[cfg(feature = "alloc")] -impl fmt::Display for HexArray { - fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { - hex::encode(self.0).fmt(f) - } -} - -impl core::ops::Deref for HexArray { - type Target = [u8; N]; - - #[inline] - fn deref(&self) -> &Self::Target { - &self.0 - } -} - -impl core::ops::DerefMut for HexArray { - #[inline] - fn deref_mut(&mut self) -> &mut Self::Target { - &mut self.0 - } -} - -impl AsRef<[u8]> for HexArray { - #[inline] - fn as_ref(&self) -> &[u8] { - &self.0 - } -} - -impl AsMut<[u8]> for HexArray { - #[inline] - fn as_mut(&mut self) -> &mut [u8] { - &mut self.0 - } -} - -impl From<[u8; N]> for HexArray { - #[inline] - fn from(bytes: [u8; N]) -> Self { - Self(bytes) - } -} - -impl From> for [u8; N] { - #[inline] - fn from(hex_array: HexArray) -> Self { - hex_array.0 - } -} - -#[cfg(feature = "alloc")] -impl serde_core::Serialize for HexArray { - fn serialize(&self, serializer: S) -> Result - where - S: serde_core::Serializer, - { - hex_array::serialize(&self.0, serializer) - } -} - -impl<'de, const N: usize> serde_core::Deserialize<'de> for HexArray { - fn deserialize(deserializer: D) -> Result - where - D: Deserializer<'de>, - { - hex_array::deserialize(deserializer).map(Self) - } -} - -#[cfg(feature = "schemars08")] -mod schemars_impls { - use super::HexArray; - use alloc::{boxed::Box, format, string::String}; - use schemars08::{ - JsonSchema, - r#gen::SchemaGenerator, - schema::{InstanceType, Schema, SchemaObject, StringValidation}, - }; - - impl JsonSchema for HexArray { - fn schema_name() -> String { - format!("HexArray_{N}") - } - - fn is_referenceable() -> bool { - false - } - - fn json_schema(_generator: &mut SchemaGenerator) -> Schema { - let hex_len = N * 2; - Schema::Object(SchemaObject { - instance_type: Some(InstanceType::String.into()), - string: Some(Box::new(StringValidation { - min_length: Some(hex_len as u32), - max_length: Some(hex_len as u32), - pattern: Some(format!("^[0-9a-fA-F]{{{hex_len}}}$")), - })), - ..Default::default() - }) - } - } -}