mirror of
https://github.com/Noratrieb/haesli.git
synced 2026-01-14 11:45:02 +01:00
fixed all errors kind of
This commit is contained in:
parent
5dc33f0dab
commit
30f9070cca
5 changed files with 1760 additions and 1478 deletions
|
|
@ -143,12 +143,14 @@ fn assert_check(assert: &Assert, type_name: &str, var_name: &str) {
|
|||
match &*assert.check {
|
||||
"notnull" => match type_name {
|
||||
"shortstr" | "longstr" => {
|
||||
let cause = "string was null";
|
||||
println!(r#" if {var_name}.is_empty() {{ fail!("{cause}") }}"#);
|
||||
println!(
|
||||
r#" if {var_name}.is_empty() {{ fail!("string was null for field {var_name}") }}"#
|
||||
);
|
||||
}
|
||||
"short" => {
|
||||
let cause = "number was 0";
|
||||
println!(r#" if {var_name} == 0 {{ fail!("{cause}") }}"#);
|
||||
println!(
|
||||
r#" if {var_name} == 0 {{ fail!("number was 0 for field {var_name}") }}"#
|
||||
);
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
},
|
||||
|
|
@ -157,13 +159,13 @@ fn assert_check(assert: &Assert, type_name: &str, var_name: &str) {
|
|||
println!(
|
||||
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}") }}"#);
|
||||
}
|
||||
"le" => {} // can't validate this here
|
||||
"length" => {
|
||||
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}") }}"#);
|
||||
}
|
||||
_ => unimplemented!(),
|
||||
|
|
|
|||
File diff suppressed because it is too large
Load diff
|
|
@ -140,7 +140,6 @@ pub fn timestamp(input: &[u8]) -> IResult<Timestamp> {
|
|||
|
||||
pub fn table(input: &[u8]) -> IResult<Table> {
|
||||
let (input, len) = u32(Big)(input)?;
|
||||
|
||||
let (input, values) = count(table_value_pair, usize::try_from(len).unwrap())(input)?;
|
||||
let table = HashMap::from_iter(values.into_iter());
|
||||
Ok((input, table))
|
||||
|
|
@ -215,9 +214,14 @@ fn field_value(input: &[u8]) -> IResult<FieldValue> {
|
|||
number!(b"T", timestamp, u64(Big), Timestamp, R);
|
||||
|
||||
fn field_table(input: &[u8]) -> R {
|
||||
let (input, _) = tag("F")(input)?;
|
||||
table(input).map(|(input, value)| (input, FieldValue::FieldTable(value)))
|
||||
}
|
||||
|
||||
fn void(input: &[u8]) -> R {
|
||||
tag("V")(input).map(|(input, _)| (input, FieldValue::Void))
|
||||
}
|
||||
|
||||
alt((
|
||||
boolean,
|
||||
short_short_int,
|
||||
|
|
@ -235,5 +239,7 @@ fn field_value(input: &[u8]) -> IResult<FieldValue> {
|
|||
long_str,
|
||||
field_array,
|
||||
timestamp,
|
||||
field_table,
|
||||
void,
|
||||
))(input)
|
||||
}
|
||||
|
|
|
|||
|
|
@ -19,7 +19,7 @@ impl<R: Rng> RandomMethod<R> for String {
|
|||
|
||||
impl<R: Rng, T: RandomMethod<R>> RandomMethod<R> for Vec<T> {
|
||||
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);
|
||||
(0..len).for_each(|_| vec.push(RandomMethod::random(rng)));
|
||||
vec
|
||||
|
|
@ -97,14 +97,12 @@ fn pack_many_bits() {
|
|||
assert_eq!(bits.as_slice(), parsed_bits.as_slice());
|
||||
}
|
||||
|
||||
#[ignore]
|
||||
#[test]
|
||||
fn random_ser_de() {
|
||||
const ITERATIONS: usize = 1000;
|
||||
const ITERATIONS: usize = 100000;
|
||||
let mut rng = rand::rngs::StdRng::from_seed([0; 32]);
|
||||
|
||||
for _ in 0..ITERATIONS {
|
||||
println!("iter");
|
||||
let class = Class::random(&mut rng);
|
||||
let mut bytes = Vec::new();
|
||||
|
||||
|
|
@ -137,6 +135,7 @@ fn nested_table() {
|
|||
FieldValue::Boolean(true),
|
||||
)])),
|
||||
)]);
|
||||
eprintln!("{table:?}");
|
||||
|
||||
let mut bytes = Vec::new();
|
||||
crate::classes::write_helper::table(table.clone(), &mut bytes).unwrap();
|
||||
|
|
|
|||
|
|
@ -125,22 +125,19 @@ fn server_properties(host: SocketAddr) -> classes::Table {
|
|||
}
|
||||
|
||||
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)
|
||||
} else {
|
||||
FieldValue::LongString(host_str.into())
|
||||
};
|
||||
|
||||
// todo: fix
|
||||
|
||||
//HashMap::from([
|
||||
// ("host".to_string(), host_value),
|
||||
// ("product".to_string(), ss("no name yet")),
|
||||
// ("version".to_string(), ss("0.1.0")),
|
||||
// ("platform".to_string(), ss("microsoft linux")),
|
||||
// ("copyright".to_string(), ss("MIT")),
|
||||
// ("information".to_string(), ss("hello reader")),
|
||||
// ("uwu".to_string(), ss("owo")),
|
||||
//])
|
||||
HashMap::new()
|
||||
HashMap::from([
|
||||
("host".to_string(), host_value),
|
||||
("product".to_string(), ss("no name yet")),
|
||||
("version".to_string(), ss("0.1.0")),
|
||||
("platform".to_string(), ss("microsoft linux")),
|
||||
("copyright".to_string(), ss("MIT")),
|
||||
("information".to_string(), ss("hello reader")),
|
||||
("uwu".to_string(), ss("owo")),
|
||||
])
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue