1use super::{Decoded, FieldNode, FieldValue, PartitionDecoder, PartitionMeta};
5
6const PREVIEW_BYTES: usize = 32;
9
10pub struct RawDecoder;
11
12impl PartitionDecoder for RawDecoder {
13 fn name(&self) -> &'static str {
14 "raw"
15 }
16
17 fn matches(&self, _meta: &PartitionMeta, _data: &[u8]) -> bool {
18 true
20 }
21
22 fn decode(&self, _meta: &PartitionMeta, data: &[u8]) -> Decoded {
23 let printable = data.iter().filter(|&&b| (0x20..0x7f).contains(&b)).count();
24 let ratio = if data.is_empty() {
25 0.0
26 } else {
27 printable as f64 / data.len() as f64
28 };
29
30 let mut fields = vec![
31 FieldNode::leaf(
32 "size",
33 FieldValue::U64(data.len() as u64),
34 (0, data.len() as u64),
35 ),
36 FieldNode::leaf(
37 "printable_ascii_ratio",
38 FieldValue::Text(format!("{:.0}%", ratio * 100.0)),
39 (0, data.len() as u64),
40 ),
41 ];
42
43 let preview_len = data.len().min(PREVIEW_BYTES);
44 fields.push(FieldNode::leaf(
45 "preview",
46 FieldValue::Bytes(data[..preview_len].to_vec()),
47 (0, preview_len as u64),
48 ));
49
50 Decoded {
51 format_name: "RAW".into(),
52 fields,
53 warnings: Vec::new(),
54 }
55 }
56}