Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add decode_any through Visitor #11

Merged
merged 1 commit into from
May 16, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 4 additions & 4 deletions crates/musli-common/src/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -89,11 +89,11 @@ pub trait Reader<'de> {

#[inline]
fn visit_borrowed(self, bytes: &'de Self::Target) -> Result<Self::Ok, Self::Error> {
self.visit_any(bytes)
self.visit_ref(bytes)
}

#[inline]
fn visit_any(self, bytes: &Self::Target) -> Result<Self::Ok, Self::Error> {
fn visit_ref(self, bytes: &Self::Target) -> Result<Self::Ok, Self::Error> {
self.0.copy_from_slice(bytes);
Ok(())
}
Expand Down Expand Up @@ -134,11 +134,11 @@ pub trait Reader<'de> {

#[inline]
fn visit_borrowed(self, bytes: &'de Self::Target) -> Result<Self::Ok, Self::Error> {
self.visit_any(bytes)
self.visit_ref(bytes)
}

#[inline]
fn visit_any(mut self, bytes: &Self::Target) -> Result<Self::Ok, Self::Error> {
fn visit_ref(mut self, bytes: &Self::Target) -> Result<Self::Ok, Self::Error> {
self.0.copy_from_slice(bytes);
Ok(self.0)
}
Expand Down
94 changes: 81 additions & 13 deletions crates/musli-descriptive/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use alloc::string::String;
use alloc::vec::Vec;

use musli::de::{
Decoder, LengthHint, NumberHint, NumberVisitor, PackDecoder, PairDecoder, PairsDecoder,
SequenceDecoder, TypeHint, ValueVisitor, VariantDecoder,
Decoder, NumberHint, NumberVisitor, PackDecoder, PairDecoder, PairsDecoder, SequenceDecoder,
SizeHint, TypeHint, ValueVisitor, VariantDecoder, Visitor,
};
use musli::error::Error;
use musli_common::int::{continuation as c, UsizeEncoding, Variable};
Expand Down Expand Up @@ -209,28 +209,28 @@ where
Kind::Sequence => {
let hint = tag
.data()
.map(|d| LengthHint::Exact(d as usize))
.map(|d| SizeHint::Exact(d as usize))
.unwrap_or_default();
Ok(TypeHint::Sequence(hint))
}
Kind::Map => {
let hint = tag
.data()
.map(|d| LengthHint::Exact(d as usize))
.map(|d| SizeHint::Exact(d as usize))
.unwrap_or_default();
Ok(TypeHint::Map(hint))
}
Kind::Bytes => {
let hint = tag
.data()
.map(|d| LengthHint::Exact(d as usize))
.map(|d| SizeHint::Exact(d as usize))
.unwrap_or_default();
Ok(TypeHint::Bytes(hint))
}
Kind::String => {
let hint = tag
.data()
.map(|d| LengthHint::Exact(d as usize))
.map(|d| SizeHint::Exact(d as usize))
.unwrap_or_default();
Ok(TypeHint::String(hint))
}
Expand Down Expand Up @@ -321,9 +321,9 @@ where
}

#[inline]
fn visit_any(self, bytes: &[u8]) -> Result<Self::Ok, Self::Error> {
fn visit_ref(self, bytes: &[u8]) -> Result<Self::Ok, Self::Error> {
let string = core::str::from_utf8(bytes).map_err(Self::Error::custom)?;
self.0.visit_any(string)
self.0.visit_ref(string)
}
}
}
Expand Down Expand Up @@ -371,7 +371,7 @@ where
#[inline]
fn decode_number<V>(mut self, visitor: V) -> Result<V::Ok, Self::Error>
where
V: NumberVisitor<Error = Self::Error>,
V: NumberVisitor<'de, Error = Self::Error>,
{
let tag = Tag::from_byte(self.reader.read_byte()?);

Expand Down Expand Up @@ -538,6 +538,74 @@ where

Ok(self)
}

