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
174
175
176
177
178
179
180
181
182
183
184
185
186
#![allow(missing_docs)]
use crate::{
color::Color,
probe::{Log, Probe},
utils::de_from_str,
};
use anyhow::Error;
use drone_config::parse_size;
use std::{
ffi::{OsStr, OsString},
os::unix::ffi::OsStrExt,
path::PathBuf,
};
use structopt::StructOpt;
#[derive(Debug, StructOpt)]
pub struct Cli {
#[structopt(long, short, parse(from_occurrences))]
pub verbosity: u64,
#[structopt(long, default_value = "auto", parse(try_from_str = de_from_str))]
pub color: Color,
#[structopt(subcommand)]
pub cmd: Cmd,
}
#[derive(Debug, StructOpt)]
pub enum Cmd {
Flash(FlashCmd),
Gdb(GdbCmd),
Heap(HeapCmd),
Log(LogCmd),
New(NewCmd),
Reset(ResetCmd),
Print(PrintCmd),
}
#[derive(Debug, StructOpt)]
pub struct NewCmd {
#[structopt(parse(from_os_str))]
pub path: PathBuf,
#[structopt(short, long)]
pub device: String,
#[structopt(short, long, parse(try_from_str = parse_size))]
pub flash_size: u32,
#[structopt(short, long, parse(try_from_str = parse_size))]
pub ram_size: u32,
#[structopt(short, long, parse(try_from_str = de_from_str))]
pub probe: Option<Probe>,
#[structopt(long, short, parse(try_from_str = de_from_str))]
pub log: Option<Log>,
#[structopt(long)]
pub name: Option<String>,
#[structopt(long, default_value = "nightly")]
pub toolchain: String,
}
#[derive(Debug, StructOpt)]
pub struct HeapCmd {
#[structopt(
short = "f",
long,
name = "heaptrace",
default_value = "heaptrace",
parse(from_os_str)
)]
pub trace_file: PathBuf,
#[structopt(short, long, default_value = "main")]
pub config: String,
#[structopt(short, long, parse(try_from_str = parse_size))]
pub size: Option<u32>,
#[structopt(subcommand)]
pub heap_sub_cmd: Option<HeapSubCmd>,
}
#[derive(Debug, StructOpt)]
pub enum HeapSubCmd {
Generate(HeapGenerateCmd),
}
#[derive(Debug, StructOpt)]
pub struct HeapGenerateCmd {
#[structopt(short, long, parse(try_from_str = parse_size))]
pub pools: u32,
}
#[derive(Debug, StructOpt)]
pub struct ResetCmd {}
#[derive(Debug, StructOpt)]
pub struct FlashCmd {
#[structopt(parse(from_os_str))]
pub firmware: PathBuf,
}
#[derive(Debug, StructOpt)]
pub struct GdbCmd {
#[structopt(parse(from_os_str))]
pub firmware: Option<PathBuf>,
#[structopt(short, long)]
pub reset: bool,
#[structopt(short, long)]
pub interpreter: Option<String>,
#[structopt(parse(from_os_str), last(true))]
pub gdb_args: Vec<OsString>,
}
#[derive(Debug, StructOpt)]
pub struct LogCmd {
#[structopt(short, long)]
pub reset: bool,
#[structopt(
name = "OUTPUT",
parse(try_from_os_str = parse_log_output)
)]
pub outputs: Vec<LogOutput>,
}
#[derive(Debug, Clone)]
pub struct LogOutput {
pub ports: Vec<u32>,
pub path: OsString,
}
#[derive(Debug, StructOpt)]
pub struct PrintCmd {
#[structopt(subcommand)]
pub print_sub_cmd: PrintSubCmd,
}
#[derive(Debug, StructOpt)]
pub enum PrintSubCmd {
Target,
SupportedDevices,
}
fn parse_log_output(src: &OsStr) -> Result<LogOutput, OsString> {
let mut chunks = src.as_bytes().split(|&b| b == b':');
let path = OsStr::from_bytes(chunks.next().unwrap()).into();
let ports = chunks
.map(|port| Ok(String::from_utf8(port.to_vec())?.parse()?))
.collect::<Result<_, Error>>()
.map_err(|err| err.to_string())?;
Ok(LogOutput { ports, path })
}