Skip to content

Commit

Permalink
fix??
Browse files Browse the repository at this point in the history
  • Loading branch information
nothendev committed Oct 17, 2023
1 parent 0ce8f6a commit df6f734
Show file tree
Hide file tree
Showing 7 changed files with 118 additions and 104 deletions.
80 changes: 14 additions & 66 deletions cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@ use std::{borrow::Cow, num::ParseIntError, ops::Range};
use oxcr_protocol::{
aott::{
self,
text::{self, CharError},
error::LabelError,
text::{self, CharError, CharLabel},
},
miette::{self, SourceSpan},
ser::any_of,
Expand Down Expand Up @@ -61,35 +62,21 @@ pub enum ParseError {
#[source]
error: ParseIntError,
},
#[error("filter failed in {location}, while checking {token}")]
#[diagnostic(code(aott::error::filter_failed))]
FilterFailed {
#[label = "this is the token that didn't pass"]
at: SourceSpan,
location: &'static core::panic::Location<'static>,
token: char,
},
#[error("expected keyword {keyword}")]
#[diagnostic(code(aott::text::error::expected_keyword))]
ExpectedKeyword {
#[label = "the keyword here is {found}"]
at: SourceSpan,
keyword: String,
found: String,
},
#[error("expected digit of radix {radix}")]
#[diagnostic(code(aott::text::error::expected_digit))]
ExpectedDigit {

#[error("{label}; last token was {last_token:?}")]
Text {
#[label = "here"]
at: SourceSpan,
radix: u32,
found: char,
label: aott::text::CharLabel<char>,
last_token: Option<char>,
},
#[error("expected identifier character (a-zA-Z or _), but found {found}")]
ExpectedIdent {

#[error("{label}; last token was {last_token:?}")]
Builtin {
#[label = "here"]
at: SourceSpan,
found: char,
label: aott::error::BuiltinLabel,
last_token: Option<char>,
},
}

Expand All @@ -105,7 +92,7 @@ pub enum Expectation {
Digit(u32),
}

impl<'a> aott::error::Error<&'a str> for ParseError {
impl<'a> aott::error::FundamentalError<&'a str> for ParseError {
fn unexpected_eof(
span: Range<usize>,
expected: Option<Vec<<&'a str as aott::prelude::InputType>::Token>>,
Expand Down Expand Up @@ -134,45 +121,6 @@ impl<'a> aott::error::Error<&'a str> for ParseError {
help: None,
}
}

fn filter_failed(
span: Range<usize>,
location: &'static core::panic::Location<'static>,
token: <&'a str as aott::prelude::InputType>::Token,
) -> Self {
Self::FilterFailed {
at: span.into(),
location,
token,
}
}
}

impl CharError<char> for ParseError {
fn expected_digit(span: Range<usize>, radix: u32, got: char) -> Self {
Self::ExpectedDigit {
at: span.into(),
radix,
found: got,
}
}

fn expected_ident_char(span: Range<usize>, got: char) -> Self {
Self::ExpectedIdent {
at: span.into(),
found: got,
}
}

fn expected_keyword<'a, 'b: 'a>(
span: Range<usize>,
keyword: &'b <char as text::Char>::Str,
actual: &'a <char as text::Char>::Str,
) -> Self {
Self::ExpectedKeyword {
at: span.into(),
keyword: keyword.to_owned(),
found: actual.to_owned(),
}
}
}
impl<'a> LabelError<&'a str, CharLabel<char>> for ParseError {}
6 changes: 3 additions & 3 deletions protocol/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,12 @@ pub mod packets;
pub mod registry;
mod varint;
use self::registry::RegistryItem;
use aott::primitive::one_of;
use aott::primitive::{filter, one_of};
use bytes::BufMut;
use std::{ops::RangeInclusive, ptr};
pub use varint::*;

use crate::ser::{Deserialize, Serialize};
use crate::ser::{Deserialize, Serialize, Type};
pub mod item;

pub const MAX_PACKET_DATA: usize = 0x1FFFFF;
Expand Down Expand Up @@ -43,7 +43,7 @@ impl Deserialize for Difficulty {
fn deserialize<'a>(
input: &mut aott::prelude::Input<&'a [u8], crate::ser::Extra<Self::Context>>,
) -> aott::PResult<&'a [u8], Self, crate::ser::Extra<Self::Context>> {
let byte = one_of([0x0, 0x1, 0x2, 0x3])(input)?;
let byte = filter(|x| matches!(x, 0..=3), Type::Difficulty)(input)?;
Ok(unsafe { *ptr::addr_of!(byte).cast() })
}
}
Expand Down
6 changes: 3 additions & 3 deletions protocol/src/model/packets/play.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::{
};
use std::ptr;

