paramdef: move row size calc to Paramdef struct

This commit is contained in:
dece 2020-05-18 23:35:18 +02:00
parent 93fec86085
commit 7ab1f763f9
3 changed files with 51 additions and 30 deletions

View file

@ -158,6 +158,23 @@ pub struct Paramdef {
pub fields: Vec<ParamdefField>, pub fields: Vec<ParamdefField>,
} }
impl Paramdef {
pub fn row_size(&self) -> usize {
let mut num_bits: usize = 0;
self.fields.iter().map(|field| {
match field.bit_size() {
0 => field.byte_count as usize,
n => {
num_bits += n;
let num_bytes = num_bits / 8;
num_bits %= 8;
num_bytes
}
}
}).sum()
}
}
pub fn parse(i: &[u8]) -> IResult<&[u8], Paramdef> { pub fn parse(i: &[u8]) -> IResult<&[u8], Paramdef> {
let full_file = i; let full_file = i;
let (i, header) = parse_header(i)?; let (i, header) = parse_header(i)?;

View file

@ -20,41 +20,41 @@ pub fn load_paramdef(paramdef_data: &[u8]) -> Result<paramdef::Paramdef, UnpackE
} }
} }
pub fn print_paramdef(paramdef: &paramdef::Paramdef) { pub fn print_paramdef_intro(paramdef: &paramdef::Paramdef) {
println!("{} -- ver. {} -- format ver. {} -- {} fields", println!(
"{} -- ver. {} -- format ver. {} -- {} fields -- {} per row",
paramdef.header.param_name, paramdef.header.param_name,
paramdef.header.data_version, paramdef.header.format_version, paramdef.header.data_version, paramdef.header.format_version,
paramdef.header.num_fields); paramdef.header.num_fields,
utils_str::n_bytes_pluralise(paramdef.row_size() as i32)
);
}
let mut row_size = 0; pub fn print_paramdef(paramdef: &paramdef::Paramdef) {
let mut bit_count = 0; print_paramdef_intro(paramdef);
for field in &paramdef.fields { for field in &paramdef.fields {
let bit_size = field.bit_size(); let size_str = match field.bit_size() {
let size_str = match bit_size { 0 => utils_str::n_bytes_pluralise(field.byte_count as i32),
0 => utils_str::n_plural(field.byte_count as i32, "byte", "bytes"), x => utils_str::n_pluralise(x as i32, "bit", "bits")
x => utils_str::n_plural(x as i32, "bit", "bits")
}; };
println!(
println!(" - [{}] {} ({}) {} ({}, {})", " - [{}] {} ({}) {} ({}, {})",
field.sort_id, field.display_name, field.sort_id,
field.display_name,
field.internal_name.as_ref().unwrap_or(&String::from("<noname>")), field.internal_name.as_ref().unwrap_or(&String::from("<noname>")),
field.display_type, field.internal_type, size_str); field.display_type,
println!(" Values: default {}, range [{}, {}], inc {}", field.internal_type,
field.default_value, field.min_value, field.max_value, field.increment); size_str
);
println!(
" Values: default {}, range [{}, {}], inc {}",
field.default_value,
field.min_value,
field.max_value,
field.increment
);
if let Some(desc) = &field.description { if let Some(desc) = &field.description {
println!(" Description: {}", desc); println!(" Description: {}", desc);
} }
if bit_size != 0 {
bit_count += bit_size;
while bit_count >= 8 {
bit_count -= 8;
row_size += 1;
} }
} else {
row_size += field.byte_count;
}
}
println!("Size per row: {}", utils_str::n_plural(row_size as i32, "byte", "bytes"));
} }

View file

@ -1,10 +1,14 @@
pub fn plural<'a>(num: i32, singular_name: &'a str, plural_name: &'a str) -> &'a str { pub fn pluralise<'a>(num: i32, singular_name: &'a str, plural_name: &'a str) -> &'a str {
match num { match num {
1 => singular_name, 1 => singular_name,
_ => plural_name, _ => plural_name,
} }
} }
pub fn n_plural<'a>(num: i32, singular_name: &'a str, plural_name: &'a str) -> String { pub fn n_pluralise<'a>(num: i32, singular_name: &'a str, plural_name: &'a str) -> String {
format!("{} {}", num, plural(num, singular_name, plural_name)) format!("{} {}", num, pluralise(num, singular_name, plural_name))
}
pub fn n_bytes_pluralise(num: i32) -> String {
n_pluralise(num, "byte", "bytes")
} }