Skip to content
This repository has been archived by the owner on Dec 30, 2024. It is now read-only.

Commit

Permalink
tauri dev watches shared, VarFormat::format
Browse files Browse the repository at this point in the history
  • Loading branch information
MartinKavik committed Jun 14, 2024
1 parent a6da288 commit d0188c0
Show file tree
Hide file tree
Showing 3 changed files with 104 additions and 16 deletions.
1 change: 1 addition & 0 deletions .taurignore
Original file line number Diff line number Diff line change
@@ -1,2 +1,3 @@
/*
!/src-tauri
!/shared
4 changes: 3 additions & 1 deletion MoonZoon.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
115 changes: 100 additions & 15 deletions shared/src/var_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ pub enum VarFormat {
ASCII,
Binary,
BinaryWithGroups,
#[default]
// #[default]
Hexadecimal,
Octal,
#[default]
Signed,
Unsigned,
}
Expand All @@ -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",
}
}

Expand All @@ -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
},
}
}
}

0 comments on commit d0188c0

Please sign in to comment.