fixed all errors kind of

This commit is contained in:
nora 2022-02-13 23:29:29 +01:00
parent 5dc33f0dab
commit 30f9070cca
5 changed files with 1760 additions and 1478 deletions

View file

@ -143,12 +143,14 @@ fn assert_check(assert: &Assert, type_name: &str, var_name: &str) {
match &*assert.check { match &*assert.check {
"notnull" => match type_name { "notnull" => match type_name {
"shortstr" | "longstr" => { "shortstr" | "longstr" => {
let cause = "string was null"; println!(
println!(r#" if {var_name}.is_empty() {{ fail!("{cause}") }}"#); r#" if {var_name}.is_empty() {{ fail!("string was null for field {var_name}") }}"#
);
} }
"short" => { "short" => {
let cause = "number was 0"; println!(
println!(r#" if {var_name} == 0 {{ fail!("{cause}") }}"#); r#" if {var_name} == 0 {{ fail!("number was 0 for field {var_name}") }}"#
);
} }
_ => unimplemented!(), _ => unimplemented!(),
}, },
@ -157,13 +159,13 @@ fn assert_check(assert: &Assert, type_name: &str, var_name: &str) {
println!( println!(
r#" static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"{value}").unwrap());"# r#" static REGEX: Lazy<Regex> = Lazy::new(|| Regex::new(r"{value}").unwrap());"#
); );
let cause = format!("regex `{value}` did not match value"); let cause = format!("regex `{value}` did not match value for field {var_name}");
println!(r#" if !REGEX.is_match(&{var_name}) {{ fail!(r"{cause}") }}"#); println!(r#" if !REGEX.is_match(&{var_name}) {{ fail!(r"{cause}") }}"#);
} }
"le" => {} // can't validate this here "le" => {} // can't validate this here
"length" => { "length" => {
let length = assert.value.as_ref().unwrap(); let length = assert.value.as_ref().unwrap();
let cause = format!("value is shorter than {length}"); let cause = format!("value is shorter than {length} for field {var_name}");
println!(r#" if {var_name}.len() > {length} {{ fail!("{cause}") }}"#); println!(r#" if {var_name}.len() > {length} {{ fail!("{cause}") }}"#);
} }
_ => unimplemented!(), _ => unimplemented!(),

File diff suppressed because it is too large Load diff

View file

@ -140,7 +140,6 @@ pub fn timestamp(input: &[u8]) -> IResult<Timestamp> {
pub fn table(input: &[u8]) -> IResult<Table> { pub fn table(input: &[u8]) -> IResult<Table> {
let (input, len) = u32(Big)(input)?; let (input, len) = u32(Big)(input)?;
let (input, values) = count(table_value_pair, usize::try_from(len).unwrap())(input)?; let (input, values) = count(table_value_pair, usize::try_from(len).unwrap())(input)?;
let table = HashMap::from_iter(values.into_iter()); let table = HashMap::from_iter(values.into_iter());
Ok((input, table)) Ok((input, table))
@ -215,9 +214,14 @@ fn field_value(input: &[u8]) -> IResult<FieldValue> {
number!(b"T", timestamp, u64(Big), Timestamp, R); number!(b"T", timestamp, u64(Big), Timestamp, R);
fn field_table(input: &[u8]) -> R { fn field_table(input: &[u8]) -> R {
let (input, _) = tag("F")(input)?;
table(input).map(|(input, value)| (input, FieldValue::FieldTable(value))) table(input).map(|(input, value)| (input, FieldValue::FieldTable(value)))
} }
fn void(input: &[u8]) -> R {
tag("V")(input).map(|(input, _)| (input, FieldValue::Void))
}
alt(( alt((
boolean, boolean,
short_short_int, short_short_int,
@ -235,5 +239,7 @@ fn field_value(input: &[u8]) -> IResult<FieldValue> {
long_str, long_str,
field_array, field_array,
timestamp, timestamp,
field_table,
void,
))(input) ))(input)
} }

View file

@ -19,7 +19,7 @@ impl<R: Rng> RandomMethod<R> for String {
impl<R: Rng, T: RandomMethod<R>> RandomMethod<R> for Vec<T> { impl<R: Rng, T: RandomMethod<R>> RandomMethod<R> for Vec<T> {
fn random(rng: &mut R) -> Self { fn random(rng: &mut R) -> Self {
let len = rng.gen_range(0_usize..10); let len = rng.gen_range(1_usize..10);
let mut vec = Vec::with_capacity(len); let mut vec = Vec::with_capacity(len);
(0..len).for_each(|_| vec.push(RandomMethod::random(rng))); (0..len).for_each(|_| vec.push(RandomMethod::random(rng)));
vec vec
@ -97,14 +97,12 @@ fn pack_many_bits() {
assert_eq!(bits.as_slice(), parsed_bits.as_slice()); assert_eq!(bits.as_slice(), parsed_bits.as_slice());
} }
#[ignore]
#[test] #[test]
fn random_ser_de() { fn random_ser_de() {
const ITERATIONS: usize = 1000; const ITERATIONS: usize = 100000;
let mut rng = rand::rngs::StdRng::from_seed([0; 32]); let mut rng = rand::rngs::StdRng::from_seed([0; 32]);
for _ in 0..ITERATIONS { for _ in 0..ITERATIONS {
println!("iter");
let class = Class::random(&mut rng); let class = Class::random(&mut rng);
let mut bytes = Vec::new(); let mut bytes = Vec::new();
@ -137,6 +135,7 @@ fn nested_table() {
FieldValue::Boolean(true), FieldValue::Boolean(true),
)])), )])),
)]); )]);
eprintln!("{table:?}");
let mut bytes = Vec::new(); let mut bytes = Vec::new();
crate::classes::write_helper::table(table.clone(), &mut bytes).unwrap(); crate::classes::write_helper::table(table.clone(), &mut bytes).unwrap();

View file

@ -125,22 +125,19 @@ fn server_properties(host: SocketAddr) -> classes::Table {
} }
let host_str = host.ip().to_string(); let host_str = host.ip().to_string();
let _host_value = if host_str.len() < 256 { let host_value = if host_str.len() < 256 {
FieldValue::ShortString(host_str) FieldValue::ShortString(host_str)
} else { } else {
FieldValue::LongString(host_str.into()) FieldValue::LongString(host_str.into())
}; };
// todo: fix HashMap::from([
("host".to_string(), host_value),
//HashMap::from([ ("product".to_string(), ss("no name yet")),
// ("host".to_string(), host_value), ("version".to_string(), ss("0.1.0")),
// ("product".to_string(), ss("no name yet")), ("platform".to_string(), ss("microsoft linux")),
// ("version".to_string(), ss("0.1.0")), ("copyright".to_string(), ss("MIT")),
// ("platform".to_string(), ss("microsoft linux")), ("information".to_string(), ss("hello reader")),
// ("copyright".to_string(), ss("MIT")), ("uwu".to_string(), ss("owo")),
// ("information".to_string(), ss("hello reader")), ])
// ("uwu".to_string(), ss("owo")),
//])
HashMap::new()
} }