use aott::primitive::one_of;
use aott::primitive::{filter, one_of};
use bytes::BufMut;
use indexmap::IndexMap;

Expand Down Expand Up @@ -106,7 +106,7 @@ impl Deserialize for GameMode {
fn deserialize<'a>(
input: &mut aott::prelude::Input<&'a [u8], Extra<Self::Context>>,
) -> aott::PResult<&'a [u8], Self, Extra<Self::Context>> {
let byte = one_of([0x0, 0x1, 0x2, 0x3])(input)?;
let byte = filter(|x| matches!(x, 0..=3), Type::GameMode)(input)?;
Ok(unsafe { *ptr::addr_of!(byte).cast() })
}
}
Expand All @@ -131,7 +131,7 @@ impl Deserialize for PreviousGameMode {
input: &mut aott::prelude::Input<&'a [u8], Extra<Self::Context>>,
) -> aott::PResult<&'a [u8], Self, Extra<Self::Context>> {
let byte = aott::bytes::number::big::i8
.filter(|g| (-1..=3).contains(g))
.filter(|g| (-1..=3).contains(g), Type::PreviousGameMode)
.parse_with(input)?;
Ok(unsafe { *ptr::addr_of!(byte).cast() })
}
Expand Down
21 changes: 14 additions & 7 deletions protocol/src/nbt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,13 @@ use serde::{
};

use crate::ser::*;
use aott::iter::IterParser;

#[derive(Clone, Debug, thiserror::Error, miette::Diagnostic)]
pub enum Label {
InvalidTagType(u8)
#[error("invalid tag type {_0}, expected a byte in range 0..=12")]
#[diagnostic(code(nbt::error::invalid_tag_type))]
InvalidTagType(u8),
}

#[derive(Debug, Clone, derive_more::From)]
Expand Down Expand Up @@ -256,7 +259,8 @@ impl NbtTag {
v.ok_or(crate::error::Error::Nbt(NbtError::ExpectedAnythingButEnd))
})
.repeated()
.exactly(length),
.exactly(length)
.collect::<Vec<_>>(),
tag,
)(input)?;