#[inline]
fn decode_any<V>(mut self, visitor: V) -> Result<V::Ok, Self::Error>
where
V: Visitor<'de, Error = Self::Error>,
{
let tag = match self.reader.peek()? {
Some(b) => Tag::from_byte(b),
None => return visitor.visit_any(self, TypeHint::Any),
};

match tag.kind() {
Kind::Number => match tag.data() {
Some(U8) => visitor.visit_u8(self.decode_u8()?),
Some(U16) => visitor.visit_u16(self.decode_u16()?),
Some(U32) => visitor.visit_u32(self.decode_u32()?),
Some(U64) => visitor.visit_u64(self.decode_u64()?),
Some(U128) => visitor.visit_u128(self.decode_u128()?),
Some(I8) => visitor.visit_i8(self.decode_i8()?),
Some(I16) => visitor.visit_i16(self.decode_i16()?),
Some(I32) => visitor.visit_i32(self.decode_i32()?),
Some(I64) => visitor.visit_i64(self.decode_i64()?),
Some(I128) => visitor.visit_i128(self.decode_i128()?),
Some(F32) => visitor.visit_f32(self.decode_f32()?),
Some(F64) => visitor.visit_f64(self.decode_f64()?),
_ => {
let visitor = visitor.visit_number(NumberHint::Any)?;
visitor.visit_any(self, TypeHint::Number(NumberHint::Any))
}
},
Kind::Sequence => {
let sequence = self.shared_decode_sequence()?;
visitor.visit_sequence(sequence)
}
Kind::Map => {
let map = self.shared_decode_map()?;
visitor.visit_map(map)
}
Kind::Bytes => {
let hint = tag
.data()
.map(|d| SizeHint::Exact(d as usize))
.unwrap_or_default();
let visitor = visitor.visit_bytes(hint)?;
self.decode_bytes(visitor)
}
Kind::String => {
let hint = tag
.data()
.map(|d| SizeHint::Exact(d as usize))
.unwrap_or_default();
let visitor = visitor.visit_string(hint)?;
self.decode_string(visitor)
}
Kind::Mark => match tag.mark() {
Mark::True | Mark::False => visitor.visit_bool(self.decode_bool()?),
Mark::Variant => visitor.visit_variant(self.decode_variant()?),
Mark::Some | Mark::None => visitor.visit_option(self.decode_option()?),
Mark::Char => visitor.visit_char(self.decode_char()?),
Mark::Unit => {
self.decode_unit()?;
visitor.visit_unit()
}
_ => visitor.visit_any(self, TypeHint::Any),
},
_ => visitor.visit_any(self, TypeHint::Any),
}
}
}

impl<'de, R> PackDecoder<'de> for SelfPackDecoder<R>
Expand Down Expand Up @@ -599,8 +667,8 @@ where
type Decoder<'this> = SelfDecoder<R::PosMut<'this>> where Self: 'this;

