1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
use clap::Args;
use hff_std::*;
use std::path::PathBuf;
/// Dump out the structure of an hff file.
#[derive(Debug, Args)]
pub struct Dump {
/// The input file.
pub input: PathBuf,
/// Indent the information by depth?
#[arg(long, overrides_with = "_no_indent")]
pub indent: bool,
/// No indention.
#[arg(long = "no-indent")]
pub _no_indent: bool,
/// Max length to indent.
#[arg(long, default_value = "20")]
pub max_indent: usize,
/// Number of spaces per indent level to use.
#[arg(long, default_value = "2")]
pub indent_size: usize,
/// Dump chunk type, sub-type?
#[arg(long, default_value = "false")]
pub chunk_types: bool,
/// Dump metadata?
#[arg(long, default_value = "false")]
pub metadata: bool,
/// Interpret the metadata as a key string vector table.
#[arg(long, default_value = "false", conflicts_with = "as_string_vec")]
pub as_ksv: bool,
/// Interpret the metadata as a string vector.
#[arg(long, conflicts_with = "as_ksv")]
pub as_string_vec: bool,
}
impl Dump {
/// Execute the dump subcommand.
pub fn execute(self) -> Result<()> {
use std::fs::File;
// The input must exist and be a single file.
if self.input.exists() && self.input.metadata()?.is_file() {
// Open the hff and check it is valid.
let hff = open(File::open(&self.input)?)?;
// Iterate through the content.
println!();
println!("----------");
for (depth, table) in hff.depth_first() {
self.dump(&hff, depth, &table)?;
}
println!("----------");
Ok(())
} else {
Err(Error::Invalid(format!(
"Invalid input: {}",
self.input.display().to_string()
)))
}
}
/// Dump information about the provided table.
fn dump(
&self,
hff: &Hff<StdReader>,
depth: usize,
table: &TableView<'_, StdReader>,
) -> Result<()> {
// Always print out the table information.
println!(
"{} ({} : children: {} chunks: {})",
self.indent(depth),
table.identifier().to_string(hff.id_type()),
table.child_count(),
table.chunk_count()
);
// Print out the metadata if desired.
if self.metadata {
self.dump_metadata(hff, depth, table)?;
}
// Print out chunk types if desired.
if self.chunk_types {
self.dump_chunk_types(hff.id_type(), depth, table)?;
}
Ok(())
}
/// Dump out the metadata if any.
fn dump_metadata(
&self,
hff: &Hff<StdReader>,
depth: usize,
table: &TableView<'_, StdReader>,
) -> Result<()> {
let metadata = hff.get(table)?;
if self.as_ksv {
match hff_std::hff_core::utilities::Ksv::from_bytes(metadata.as_slice()) {
Ok(ksv) => {
println!(" {} {:#?}", self.indent(depth), ksv);
}
Err(_) => {
println!(" {} <Not a key string vector>", self.indent(depth));
}
}
} else if self.as_string_vec {
match hff_std::hff_core::utilities::StringVec::from_bytes(metadata.as_slice()) {
Ok(sv) => {
println!(" {} {:#?}", self.indent(depth), sv);
}
Err(_) => {
println!(" {} <Not a string vector>", self.indent(depth));
}
}
} else {
match std::str::from_utf8(&metadata) {
Ok(s) => {
println!(" {}{}", self.indent(depth), s);
}
Err(_) => {
println!(
" {} ({:<8} {:<8})",
self.indent(depth),
metadata.len(),
table.offset()
);
}
}
}
Ok(())
}
/// Dump out the chunk types if any.
fn dump_chunk_types(
&self,
id_type: IdType,
depth: usize,
table: &TableView<'_, StdReader>,
) -> Result<()> {
for chunk in table.chunks() {
println!(
" {} [{} Len: {}]",
self.indent(depth),
chunk.identifier().to_string(id_type),
chunk.len()
);
}
Ok(())
}
/// Get a string of spaces representing the indent level desired.
fn indent(&self, depth: usize) -> String {
if self.indent {
if self.indent_size * depth < self.max_indent {
std::iter::repeat(' ')
.take(self.indent_size * depth)
.collect::<String>()
} else {
std::iter::repeat(' ')
.take((self.indent_size * self.max_indent) - 3)
.collect::<String>()
+ "-> "
}
} else {
String::new()
}
}
}