mirror of
https://github.com/tiffany352/rink-rs
synced 2024-11-10 05:34:14 +00:00
Simplify Context::describe_unit
Move duplicate code into a closure.
This commit is contained in:
parent
612ca08306
commit
0e65b19e56
1 changed files with 21 additions and 26 deletions
|
@ -178,6 +178,25 @@ impl Context {
|
|||
recip = true;
|
||||
write!(buf, "{}", name).unwrap();
|
||||
} else {
|
||||
let mut helper = |dim: &Dim, pow: i64, buf: &mut Vec<u8>| {
|
||||
let mut map = Unit::new();
|
||||
map.insert(dim.clone(), pow);
|
||||
if let Some(name) = self.quantities.get(&map) {
|
||||
write!(buf, " {}", name).unwrap();
|
||||
} else {
|
||||
let mut map = Unit::new();
|
||||
map.insert(dim.clone(), 1);
|
||||
if let Some(name) = self.quantities.get(&map) {
|
||||
write!(buf, " {}", name).unwrap();
|
||||
} else {
|
||||
write!(buf, " '{}'", dim).unwrap();
|
||||
}
|
||||
if pow != 1 {
|
||||
write!(buf, "^{}", pow).unwrap();
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let mut frac = vec![];
|
||||
let mut found = false;
|
||||
for (dim, &pow) in &value.unit {
|
||||
|
@ -185,22 +204,7 @@ impl Context {
|
|||
frac.push((dim, -pow));
|
||||
} else {
|
||||
found = true;
|
||||
let mut map = Unit::new();
|
||||
map.insert(dim.clone(), pow);
|
||||
if let Some(name) = self.quantities.get(&map) {
|
||||
write!(buf, " {}", name).unwrap();
|
||||
} else {
|
||||
let mut map = Unit::new();
|
||||
map.insert(dim.clone(), 1);
|
||||
if let Some(name) = self.quantities.get(&map) {
|
||||
write!(buf, " {}", name).unwrap();
|
||||
} else {
|
||||
write!(buf, " '{}'", dim).unwrap();
|
||||
}
|
||||
if pow != 1 {
|
||||
write!(buf, "^{}", pow).unwrap();
|
||||
}
|
||||
}
|
||||
helper(dim, pow, &mut buf);
|
||||
}
|
||||
}
|
||||
if !frac.is_empty() {
|
||||
|
@ -215,16 +219,7 @@ impl Context {
|
|||
if let Some(name) = self.quantities.get(&map) {
|
||||
write!(buf, " {}", name).unwrap();
|
||||
} else {
|
||||
let mut map = Unit::new();
|
||||
map.insert(dim.clone(), 1);
|
||||
if let Some(name) = self.quantities.get(&map) {
|
||||
write!(buf, " {}", name).unwrap();
|
||||
} else {
|
||||
write!(buf, " '{}'", dim).unwrap();
|
||||
}
|
||||
if pow != 1 {
|
||||
write!(buf, "^{}", pow).unwrap();
|
||||
}
|
||||
helper(dim, pow, &mut buf);
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Reference in a new issue