#[inline]
fn size_hint(&self) -> Option<usize> {
Some(self.remaining)
fn size_hint(&self) -> SizeHint {
SizeHint::Exact(self.remaining)
}

#[inline]
Expand Down Expand Up @@ -690,8 +758,8 @@ where
Self: 'this;

#[inline]
fn size_hint(&self) -> Option<usize> {
Some(self.remaining)
fn size_hint(&self) -> SizeHint {
SizeHint::Exact(self.remaining)
}

#[inline]
Expand Down
63 changes: 48 additions & 15 deletions crates/musli-json/src/de.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use core::str;
use alloc::vec::Vec;

use musli::de::{
Decoder, LengthHint, NumberHint, NumberVisitor, PackDecoder, PairDecoder, PairsDecoder,
SequenceDecoder, TypeHint, ValueVisitor, VariantDecoder,
Decoder, NumberHint, NumberVisitor, PackDecoder, PairDecoder, PairsDecoder, SequenceDecoder,
SizeHint, TypeHint, ValueVisitor, VariantDecoder, Visitor,
};
use musli::error::Error;
#[cfg(feature = "musli-value")]
Expand Down Expand Up @@ -130,9 +130,9 @@ where
#[inline]
fn type_hint(&mut self) -> Result<TypeHint, Self::Error> {
Ok(match self.parser.peek()? {
Token::OpenBrace => TypeHint::Map(LengthHint::Any),
Token::OpenBracket => TypeHint::Sequence(LengthHint::Any),
Token::String => TypeHint::String(LengthHint::Any),
Token::OpenBrace => TypeHint::Map(SizeHint::Any),
Token::OpenBracket => TypeHint::Sequence(SizeHint::Any),
Token::String => TypeHint::String(SizeHint::Any),
Token::Number => TypeHint::Number(NumberHint::Any),
Token::Null => TypeHint::Unit,
Token::True => TypeHint::Bool,
Expand Down Expand Up @@ -270,7 +270,7 @@ where
#[inline]
fn decode_number<V>(mut self, visitor: V) -> Result<V::Ok, Self::Error>
where
V: NumberVisitor<Error = Self::Error>,
V: NumberVisitor<'de, Error = Self::Error>,
{
self.parser.parse_number(visitor)
}
Expand All @@ -282,7 +282,7 @@ where
V: ValueVisitor<'de, Target = [u8], Error = Self::Error>,
{
let mut seq = self.decode_sequence()?;
let mut bytes = Vec::with_capacity(seq.size_hint().unwrap_or_default());
let mut bytes = Vec::with_capacity(seq.size_hint().or_default());

while let Some(item) = SequenceDecoder::next(&mut seq)? {
bytes.push(item.decode_u8()?);
Expand All @@ -298,7 +298,7 @@ where
{
match self.parser.parse_string(self.scratch, true)? {
StringReference::Borrowed(borrowed) => visitor.visit_borrowed(borrowed),
StringReference::Scratch(string) => visitor.visit_any(string),
StringReference::Scratch(string) => visitor.visit_ref(string),
}
}

Expand Down Expand Up @@ -341,6 +341,27 @@ where
fn decode_variant(self) -> Result<Self::Variant, Self::Error> {
JsonVariantDecoder::new(self.scratch, self.parser)
}

#[inline]
fn decode_any<V>(mut self, visitor: V) -> Result<V::Ok, Self::Error>
where
V: Visitor<'de, Error = Self::Error>,
{
match self.parser.peek()? {
Token::OpenBrace => {
visitor.visit_map(JsonObjectDecoder::new(self.scratch, None, self.parser)?)
}
Token::OpenBracket => {
visitor.visit_sequence(JsonSequenceDecoder::new(self.scratch, None, self.parser)?)
}
Token::String => self.decode_string(visitor.visit_string(SizeHint::Any)?),
Token::Number => self.decode_number(visitor.visit_number(NumberHint::Any)?),
Token::Null => visitor.visit_unit(),
Token::True => visitor.visit_bool(true),
Token::False => visitor.visit_bool(false),
_ => visitor.visit_any(self, TypeHint::Any),
}
}
}

/// A JSON object key decoder for Müsli.
Expand Down Expand Up @@ -376,7 +397,7 @@ where
{
match self.parser.parse_string(self.scratch, true)? {
StringReference::Borrowed(string) => visitor.visit_borrowed(string.as_bytes()),
StringReference::Scratch(string) => visitor.visit_any(string.as_bytes()),
StringReference::Scratch(string) => visitor.visit_ref(string.as_bytes()),
}
}
}
Expand Down Expand Up @@ -407,7 +428,7 @@ where
}

#[inline]
fn visit_any(self, bytes: &Self::Target) -> Result<Self::Ok, Self::Error> {
fn visit_ref(self, bytes: &Self::Target) -> Result<Self::Ok, Self::Error> {
integer::parse_unsigned(&mut &mut SliceParser::new(bytes))
}
}
Expand Down Expand Up @@ -438,7 +459,7 @@ where
}

#[inline]
fn visit_any(self, bytes: &Self::Target) -> Result<Self::Ok, Self::Error> {
fn visit_ref(self, bytes: &Self::Target) -> Result<Self::Ok, Self::Error> {
integer::parse_signed(&mut SliceParser::new(bytes))
}
}
Expand Down Expand Up @@ -528,6 +549,18 @@ where
{
JsonDecoder::new(self.scratch, self.parser).decode_string(visitor)
}

#[inline]
fn decode_any<V>(mut self, visitor: V) -> Result<V::Ok, Self::Error>
where
V: Visitor<'de, Error = Self::Error>,
{
match self.parser.peek()? {
Token::String => self.decode_string(visitor.visit_string(SizeHint::Any)?),
Token::Number => self.decode_number(visitor.visit_number(NumberHint::Any)?),
_ => visitor.visit_any(self, TypeHint::Any),
}
}
}

pub struct JsonObjectDecoder<'a, P> {
Expand Down Expand Up @@ -580,8 +613,8 @@ where
Self: 'this;

#[inline]
fn size_hint(&self) -> Option<usize> {
self.len
fn size_hint(&self) -> SizeHint {
SizeHint::from(self.len)
}

#[inline]
Expand Down Expand Up @@ -733,8 +766,8 @@ where
Self: 'this;

#[inline]
fn size_hint(&self) -> Option<usize> {
self.len
fn size_hint(&self) -> SizeHint {
SizeHint::from(self.len)
}

#[inline]
Expand Down
2 changes: 1 addition & 1 deletion crates/musli-json/src/reader/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ pub trait Parser<'de>: private::Sealed {
#[doc(hidden)]
fn parse_number<V>(&mut self, visitor: V) -> Result<V::Ok, ParseError>
where
V: NumberVisitor<Error = ParseError>,
V: NumberVisitor<'de, Error = ParseError>,
{
let signed = integer::decode_signed::<i128, _>(self)?;

Expand Down
23 changes: 23 additions & 0 deletions crates/musli-macros/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -108,6 +108,29 @@ pub fn encoder(attr: TokenStream, input: TokenStream) -> TokenStream {
}
}

#[proc_macro_attribute]
pub fn visitor(attr: TokenStream, input: TokenStream) -> TokenStream {
let attr = proc_macro2::TokenStream::from(attr);

if !attr.is_empty() {
return syn::Error::new_spanned(attr, "Arguments not supported")
.to_compile_error()
.into();
}

let input = syn::parse_macro_input!(input as types::Types);

match input.expand(
"visitor",
&types::VISITOR_TYPES,
["Ok", "Error"],
"__UseMusliVisitorAttributeMacro",
) {
Ok(tokens) => tokens.into(),
Err(err) => err.to_compile_error().into(),
}
}

fn to_compile_errors(errors: Vec<syn::Error>) -> proc_macro2::TokenStream {
let mut output = proc_macro2::TokenStream::new();

Expand Down
Loading