Expand Down Expand Up @@ -284,7 +288,7 @@ pub enum NbtTagType {

#[parser(extras = "Extra<()>")]
fn nbt_tag(input: &[u8]) -> NbtTagType {
any.filter(|b| (0u8..=12u8).contains(b), |b| Label::)
any.filter(|b| (0u8..=12u8).contains(b), Label::InvalidTagType)
// SAFETY: in filter we filter the tag types to be in bounds
.map(|b| unsafe { *(&b as *const u8 as *const NbtTagType) })
.parse_with(input)
Expand Down Expand Up @@ -328,10 +332,13 @@ impl<T: Deserialize> Deserialize for SmolArray<T> {
debug_assert!(length >= 0);
let length = length as usize;

T::deserialize
.repeated_custom::<Self>()
.exactly(length)
.parse_with(input)
Ok(Self(
T::deserialize
.repeated()
.exactly(length)
.collect()
.parse_with(input)?,
))
}
}

Expand Down
19 changes: 17 additions & 2 deletions protocol/src/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,14 +21,14 @@ use crate::model::VarInt;
use ::bytes::{BufMut, Bytes, BytesMut};
pub use aott::prelude::parser;
pub use aott::prelude::Parser;
use aott::{error::FundamentalError, pfn_type, prelude::*};
use aott::{error::FundamentalError, iter::IterParser, pfn_type, prelude::*};
use tracing::debug;

mod error;
mod types;

pub use error::*;
pub use types::*;
pub use types::{Label as Type, *};

pub trait Deserialize: Sized {
type Context = ();
Expand Down Expand Up @@ -143,3 +143,18 @@ pub macro impl_ser($(|$context:ty|)? $ty:ty => [$($(|$cx:ty|)?$field:ident),*$(,
crate::ser::deserialize!($(|$context|)? $ty => [$($(|$cx|)?$field,)*]);
crate::ser::serialize!($ty => [$($field,)*]);
}

#[inline(always)]
pub fn no_context<I: InputType, O, E: ParserExtras<I>, EV: ParserExtras<I, Context = ()>>(
parser: impl Parser<I, O, EV>,
) -> impl Fn(&mut Input<I, E>) -> Result<O, EV::Error> {
move |input| parser.parse_with(&mut input.no_context())
}

#[inline(always)]
pub fn with_context<I: InputType, O, E: ParserExtras<I>, E2: ParserExtras<I, Context = C>, C>(
parser: impl Parser<I, O, E2>,
context: C,
) -> impl Fn(&mut Input<I, E>) -> Result<O, E2::Error> {
move |input| parser.parse_with(&mut input.with_context(&context))
}
69 changes: 54 additions & 15 deletions protocol/src/ser/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,7 +35,7 @@ pub enum SerializationError<Item: Debug + Display + Char> {
at: SourceSpan,
},

#[error("type error {label}, last token is {last_token:?}")]
#[error("{label}; last token is {last_token:?}")]
Type {
#[label = "here"]
at: SourceSpan,
Expand All @@ -46,36 +46,75 @@ pub enum SerializationError<Item: Debug + Display + Char> {
last_token: Option<Item>,
},

#[error("strign error {label}, last token is {last_token:?}")]
#[error("{label}; last token is {last_token:?}")]
String {
#[label = "here"]
at: SourceSpan,
#[source]
label: aott::text::CharLabel<Item>,
last_token: Option<Item>,
},

#[error("{label}; last token was {last_token:?}")]
Nbt {
#[label = "here"]
at: SourceSpan,
#[diagnostic_source]
#[source]
#[diagnostic(transparent)]
label: crate::nbt::Label,
last_token: Option<Item>,
},

#[error("{label}; last token was {last_token:?}")]
Builtin {
#[label = "here"]
at: SourceSpan,
label: aott::error::BuiltinLabel,
last_token: Option<Item>,
},
}

macro label_error($laty:ty => $variant:ident) {
impl<Item: Debug + Display + Char, I: InputType<Token = Item>> LabelError<I, $laty>
for SerializationError<Item>
{
fn from_label(span: Range<usize>, label: $laty, last_token: Option<Item>) -> Self {
Self::$variant {
at: span.into(),
label,
last_token,
macro_rules! label_error {
($variant:ident; u8 => $u8:ty) => {
impl<'a> aott::error::LabelError<&'a [u8], $u8> for crate::error::Error {
fn from_label(span: Range<usize>, label: $u8, last_token: Option<u8>) -> Self {
Self::Ser(SerializationError::$variant {
at: span.into(),
label,
last_token,
})
}
}
};
($variant:ident; char => $char:ty) => {
impl<'a> aott::error::LabelError<&'a str, $char> for crate::error::Error {
fn from_label(span: Range<usize>, label: $char, last_token: Option<char>) -> Self {
Self::SerStr(SerializationError::$variant {
at: span.into(),
label,
last_token,
})
}
}
};
($variant:ident => $u8:ty; $char:ty) => {
label_error!($variant; u8 => $u8);
label_error!($variant; char => $char);
};
($laty:ty => $variant:ident) => {
label_error!($variant; u8 => $laty);
label_error!($variant; char => $laty);
}
}

label_error!(super::types::Label => Type);
label_error!(CharLabel<Item> => String);
label_error!(String => CharLabel<u8>; CharLabel<char>);
label_error!(Nbt; u8 => crate::nbt::Label);
label_error!(aott::error::BuiltinLabel => Builtin);

#[derive(thiserror::Error, miette::Diagnostic, Debug)]
#[error("{}", any_of_display(.errors))]
pub struct WithSource<Item: Debug + Display + 'static> {
#[error("{errors:#?}")]
pub struct WithSource<Item: Debug + Display + Char + 'static> {
#[source_code]
pub src: BytesSource,
#[related]
Expand Down
Loading

0 comments on commit df6f734

Please sign in to comment.