tauri dev watches shared, VarFormat::format

This commit is contained in:
Martin Kavík 2024-06-09 01:38:44 +02:00
parent a6da2887c9
commit d0188c0ad7
3 changed files with 104 additions and 16 deletions

View file

@ -1,2 +1,3 @@
/*
!/src-tauri
!/shared

View file

@ -15,8 +15,10 @@ origins = ["*"]
frontend = [
"public",
"frontend/Cargo.toml",
"frontend/typescript/bundles",
"frontend/src",
"frontend/typescript/bundles",
"shared/Cargo.toml",
"shared/src",
]
backend = [
"backend/Cargo.toml",

View file

@ -3,9 +3,10 @@ pub enum VarFormat {
ASCII,
Binary,
BinaryWithGroups,
#[default]
// #[default]
Hexadecimal,
Octal,
#[default]
Signed,
Unsigned,
}
@ -18,8 +19,8 @@ impl VarFormat {
VarFormat::BinaryWithGroups => "Bins",
VarFormat::Hexadecimal => "Hex",
VarFormat::Octal => "Oct",
VarFormat::Signed => "i32",
VarFormat::Unsigned => "u32",
VarFormat::Signed => "Int",
VarFormat::Unsigned => "UInt",
}
}
@ -38,17 +39,101 @@ impl VarFormat {
pub fn format(&self, value: wellen::SignalValue) -> String {
// @TODO optimize it by not using `.to_string` if possible
let value = value.to_string();
let ones_and_zeros = value
.chars()
.rev()
.map(|char| char.to_digit(2).unwrap())
.collect::<Vec<_>>();
let mut base = convert_base::Convert::new(2, 16);
let output = base.convert::<u32, u32>(&ones_and_zeros);
let value: String = output
.into_iter()
.map(|number| char::from_digit(number, 16).unwrap())
.collect();
value
if value.is_empty() {
return value;
}
match self {
VarFormat::ASCII => {
// @TODO
value
},
VarFormat::Binary => {
value
},
VarFormat::BinaryWithGroups => {
// @TODO
value
},
VarFormat::Hexadecimal => {
let ones_and_zeros = value
.chars()
.rev()
.map(|char| char.to_digit(2).unwrap())
.collect::<Vec<_>>();
let mut base = convert_base::Convert::new(2, 16);
let output = base.convert::<u32, u32>(&ones_and_zeros);
let value: String = output
.into_iter()
.rev()
.map(|number| char::from_digit(number, 16).unwrap())
.collect();
value
},
VarFormat::Octal => {
let ones_and_zeros = value
.chars()
.rev()
.map(|char| char.to_digit(2).unwrap())
.collect::<Vec<_>>();
let mut base = convert_base::Convert::new(2, 8);
let output = base.convert::<u32, u32>(&ones_and_zeros);
let value: String = output
.into_iter()
.rev()
.map(|number| char::from_digit(number, 8).unwrap())
.collect();
value
},
VarFormat::Signed => {
let mut ones_and_zeros = value
.chars()
.rev()
.map(|char| char.to_digit(2).unwrap())
.collect::<Vec<_>>();
// https://builtin.com/articles/twos-complement
let sign = if ones_and_zeros.last().unwrap() == &0 { "" } else { "-" };
if sign == "-" {
let mut one_found = false;
for one_or_zero in &mut ones_and_zeros {
if one_found {
*one_or_zero = if one_or_zero == &0 {
1
} else {
0
}
} else if one_or_zero == &1 {
one_found = true;
}
}
}
let mut base = convert_base::Convert::new(2, 10);
let output = base.convert::<u32, u32>(&ones_and_zeros);
let value_without_sign: String = output
.into_iter()
.rev()
.map(|number| char::from_digit(number, 10).unwrap())
.collect();
// @TODO chain `sign` before collecting?
let value = sign.to_owned() + &value_without_sign;
value
},
VarFormat::Unsigned => {
let ones_and_zeros = value
.chars()
.rev()
.map(|char| char.to_digit(2).unwrap())
.collect::<Vec<_>>();
let mut base = convert_base::Convert::new(2, 10);
let output = base.convert::<u32, u32>(&ones_and_zeros);
let value: String = output
.into_iter()
.rev()
.map(|number| char::from_digit(number, 10).unwrap())
.collect();
value
},
}
}
}