Skip to content

Commit 124145d

Browse files
committed
fix: use a heuristic for better property value printing
1 parent 9b2445f commit 124145d

1 file changed

Lines changed: 30 additions & 16 deletions

File tree

src/pretty_print.rs

Lines changed: 30 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@
44

55
use crate::{
66
cell_collector::CollectCellsError,
7-
nodes::{root::Root, Node, NodeName},
7+
nodes::{root::Root, Node, NodeName, NodeProperty},
88
parsing::{NoPanic, Parser},
99
properties::values::{InvalidPropertyValue, U32List},
1010
FdtError,
@@ -155,26 +155,40 @@ fn print_properties<'a, P: Parser<'a>>(
155155
}
156156
_ => match prop.as_value::<&str>() {
157157
Ok("") => writeln!(f, "{:width$}{};", ' ', prop.name, width = depth * 4 + 4)?,
158-
Ok(value) => writeln!(f, "{:width$}{} = {:?};", ' ', prop.name, value, width = depth * 4 + 4)?,
159-
_ => match prop.value.len() {
160-
0 => writeln!(f, "{:width$}{};", ' ', prop.name, width = depth * 4 + 4)?,
161-
_ => {
162-
write!(f, "{:width$}{} = <", ' ', prop.name, width = depth * 4 + 4)?;
158+
Ok(value) => {
159+
let len = value.len();
160+
let printable_character_count = value.chars().filter(|c| c.is_ascii_graphic()).count();
163161

164-
for (i, n) in prop.as_value::<U32List>()?.iter().enumerate() {
165-
if i != 0 {
166-
write!(f, " ")?;
167-
}
168-
169-
write!(f, "{n:#04x}")?;
170-
}
171-
172-
writeln!(f, ">;")?;
162+
match printable_character_count * 100 / len {
163+
0..75 => print_prop_raw_value(f, prop, depth)?,
164+
_ => writeln!(f, "{:width$}{} = {:?};", ' ', prop.name, value, width = depth * 4 + 4)?,
173165
}
174-
},
166+
}
167+
_ => print_prop_raw_value(f, prop, depth)?,
175168
},
176169
}
177170
}
178171

179172
Ok(any_props)
180173
}
174+
175+
fn print_prop_raw_value(f: &mut core::fmt::Formatter<'_>, prop: NodeProperty<'_>, depth: usize) -> Result<(), Error> {
176+
match prop.value.len() {
177+
0 => writeln!(f, "{:width$}{};", ' ', prop.name, width = depth * 4 + 4)?,
178+
_ => {
179+
write!(f, "{:width$}{} = <", ' ', prop.name, width = depth * 4 + 4)?;
180+
181+
for (i, n) in prop.as_value::<U32List>()?.iter().enumerate() {
182+
if i != 0 {
183+
write!(f, " ")?;
184+
}
185+
186+
write!(f, "{n:#04x}")?;
187+
}
188+
189+
writeln!(f, ">;")?;
190+
}
191+
}
192+
193+
Ok(())
194+
}

0 commit comments

Comments
 (0)