Dsl and some simple handlers
This commit is contained in:
commit
9ac06199bd
2
.cargo/config.toml
Normal file
2
.cargo/config.toml
Normal file
@ -0,0 +1,2 @@
|
||||
[registries]
|
||||
landgrafhomyak = { index = "sparse+https://cargo.landgrafhomyak.ru/" }
|
||||
3
.gitignore
vendored
Normal file
3
.gitignore
vendored
Normal file
@ -0,0 +1,3 @@
|
||||
/.idea/
|
||||
Cargo.lock
|
||||
target/
|
||||
14
Cargo.toml
Normal file
14
Cargo.toml
Normal file
@ -0,0 +1,14 @@
|
||||
[package]
|
||||
name = "json-decoder-api-0"
|
||||
edition = "2024"
|
||||
|
||||
|
||||
[lints]
|
||||
rust = { nonstandard_style = "allow" }
|
||||
|
||||
[workspace]
|
||||
#members = ["macro", "macro/parser"]
|
||||
|
||||
[patch.landgrafhomyak]
|
||||
json-decoder-api-0-macro-0 = { path = "macro" }
|
||||
json-decoder-api-0-macro-0-parser-0 = { path = "macro/parser" }
|
||||
60
src/default_handlers/enums.rs
Normal file
60
src/default_handlers/enums.rs
Normal file
@ -0,0 +1,60 @@
|
||||
use crate::dsl::{BooleanHandler, FloatHandler, IntegerHandler, NoValueHandler, NullHandler};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
pub struct Handler_Boolean<S, StoreFn: Fn(&mut S, bool)> {
|
||||
store: StoreFn,
|
||||
__phantom: PhantomData<S>,
|
||||
}
|
||||
impl<S, StoreFn: Fn(&mut S, bool)> Handler_Boolean<S, StoreFn> {
|
||||
pub fn create(f: StoreFn) -> Self {
|
||||
return Self {
|
||||
store: f,
|
||||
__phantom: PhantomData::default(),
|
||||
};
|
||||
}
|
||||
}
|
||||
impl<S, StoreFn: Fn(&mut S, bool)> BooleanHandler<S> for Handler_Boolean<S, StoreFn> {
|
||||
fn handleBoolean(&self, dst: &mut S, value: bool) {
|
||||
(self.store)(dst, value);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Handler_Null<S, StoreFn: Fn(&mut S)> {
|
||||
store: StoreFn,
|
||||
__phantom: PhantomData<S>,
|
||||
}
|
||||
|
||||
impl<S, StoreFn: Fn(&mut S)> Handler_Null<S, StoreFn> {
|
||||
pub fn create(f: StoreFn) -> Self {
|
||||
return Self {
|
||||
store: f,
|
||||
__phantom: PhantomData::default(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, StoreFn: Fn(&mut S)> NullHandler<S> for Handler_Null<S, StoreFn> {
|
||||
fn handleNull(&self, dst: &mut S) {
|
||||
(self.store)(dst);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Handler_NoValue<S, StoreFn: Fn(&mut S)> {
|
||||
store: StoreFn,
|
||||
__phantom: PhantomData<S>,
|
||||
}
|
||||
|
||||
impl<S, StoreFn: Fn(&mut S)> Handler_NoValue<S, StoreFn> {
|
||||
pub fn create(f: StoreFn) -> Self {
|
||||
return Self {
|
||||
store: f,
|
||||
__phantom: PhantomData::default(),
|
||||
};
|
||||
}
|
||||
}
|
||||
|
||||
impl<S, StoreFn: Fn(&mut S)> NoValueHandler<S> for Handler_NoValue<S, StoreFn> {
|
||||
fn handleNoValue(&self, dst: &mut S) {
|
||||
(self.store)(dst);
|
||||
}
|
||||
}
|
||||
38
src/default_handlers/floats.rs
Normal file
38
src/default_handlers/floats.rs
Normal file
@ -0,0 +1,38 @@
|
||||
use crate::dsl::{FloatHandler, IntegerHandler};
|
||||
use std::marker::PhantomData;
|
||||
|
||||
pub struct Handler_F64<S, StoreFn: Fn(&mut S, f64)> {
|
||||
store: StoreFn,
|
||||
__phantom: PhantomData<S>,
|
||||
}
|
||||
impl<S, StoreFn: Fn(&mut S, f64)> Handler_F64<S, StoreFn> {
|
||||
pub fn create(f: StoreFn) -> Self {
|
||||
return Self {
|
||||
store: f,
|
||||
__phantom: PhantomData::default(),
|
||||
};
|
||||
}
|
||||
}
|
||||
impl<S, StoreFn: Fn(&mut S, f64)> FloatHandler<S> for Handler_F64<S, StoreFn> {
|
||||
fn handleFloat(&self, dst: &mut S, value: f64) {
|
||||
(self.store)(dst, value);
|
||||
}
|
||||
}
|
||||
|
||||
pub struct Handler_F32<S, StoreFn: Fn(&mut S, f32)> {
|
||||
store: StoreFn,
|
||||
__phantom: PhantomData<S>,
|
||||
}
|
||||
impl<S, StoreFn: Fn(&mut S, f32)> Handler_F32<S, StoreFn> {
|
||||
pub fn create(f: StoreFn) -> Self {
|
||||
return Self {
|
||||
store: f,
|
||||
__phantom: PhantomData::default(),
|
||||
};
|
||||
}
|
||||
}
|
||||
impl<S, StoreFn: Fn(&mut S, f32)> FloatHandler<S> for Handler_F32<S, StoreFn> {
|
||||
fn handleFloat(&self, dst: &mut S, value: f64) {
|
||||
(self.store)(dst, value as f32);
|
||||
}
|
||||
}
|
||||
56
src/default_handlers/integers.rs
Normal file
56
src/default_handlers/integers.rs
Normal file
@ -0,0 +1,56 @@
|
||||
use crate::dsl::IntegerHandler;
|
||||
use std::marker::PhantomData;
|
||||
|
||||
macro_rules! defineIntegerHandler {
|
||||
($vis:vis $name:ident < $int_type:ty >) => {
|
||||
$vis struct $name <S, StoreFn: Fn(&mut S, $int_type)> {
|
||||
store: StoreFn,
|
||||
__phantom: PhantomData<S>
|
||||
}
|
||||
|
||||
impl <S, StoreFn: Fn(&mut S, $int_type)> $name <S, StoreFn> {
|
||||
pub fn create(f: StoreFn) -> Self {
|
||||
return Self {
|
||||
store: f,
|
||||
__phantom: PhantomData::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl <S, StoreFn: Fn(&mut S, $int_type)> IntegerHandler<S> for $name <S, StoreFn> {
|
||||
fn handleFixedInteger(&self, dst: &mut S, value: u64, isNegative: bool) {
|
||||
let downcasted: $int_type;
|
||||
|
||||
if isNegative {
|
||||
if <$int_type>::MIN >= 0 || value > (<$int_type>::abs_diff(0, <$int_type>::MIN)) as u64 {
|
||||
panic!(
|
||||
"Integer underflow: expected in range {}..={}, got -{}",
|
||||
<$int_type>::MIN, <$int_type>::MAX, value
|
||||
);
|
||||
}
|
||||
downcasted = i64::strict_sub_unsigned(0i64, value as u64) as $int_type;
|
||||
} else {
|
||||
if value > (<$int_type>::MAX as u64) {
|
||||
panic!(
|
||||
"Integer overflow: expected in range {}..={}, got {}",
|
||||
<$int_type>::MIN, <$int_type>::MAX, value
|
||||
);
|
||||
}
|
||||
downcasted = value as $int_type;
|
||||
}
|
||||
|
||||
(self.store)(dst, downcasted);
|
||||
return;
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
defineIntegerHandler!( pub Handler_U8<u8> );
|
||||
defineIntegerHandler!( pub Handler_S8<i8> );
|
||||
defineIntegerHandler!( pub Handler_U16<u16> );
|
||||
defineIntegerHandler!( pub Handler_S16<i16> );
|
||||
defineIntegerHandler!( pub Handler_U32<u32> );
|
||||
defineIntegerHandler!( pub Handler_S32<i32> );
|
||||
defineIntegerHandler!( pub Handler_U64<u64> );
|
||||
defineIntegerHandler!( pub Handler_S64<i64> );
|
||||
7
src/default_handlers/mod.rs
Normal file
7
src/default_handlers/mod.rs
Normal file
@ -0,0 +1,7 @@
|
||||
mod integers;
|
||||
mod floats;
|
||||
mod enums;
|
||||
|
||||
pub use integers::*;
|
||||
pub use floats::*;
|
||||
pub use enums::*;
|
||||
88
src/dsl.rs
Normal file
88
src/dsl.rs
Normal file
@ -0,0 +1,88 @@
|
||||
pub trait IntegerHandler<S>: Sized {
|
||||
fn handleFixedInteger(&self, dst: &mut S, value: u64, isNegative: bool);
|
||||
}
|
||||
|
||||
pub trait FloatHandler<S>: Sized {
|
||||
fn handleFloat(&self, dst: &mut S, value: f64);
|
||||
}
|
||||
|
||||
pub trait BooleanHandler<S>: Sized {
|
||||
fn handleBoolean(&self, dst: &mut S, value: bool);
|
||||
}
|
||||
|
||||
pub trait NullHandler<S>: Sized {
|
||||
fn handleNull(&self, dst: &mut S);
|
||||
}
|
||||
|
||||
pub trait NoValueHandler<S>: Sized {
|
||||
fn handleNoValue(&self, dst: &mut S);
|
||||
}
|
||||
|
||||
pub trait ObjectHandler<P, S>: Sized {
|
||||
fn produceObject(&self) -> S;
|
||||
fn storeObject(&self, dst: &mut P, value: S);
|
||||
}
|
||||
|
||||
pub trait StringHandler<P, T>: Sized {
|
||||
fn handleString(&self, dst: &mut P, value: T);
|
||||
}
|
||||
|
||||
pub trait ObjectConfigurationScope<S> {
|
||||
type K;
|
||||
|
||||
fn ignoreKey_required(&mut self, name: &Self::K);
|
||||
fn ignoreKey_optional(&mut self, name: &Self::K);
|
||||
fn processKey(&mut self, name: &Self::K, config: impl ValueConfiguration<S>);
|
||||
fn processKey_optional(
|
||||
&mut self,
|
||||
name: &Self::K,
|
||||
config: impl ValueConfiguration<S>,
|
||||
fallback: impl NoValueHandler<S>,
|
||||
);
|
||||
}
|
||||
pub trait ObjectConfiguration<P>: Sized {
|
||||
type S: Sized;
|
||||
fn configureObject(
|
||||
&self,
|
||||
scope: &impl ArrayConfigurationScope<Self::S>,
|
||||
) -> impl ObjectHandler<P, Self::S>;
|
||||
}
|
||||
|
||||
pub trait ArrayHandler<P, S>: Sized {
|
||||
fn produceArray(&self) -> S;
|
||||
fn storeArray(&self, dst: &mut P, value: S);
|
||||
}
|
||||
|
||||
pub trait ArrayConfigurationScope<S> {
|
||||
fn ignoreElements_required(&self, start: usize, endInclusive: Option<usize>);
|
||||
fn ignoreElements_optional(&self, start: usize, endInclusive: Option<usize>);
|
||||
fn processElements_required(
|
||||
&self,
|
||||
start: usize,
|
||||
endInclusive: Option<usize>,
|
||||
config: impl ValueConfiguration<S>,
|
||||
);
|
||||
fn processElements_optional(
|
||||
&self,
|
||||
start: usize,
|
||||
endInclusive: Option<usize>,
|
||||
config: impl ValueConfiguration<S>,
|
||||
);
|
||||
}
|
||||
|
||||
pub trait ArrayConfiguration<P>: Sized {
|
||||
type S: Sized;
|
||||
fn configureArray(
|
||||
&self,
|
||||
scope: &impl ArrayConfigurationScope<Self::S>,
|
||||
) -> impl ObjectHandler<P, Self::S>;
|
||||
}
|
||||
|
||||
pub trait ValueConfiguration<P>: Sized {
|
||||
fn integerHandler(&self) -> Option<impl IntegerHandler<P>>;
|
||||
fn floatHandler(&self) -> Option<impl FloatHandler<P>>;
|
||||
fn booleanHandler(&self) -> Option<impl BooleanHandler<P>>;
|
||||
fn nullHandler(&self) -> Option<impl NullHandler<P>>;
|
||||
fn arrayHandler(&self) -> Option<impl ObjectConfiguration<P>>;
|
||||
fn objectHandler(&self) -> Option<impl ArrayConfiguration<P>>;
|
||||
}
|
||||
2
src/lib.rs
Normal file
2
src/lib.rs
Normal file
@ -0,0 +1,2 @@
|
||||
pub mod dsl;
|
||||
pub mod default_handlers;
|
||||
Loading…
Reference in New Issue
Block a user