Skip to content

Commit

Permalink
more cli
Browse files Browse the repository at this point in the history
  • Loading branch information
nothendev committed Sep 28, 2023
1 parent 6fc4e5b commit 85b0598
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 10 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ members = [
"server",
"cli"
]
default-members = [ "protocol", "server" ]
resolver = "2"

[workspace.dependencies]
Expand All @@ -14,3 +15,4 @@ tracing.version = "0"
thiserror.version = "1"
bytes = { version = "1", features = [ "serde" ] }
bevy = { version = "0.11.2", features = ["multi-threaded"], default-features = false }
rayon = "1"
32 changes: 26 additions & 6 deletions cli/src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,10 @@ use std::{marker::PhantomData, path::PathBuf};
use aott::prelude::*;
use oxcr_protocol::{
aott::{self, pfn_type},
bytes::Bytes,
bytes::{BufMut, Bytes, BytesMut},
};

use crate::error::{ParseError, ParseErrorKind};
use crate::error::{Expectation, ParseError, ParseErrorKind};

pub struct Extra<C = ()>(PhantomData<C>);
impl<'a, C> ParserExtras<&'a str> for Extra<C> {
Expand Down Expand Up @@ -39,7 +39,7 @@ pub enum Cli {
Serialization(CliSer),
}

fn radixshit<'a>(radix: u32, cha: char) -> pfn_type!(&'a str, Bytes, Extra) {
fn numbah<'a>(radix: u32, cha: char) -> pfn_type!(&'a str, Bytes, Extra) {
|input| {
just(['0', cha])
.ignore_then(text::int(radix))
Expand All @@ -64,9 +64,29 @@ fn radixshit<'a>(radix: u32, cha: char) -> pfn_type!(&'a str, Bytes, Extra) {

#[parser(extras = Extra)]
fn byte_input(input: &str) -> ByteInput {
choice((radixshit(8, 'o'),))
.map(ByteInput::Data)
.parse_with(input)
choice((
numbah(16, 'x'),
numbah(2, 'b'),
numbah(8, 'o'),
numbah(10, 'd'),
))
.or(text::int(16).try_map_with_span(|x: &str, span| {
x.chars()
.map(|c| {
c.to_digit(16)
.ok_or_else(|| ParseError {
span: span.into(),
kind: ParseErrorKind::Expected {
expected: Expectation::Digit(8),
found: c,
},
})
.map(|x| x.try_into().expect("not a u8"))
})
.try_collect()
}))
.map(ByteInput::Data)
.parse_with(input)
}

#[parser(extras = Extra)]
Expand Down
46 changes: 46 additions & 0 deletions cli/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
use std::ops::Range;

use oxcr_protocol::{
aott,
miette::{self, SourceSpan},
Expand Down Expand Up @@ -33,3 +35,47 @@ pub struct ParseError {
#[source]
pub kind: ParseErrorKind,
}

impl ParseError {
pub fn new(span: Range<usize>, kind: ParseErrorKind) -> Self {
Self {
span: span.into(),
kind,
}
}
}

impl<'a> aott::error::Error<&'a str> for ParseError {
type Span = Range<usize>;

fn unexpected_eof(
span: Self::Span,
_expected: Option<Vec<<&'a str as aott::prelude::InputType>::Token>>,
) -> Self {
Self::new(span, ParseErrorKind::UnexpectedEof)
}

fn expected_token_found(
span: Self::Span,
expected: Vec<char>,
found: aott::MaybeRef<'_, char>,
) -> Self {
Self::new(
span,
ParseErrorKind::Expected {
expected: Expectation::AnyOf(expected),
found: found.into_clone(),
},
)
}

fn expected_eof_found(span: Self::Span, found: aott::MaybeRef<'_, char>) -> Self {
Self::new(
span,
ParseErrorKind::Expected {
expected: Expectation::EndOfInput,
found: found.into_clone(),
},
)
}
}
11 changes: 7 additions & 4 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
#![feature(iterator_try_collect)]

use oxcr_protocol::miette::Report;
use oxcr_protocol::{
miette::{self, bail, Report},
nsfr::when_the_miette,
};

mod cli;
mod error;
Expand All @@ -9,7 +12,7 @@ fn run() -> Result<(), Report> {
let mut args = std::env::args();

match args.next() {
None => panic!("what"),
None => bail!("the"),
Some(path) => {
let arguments: String = args.collect();
Ok(())
Expand All @@ -18,8 +21,8 @@ fn run() -> Result<(), Report> {
}

fn main() {
match run() {
match when_the_miette(run()) {
Ok(()) => {}
Err(e) => {}
Err(_) => {}
}
}

0 comments on commit 85b0598

Please sign in to comment.