Skip to content

Commit

Permalink
fix: Allow using serde without bytes crate
Browse files Browse the repository at this point in the history
  • Loading branch information
Norbiros committed Sep 20, 2024
1 parent deba274 commit 27ddb22
Show file tree
Hide file tree
Showing 2 changed files with 44 additions and 13 deletions.
43 changes: 30 additions & 13 deletions src/serde/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,16 +7,17 @@ use bytes::{Buf, BytesMut};
use crab_nbt::nbt::utils::{get_nbt_string, END_ID};
use serde::de::{self, DeserializeSeed, MapAccess, SeqAccess, Visitor};
use serde::{forward_to_deserialize_any, Deserialize};
use std::io::Cursor;

#[derive(Debug)]
pub struct Deserializer<'de> {
input: &'de mut BytesMut,
pub struct Deserializer<'de, T: Buf> {
input: &'de mut T,
tag_to_deserialize: Option<u8>,
is_named: bool,
}

impl<'de> Deserializer<'de> {
pub fn new(input: &'de mut BytesMut, is_named: bool) -> Self {
impl<'de, T: Buf> Deserializer<'de, T> {
pub fn new(input: &'de mut T, is_named: bool) -> Self {
Deserializer {
input,
tag_to_deserialize: None,
Expand All @@ -34,6 +35,14 @@ where
T::deserialize(&mut deserializer)
}

pub fn from_cursor<'a, T>(cursor: &'a mut Cursor<&[u8]>) -> Result<T>
where
T: Deserialize<'a>,
{
let mut deserializer = Deserializer::new(cursor, true);
T::deserialize(&mut deserializer)
}

/// Deserializes struct using Serde Deserializer from normal NBT
pub fn from_bytes_unnamed<'a, T>(s: &'a mut BytesMut) -> Result<T>
where
Expand All @@ -43,7 +52,15 @@ where
T::deserialize(&mut deserializer)
}

impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
pub fn from_cursor_unnamed<'a, T>(cursor: &'a mut Cursor<&[u8]>) -> Result<T>
where
T: Deserialize<'a>,
{
let mut deserializer = Deserializer::new(cursor, false);
T::deserialize(&mut deserializer)
}

impl<'de, 'a, T: Buf> de::Deserializer<'de> for &'a mut Deserializer<'de, T> {
type Error = Error;

forward_to_deserialize_any!(i8 i16 i32 i64 u8 u16 u32 u64 f32 f64 seq char str string bytes byte_buf tuple tuple_struct enum ignored_any unit unit_struct option newtype_struct);
Expand Down Expand Up @@ -145,11 +162,11 @@ impl<'de, 'a> de::Deserializer<'de> for &'a mut Deserializer<'de> {
}
}

struct CompoundAccess<'a, 'de: 'a> {
de: &'a mut Deserializer<'de>,
struct CompoundAccess<'a, 'de: 'a, T: Buf> {
de: &'a mut Deserializer<'de, T>,
}

impl<'de, 'a> MapAccess<'de> for CompoundAccess<'a, 'de> {
impl<'de, 'a, T: Buf> MapAccess<'de> for CompoundAccess<'a, 'de, T> {
type Error = Error;

fn next_key_seed<K>(&mut self, seed: K) -> Result<Option<K::Value>>
Expand All @@ -174,18 +191,18 @@ impl<'de, 'a> MapAccess<'de> for CompoundAccess<'a, 'de> {
}
}

struct ListAccess<'a, 'de: 'a> {
de: &'a mut Deserializer<'de>,
struct ListAccess<'a, 'de: 'a, T: Buf> {
de: &'a mut Deserializer<'de, T>,
remaining_values: u32,
list_type: u8,
}

impl<'a, 'de> SeqAccess<'de> for ListAccess<'a, 'de> {
impl<'a, 'de, T: Buf> SeqAccess<'de> for ListAccess<'a, 'de, T> {
type Error = Error;

fn next_element_seed<T>(&mut self, seed: T) -> Result<Option<T::Value>>
fn next_element_seed<E>(&mut self, seed: E) -> Result<Option<E::Value>>
where
T: DeserializeSeed<'de>,
E: DeserializeSeed<'de>,
{
if self.remaining_values == 0 {
return Ok(None);
Expand Down
14 changes: 14 additions & 0 deletions src/serde/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,13 @@ where
Ok(serializer.output)
}

pub fn to_vec_unnamed<T>(value: &T) -> Result<Vec<u8>>
where
T: Serialize,
{
to_bytes_unnamed(value).map(|bytes| bytes.to_vec())
}

/// Serializes struct using Serde Serializer to normal NBT
pub fn to_bytes<T>(value: &T, name: String) -> Result<BytesMut>
where
Expand All @@ -78,6 +85,13 @@ where
Ok(serializer.output)
}

pub fn to_vec<T>(value: &T, name: String) -> Result<Vec<u8>>
where
T: Serialize,
{
to_bytes(value, name).map(|bytes| bytes.to_vec())
}

impl<'a> ser::Serializer for &'a mut Serializer {
type Ok = ();
type Error = Error;
Expand Down

0 comments on commit 27ddb22

Please sign in to comment.