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 argh::FromArgs;
use log;
use std::error::Error;
use std::path::PathBuf;
pub mod grpc;
pub mod record;
pub mod take;
pub type BoxError = Box<dyn Error>;
pub struct Logger;
impl log::Log for Logger {
fn enabled(&self, metadata: &log::Metadata) -> bool {
metadata.level() <= log::Level::Info
}
fn log(&self, record: &log::Record) {
if self.enabled(record.metadata()) {
println!("{}", record.args());
}
}
fn flush(&self) {}
}
#[derive(FromArgs, PartialEq, Debug)]
pub struct Command {
#[argh(switch, short = 'v')]
verbose: bool,
#[argh(subcommand)]
pub nested: SubCommand,
}
pub struct Opts {
pub verbose: bool,
}
impl Opts {
pub fn new(cmd: &Command) -> Self {
Self {
verbose: cmd.verbose,
}
}
}
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand)]
pub enum SubCommand {
Take(Take),
Record(Record),
}
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "take")]
pub struct Take {
#[argh(positional)]
frame: PathBuf,
#[argh(option, short = 'H')]
header: String,
#[argh(option)]
proto: Vec<PathBuf>,
#[argh(option, short = 'a')]
addr: String,
#[argh(option, short = 'c')]
cut: PathBuf,
#[argh(option, short = 'o')]
output: Option<PathBuf>,
}
#[derive(FromArgs, PartialEq, Debug)]
#[argh(subcommand, name = "record")]
pub struct Record {
#[argh(positional)]
path: PathBuf,
#[argh(positional)]
name: String,
#[argh(option, short = 'H')]
header: String,
#[argh(option)]
proto: Vec<PathBuf>,
#[argh(option, short = 'a')]
addr: String,
#[argh(option, short = 'c')]
cut: Option<PathBuf>,
#[argh(option, short = 'o')]
output: Option<PathBuf>,
#[argh(switch, short = 'i')]
interactive: bool,
}
impl Take {
pub fn validate(&self) -> Result<(), &str> {
if !self.frame.is_file() {
return Err("<frame> must be a valid file");
}
if !self.cut.is_file() {
return Err("<cut> must be a valid file");
}
Ok(())
}
}
impl Record {
pub fn validate(&self) -> Result<(), &str> {
if !self.path.is_dir() {
return Err("<path> must be a valid directory");
}
if let Some(cut) = &self.cut {
if !cut.is_file() {
return Err("<cut> must be a valid file");
}
} else {
if !self.get_cut_file().is_file() {
return Err("unable to find a matching cut file in the given directory");
}
}
if let Some(output) = &self.output {
if !output.is_dir() {
return Err("<output> must be a valid directory");
}
}
Ok(())
}
pub fn get_cut_file(&self) -> PathBuf {
if let Some(cut) = &self.cut {
cut.clone()
} else {
self.path.join(format!("{}.cut.json", self.name))
}
}
pub fn get_cut_copy(&self) -> PathBuf {
self.path.join(format!(".{}.cut.json", self.name))
}
}