Skip to content

Commit

Permalink
fix: refactor parsing code for new aott
Browse files Browse the repository at this point in the history
  • Loading branch information
nothendev committed Oct 18, 2023
1 parent c5bdb04 commit e1178cb
Show file tree
Hide file tree
Showing 10 changed files with 119 additions and 21 deletions.
14 changes: 14 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,8 @@
members = [
"protocol",
"server",
"cli"
"cli",
"proxy"
]
default-members = [ "protocol", "server" ]
resolver = "2"
Expand All @@ -19,5 +20,5 @@ bevy = { version = "0.11.2", features = ["multi-threaded"], default-features = f
rayon = "1"
itertools = "0"
flate2 = "1"
tracing-subscriber = { version = "0.3", features = ["env-filter"] }
tracing-subscriber = { version = "0.3", features = ["env-filter"], default-features = false }
tokio-util = { version = "0", features = [ "full" ] }
9 changes: 3 additions & 6 deletions README.org
Original file line number Diff line number Diff line change
@@ -1,11 +1,8 @@
* OxCraft - an alternative Minecraft server implementation

** Structure
*** ECS
I chose Bevy for the ECS, not because it's better, but because Apecs is dog$hit.

*** Serialization
I chose AOTT for serialization, becuase serde is too much for me and implementing a serializer AND deserializer is just an awful process and I'm lazy.
** FAQ
*Why?* For fun.
*When release ?!?!* Can't say for certain, but it's very far away.

** Sources
- [[https://wiki.vg/index.php?title=Protocol&oldid=18375][Where I get protocol documentation]] (1.20.1)
Expand Down
7 changes: 4 additions & 3 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -101,12 +101,13 @@ pub fn flag_list(input: &str) -> Vec<FlagName<'a>> {
}
just("-")
.ignore_then(choice((
slice(filter(|thing: &char| *thing != ' '))
.map(FlagName::Short)
.repeated(),
just("-")
.ignore_then(ident)
.map(|name| vec![FlagName::Long(name)]),
slice(filter(|ch| *ch != ' ', Expectation::ShortFlag))
.map(FlagName::Short)
.repeated()
.collect::<Vec<_>>(),
)))
.parse_with(input)
}
Expand Down
67 changes: 60 additions & 7 deletions cli/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use oxcr_protocol::{
self,
error::{BuiltinLabel, LabelError},
primitive::SeqLabel,
text::{self, CharError, CharLabel},
text::CharLabel,
},
miette::{self, SourceSpan},
ser::any_of,
Expand All @@ -25,17 +25,17 @@ pub enum ParseError {
#[help]
help: Option<Cow<'static, str>>,
},

#[error("unexpected end of input{}", .expected.as_ref().map(|expectation| format!(", expected {expectation}")).unwrap_or_else(String::new))]
#[diagnostic(
code(aott::error::unexpected_eof),
help("try giving it more input next time, I guess?")
)]
#[diagnostic(code(aott::error::unexpected_eof))]
UnexpectedEof {
#[label = "here"]
at: SourceSpan,
#[label = "last data here"]
last_data_at: Option<SourceSpan>,
expected: Option<Expectation>,
#[help]
help: Option<Cow<'static, str>>,
},
#[error("parsing a Level failed: {actual}")]
#[diagnostic(code(cli::parse_level_error))]
Expand Down Expand Up @@ -80,7 +80,7 @@ pub enum ParseError {
last_token: Option<char>,
},

#[error("{label}; last token was {last_token:?}")]
#[error("expected {label:?}; last token was {last_token:?}")]
Sequence {
#[label = "here"]
at: SourceSpan,
Expand All @@ -89,7 +89,7 @@ pub enum ParseError {
},
}

#[derive(Debug, thiserror::Error)]
#[derive(Debug, thiserror::Error, Clone)]
pub enum Expectation {
#[error("{}", any_of(.0))]
AnyOf(Vec<char>),
Expand All @@ -99,6 +99,8 @@ pub enum Expectation {
EndOfInput,
#[error("a digit with radix {_0}")]
Digit(u32),
#[error("a short flag character (anything but a whitespace)")]
ShortFlag,
}

impl<'a> aott::error::FundamentalError<&'a str> for ParseError {
Expand All @@ -110,6 +112,7 @@ impl<'a> aott::error::FundamentalError<&'a str> for ParseError {
at: span.into(),
last_data_at: None,
expected: expected.map(Expectation::AnyOf),
help: None,
}
}

Expand Down Expand Up @@ -173,3 +176,53 @@ impl<'a> LabelError<&'a str, SeqLabel<char>> for ParseError {
}
}
}

impl<'a> LabelError<&'a str, Expectation> for ParseError {
fn from_label(
span: Range<usize>,
label: Expectation,
last_token: Option<<&'a str as aott::prelude::InputType>::Token>,
) -> Self {
if let Some(found) = last_token {
Self::Expected {
at: span.into(),
expected: label,
found,
help: None,
}
} else {
let last_data_at = (span.start.saturating_sub(1), span.end.saturating_sub(1)).into();
Self::UnexpectedEof {
at: span.into(),
last_data_at: Some(last_data_at),
expected: Some(label),
help: None,
}
}
}
}

impl<'a> LabelError<&'a str, (Expectation, Cow<'static, str>)> for ParseError {
fn from_label(
span: Range<usize>,
(label, help): (Expectation, Cow<'static, str>),
last_token: Option<<&'a str as aott::prelude::InputType>::Token>,
) -> Self {
if let Some(found) = last_token {
Self::Expected {
at: span.into(),
expected: label,
found,
help: Some(help),
}
} else {
let last_data_at = (span.start.saturating_sub(1), span.end.saturating_sub(1)).into();
Self::UnexpectedEof {
at: span.into(),
last_data_at: Some(last_data_at),
expected: Some(label),
help: Some(help),
}
}
}
}
2 changes: 1 addition & 1 deletion protocol/src/model.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@ pub mod packets;
pub mod registry;
mod varint;
use self::registry::RegistryItem;
use aott::primitive::{filter, one_of};
use aott::primitive::filter;
use bytes::BufMut;
use std::{ops::RangeInclusive, ptr};
pub use varint::*;
Expand Down
2 changes: 1 addition & 1 deletion 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::{filter, one_of};
use aott::primitive::filter;
use bytes::BufMut;
use indexmap::IndexMap;

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

mod error;
Expand Down
14 changes: 14 additions & 0 deletions proxy/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
[package]
name = "oxcr_proxy"
version = "0.1.0"
edition = "2021"

[dependencies]
oxcr_protocol.workspace = true
oxcr_cli.workspace = true
tokio.workspace = true
tokio-util.workspace = true
bevy.workspace = true
tracing.workspace = true
tracing-subscriber.workspace = true
futures-util = "0.3.28"
18 changes: 18 additions & 0 deletions proxy/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
use oxcr_protocol::logging::CraftLayer;
use oxcr_protocol::miette::Report;
use tracing::info;
use tracing_subscriber::{
prelude::__tracing_subscriber_SubscriberExt, util::SubscriberInitExt, EnvFilter,
};

#[tokio::main]
async fn main() -> Result<(), Report> {
tracing_subscriber::registry()
.with(EnvFilter::from_env("OXCR_LOG"))
.with(CraftLayer)
.init();

info!("henlo worled me is proxe");

Ok(())
}

0 comments on commit e1178cb

Please sign in to comment.