diff options
author | SoniEx2 <endermoneymod@gmail.com> | 2021-11-11 20:29:55 -0300 |
---|---|---|
committer | SoniEx2 <endermoneymod@gmail.com> | 2021-11-11 20:29:55 -0300 |
commit | d4724b4734776d32fb86cd3c932e18fc41b68316 (patch) | |
tree | f289b70f1aa1b971d76737d702ea112a67d9c8c2 /tests/common |
Start porting Brigadier to Rust
Diffstat (limited to 'tests/common')
-rw-r--r-- | tests/common/errorfunc.rs | 89 | ||||
-rw-r--r-- | tests/common/errorpanic.rs | 114 | ||||
-rw-r--r-- | tests/common/mod.rs | 12 |
3 files changed, 215 insertions, 0 deletions
diff --git a/tests/common/errorfunc.rs b/tests/common/errorfunc.rs new file mode 100644 index 0000000..99ba641 --- /dev/null +++ b/tests/common/errorfunc.rs @@ -0,0 +1,89 @@ +// Copyright (c) 2021 Soni L. + +use ::std::marker::PhantomData; + +use ::iosonism::strcursor::ReadError; +use ::iosonism::strcursor::StringReader; + +/// An error callback. +pub trait ErrorFunc<'a, C: StringReader<'a>> { + fn call<'b>(context: &C, ty: ErrorType<'b>); +} + +/// An implementation of various Iosonism errors that calls T. +pub struct ErrorCall<T>(PhantomData<T>); + +#[non_exhaustive] +#[derive(PartialEq, Eq, Debug)] +pub enum ErrorType<'a> { + InvalidInteger(&'a str), + ExpectedInteger, + InvalidFloat(&'a str), + ExpectedFloat, + InvalidBool(&'a str), + ExpectedBool, + ExpectedStartOfQuote, + ExpectedEndOfQuote, + InvalidEscape(&'a str), + ExpectedSymbol(&'a str), +} + +impl<T> ::std::fmt::Display for ErrorCall<T> { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + write!(f, "error!") + } +} + +impl<T> ::std::fmt::Debug for ErrorCall<T> { + fn fmt(&self, f: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + write!(f, "ErrorCall") + } +} + +impl<T> ::std::error::Error for ErrorCall<T> { +} + +impl<'a, C: StringReader<'a>, T> ReadError<'a, C> for ErrorCall<T> +where T: ErrorFunc<'a, C> { + fn invalid_integer(context: &C, from: &str) -> Self { + T::call(context, ErrorType::InvalidInteger(from)); + Self(PhantomData) + } + fn expected_integer(context: &C) -> Self { + T::call(context, ErrorType::ExpectedInteger); + Self(PhantomData) + } + fn invalid_float(context: &C, from: &str) -> Self { + T::call(context, ErrorType::InvalidFloat(from)); + Self(PhantomData) + } + fn expected_float(context: &C) -> Self { + T::call(context, ErrorType::ExpectedFloat); + Self(PhantomData) + } + fn invalid_bool(context: &C, from: &str) -> Self { + T::call(context, ErrorType::InvalidBool(from)); + Self(PhantomData) + } + fn expected_bool(context: &C) -> Self { + T::call(context, ErrorType::ExpectedBool); + Self(PhantomData) + } + fn expected_start_of_quote(context: &C) -> Self { + T::call(context, ErrorType::ExpectedStartOfQuote); + Self(PhantomData) + } + fn expected_end_of_quote(context: &C) -> Self { + T::call(context, ErrorType::ExpectedEndOfQuote); + Self(PhantomData) + } + fn invalid_escape(context: &C, from: &str) -> Self { + T::call(context, ErrorType::InvalidEscape(from)); + Self(PhantomData) + } + fn expected_symbol(context: &C, from: &str) -> Self { + T::call(context, ErrorType::ExpectedSymbol(from)); + Self(PhantomData) + } +} + diff --git a/tests/common/errorpanic.rs b/tests/common/errorpanic.rs new file mode 100644 index 0000000..202c4be --- /dev/null +++ b/tests/common/errorpanic.rs @@ -0,0 +1,114 @@ +// Copyright (c) 2021 Soni L. + +use ::iosonism::strcursor::ReadError; +use ::iosonism::strcursor::StringReader; + +/// An implementation of various Iosonism errors that just panics. +#[derive(Debug)] +pub enum ErrorPanic { + // uninhabitable! +} + +impl ::std::fmt::Display for ErrorPanic { + fn fmt(&self, _: &mut ::std::fmt::Formatter) -> ::std::fmt::Result { + match *self { + } + } +} + +impl ::std::error::Error for ErrorPanic { +} + +impl<'a, C: StringReader<'a>> ReadError<'a, C> for ErrorPanic { + fn invalid_integer(context: &C, from: &str) -> Self { + if !context.get_remaining().is_empty() { + panic!( + "invalid integer: {} at ...{}", + from, + context.get_remaining(), + ); + } else { + panic!("invalid integer: {}", from); + } + } + fn expected_integer(context: &C) -> Self { + if !context.get_remaining().is_empty() { + panic!("expected integer at ...{}", context.get_remaining()); + } else { + panic!("expected integer"); + } + } + fn invalid_float(context: &C, from: &str) -> Self { + if !context.get_remaining().is_empty() { + panic!( + "invalid float: {} at ...{}", + from, + context.get_remaining(), + ); + } else { + panic!("invalid float: {}", from); + } + } + fn expected_float(context: &C) -> Self { + if !context.get_remaining().is_empty() { + panic!("expected float at ...{}", context.get_remaining()); + } else { + panic!("expected float"); + } + } + fn invalid_bool(context: &C, from: &str) -> Self { + if !context.get_remaining().is_empty() { + panic!( + "invalid bool: {} at ...{}", + from, + context.get_remaining(), + ); + } else { + panic!("invalid bool: {}", from); + } + } + fn expected_bool(context: &C) -> Self { + if !context.get_remaining().is_empty() { + panic!("expected bool at ...{}", context.get_remaining()); + } else { + panic!("expected bool"); + } + } + fn expected_start_of_quote(context: &C) -> Self { + if !context.get_remaining().is_empty() { + panic!("expected start of quote at ...{}", context.get_remaining()); + } else { + panic!("expected start of quote"); + } + } + fn expected_end_of_quote(context: &C) -> Self { + if !context.get_remaining().is_empty() { + panic!("expected end of quote at ...{}", context.get_remaining()); + } else { + panic!("expected end of quote"); + } + } + fn invalid_escape(context: &C, from: &str) -> Self { + if !context.get_remaining().is_empty() { + panic!( + "invalid escape: {} at ...{}", + from, + context.get_remaining(), + ); + } else { + panic!("invalid escape: {}", from); + } + } + fn expected_symbol(context: &C, from: &str) -> Self { + if !context.get_remaining().is_empty() { + panic!( + "expected symbol: {} at ...{}", + from, + context.get_remaining(), + ); + } else { + panic!("expected symbol: {}", from); + } + } +} + diff --git a/tests/common/mod.rs b/tests/common/mod.rs new file mode 100644 index 0000000..6760da1 --- /dev/null +++ b/tests/common/mod.rs @@ -0,0 +1,12 @@ +// Copyright (c) 2021 Soni L. + +// see rationale in tests/*.rs +#![warn(non_snake_case)] + +pub mod errorfunc; +pub mod errorpanic; + +pub use self::errorfunc::ErrorCall; +pub use self::errorfunc::ErrorFunc; +pub use self::errorfunc::ErrorType; +pub use self::errorpanic::ErrorPanic; |