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
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
|
//! Options support.
//!
//! This module contains the definitions for options and names for common options.
//! Options are used to set custom parameters in e.g. decoders or muxers.
//!
//! As a rule target for options should provide a list of supported options and ignore unknown options.
use std::sync::Arc;
use std::fmt;
pub use crate::compr::deflate::{DEFLATE_MODE_DESCRIPTION, DEFLATE_OPTION_VALUES, DEFLATE_MODE_NONE, DEFLATE_MODE_FAST, DEFLATE_MODE_BETTER, DEFLATE_MODE_BEST};
/// Common name for keyframe interval option.
pub const KEYFRAME_OPTION: &str = "key_int";
/// Common description for keyframe interval option.
pub const KEYFRAME_OPTION_DESC: &str = "Keyframe interval (0 - automatic)";
/// Common name for frame skipping mode.
pub const FRAME_SKIP_OPTION: &str = "frame_skip";
/// Common description for frame skipping mode.
pub const FRAME_SKIP_OPTION_DESC: &str = "Frame skipping mode";
/// Frame skipping option value for no skipped frames.
pub const FRAME_SKIP_OPTION_VAL_NONE: &str = "none";
/// Frame skipping option value for decoding only keyframes.
pub const FRAME_SKIP_OPTION_VAL_KEYFRAME: &str = "keyframes";
/// Frame skipping option value for decoding only intra frames.
pub const FRAME_SKIP_OPTION_VAL_INTRA: &str = "intra";
/// A list specifying option parsing and validating errors.
#[derive(Clone,Copy,Debug,PartialEq)]
pub enum OptionError {
/// Input is not intended for the current option definition.
WrongName,
/// Option value is not in the expected format.
InvalidFormat,
/// Option value was not in the range.
InvalidValue,
/// Parse error.
ParseError,
}
/// A specialised `Result` type for option parsing/validation.
pub type OptionResult<T> = Result<T, OptionError>;
/// Option definition type.
#[derive(Debug)]
pub enum NAOptionDefinitionType {
/// Option may just be present.
None,
/// Option is a boolean value.
Bool,
/// Option is an integer with optional minimum and maximum value.
Int(Option<i64>, Option<i64>),
/// Option is a floating point number with optional minimum and maximum value.
Float(Option<f64>, Option<f64>),
/// Option is a string with an optional list of allowed values.
String(Option<&'static [&'static str]>),
/// Option is some binary data.
Data,
}
/// Option definition.
#[derive(Debug)]
pub struct NAOptionDefinition {
/// Option name.
pub name: &'static str,
/// Option meaning.
pub description: &'static str,
/// Option type.
pub opt_type: NAOptionDefinitionType,
}
impl NAOptionDefinition {
/// Tries to parse input string(s) as an option and returns new option and number of arguments used (1 or 2) on success.
pub fn parse(&self, name: &str, value: Option<&String>) -> OptionResult<(NAOption, usize)> {
let no_name = "no".to_owned() + self.name;
let opt_no_name = "--no".to_owned() + self.name;
if name == no_name || name == opt_no_name {
match self.opt_type {
NAOptionDefinitionType::Bool => return Ok((NAOption { name: self.name, value: NAValue::Bool(false) }, 1)),
_ => return Err(OptionError::InvalidFormat),
};
}
let opt_name = "--".to_owned() + self.name;
if self.name != name && opt_name != name {
return Err(OptionError::WrongName);
}
match self.opt_type {
NAOptionDefinitionType::None => Ok((NAOption { name: self.name, value: NAValue::None }, 1)),
NAOptionDefinitionType::Bool => Ok((NAOption { name: self.name, value: NAValue::Bool(true) }, 1)),
NAOptionDefinitionType::Int(_, _) => {
if let Some(strval) = value {
let ret = strval.parse::<i64>();
if let Ok(val) = ret {
let opt = NAOption { name: self.name, value: NAValue::Int(val) };
self.check(&opt)?;
Ok((opt, 2))
} else {
Err(OptionError::ParseError)
}
} else {
Err(OptionError::ParseError)
}
},
NAOptionDefinitionType::Float(_, _) => {
if let Some(strval) = value {
let ret = strval.parse::<f64>();
if let Ok(val) = ret {
let opt = NAOption { name: self.name, value: NAValue::Float(val) };
self.check(&opt)?;
Ok((opt, 2))
} else {
Err(OptionError::ParseError)
}
} else {
Err(OptionError::ParseError)
}
},
NAOptionDefinitionType::String(_) => {
if let Some(strval) = value {
let opt = NAOption { name: self.name, value: NAValue::String(strval.to_string()) };
self.check(&opt)?;
Ok((opt, 2))
} else {
Err(OptionError::ParseError)
}
},
_ => unimplemented!(),
}
}
/// Checks whether input option conforms to the definition i.e. whether it has proper format and it lies in range.
pub fn check(&self, option: &NAOption) -> OptionResult<()> {
if option.name != self.name {
return Err(OptionError::WrongName);
}
match option.value {
NAValue::None => Ok(()),
NAValue::Bool(_) => Ok(()),
NAValue::Int(intval) => {
match self.opt_type {
NAOptionDefinitionType::Int(minval, maxval) => {
if let Some(minval) = minval {
if intval < minval { return Err(OptionError::InvalidValue); }
}
if let Some(maxval) = maxval {
if intval > maxval { return Err(OptionError::InvalidValue); }
}
},
NAOptionDefinitionType::Float(minval, maxval) => {
let fval = intval as f64;
if let Some(minval) = minval {
if fval < minval { return Err(OptionError::InvalidValue); }
}
if let Some(maxval) = maxval {
if fval > maxval { return Err(OptionError::InvalidValue); }
}
},
_ => return Err(OptionError::InvalidFormat),
};
Ok(())
},
NAValue::Float(fval) => {
match self.opt_type {
NAOptionDefinitionType::Int(minval, maxval) => {
let intval = fval as i64;
if let Some(minval) = minval {
if intval < minval { return Err(OptionError::InvalidValue); }
}
if let Some(maxval) = maxval {
if intval > maxval { return Err(OptionError::InvalidValue); }
}
},
NAOptionDefinitionType::Float(minval, maxval) => {
if let Some(minval) = minval {
if fval < minval { return Err(OptionError::InvalidValue); }
}
if let Some(maxval) = maxval {
if fval > maxval { return Err(OptionError::InvalidValue); }
}
},
_ => return Err(OptionError::InvalidFormat),
};
Ok(())
},
NAValue::String(ref cur_str) => {
if let NAOptionDefinitionType::String(Some(strings)) = self.opt_type {
for string in strings.iter() {
if cur_str == string {
return Ok(());
}
}
Err(OptionError::InvalidValue)
} else {
Ok(())
}
},
NAValue::Data(_) => Ok(()),
}
}
}
impl fmt::Display for NAOptionDefinition {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self.opt_type {
NAOptionDefinitionType::None => write!(f, "{}: {}", self.name, self.description),
NAOptionDefinitionType::Bool => write!(f, "[no]{}: {}", self.name, self.description),
NAOptionDefinitionType::String(ref str_list) => {
if let Some(opts) = str_list {
write!(f, "{} {}: {}", self.name, opts.join("|"), self.description)
} else {
write!(f, "{} <string>: {}", self.name, self.description)
}
},
NAOptionDefinitionType::Int(minval, maxval) => {
let range = match (&minval, &maxval) {
(Some(minval), None) => format!("{}-..", minval),
(None, Some(maxval)) => format!("..-{}", maxval),
(Some(minval), Some(maxval)) => format!("{}-{}", minval, maxval),
_ => "<integer>".to_string(),
};
write!(f, "{} {}: {}", self.name, range, self.description)
},
NAOptionDefinitionType::Float(minval, maxval) => {
let range = match (&minval, &maxval) {
(Some(minval), None) => format!("{}-..", minval),
(None, Some(maxval)) => format!("..-{}", maxval),
(Some(minval), Some(maxval)) => format!("{}-{}", minval, maxval),
_ => "<float>".to_string(),
};
write!(f, "{} {}: {}", self.name, range, self.description)
},
NAOptionDefinitionType::Data => write!(f, "{} <binary data>: {}", self.name, self.description),
}
}
}
/// Option.
#[derive(Clone,Debug,PartialEq)]
pub struct NAOption {
/// Option name.
pub name: &'static str,
/// Option value.
pub value: NAValue,
}
/// A list of accepted option values.
#[derive(Debug,Clone,PartialEq)]
pub enum NAValue {
/// Empty value.
None,
/// Boolean value.
Bool(bool),
/// Integer value.
Int(i64),
/// Floating point value.
Float(f64),
/// String value.
String(String),
/// Binary data value.
Data(Arc<Vec<u8>>),
}
/// Trait for all objects that handle `NAOption`.
pub trait NAOptionHandler {
/// Returns the options recognised by current object.
fn get_supported_options(&self) -> &[NAOptionDefinition];
/// Passes options for the object to set (or ignore).
fn set_options(&mut self, options: &[NAOption]);
/// Queries the current option value in the object (if present).
fn query_option_value(&self, name: &str) -> Option<NAValue>;
}
#[cfg(test)]
mod test {
use super::*;
#[test]
fn test_option_validation() {
let option = NAOption {name: "option", value: NAValue::Int(42) };
let mut def = NAOptionDefinition { name: "option", description: "", opt_type: NAOptionDefinitionType::Int(None, None) };
assert!(def.check(&option).is_ok());
def.opt_type = NAOptionDefinitionType::Int(None, Some(30));
assert_eq!(def.check(&option), Err(OptionError::InvalidValue));
def.opt_type = NAOptionDefinitionType::Int(Some(40), None);
assert!(def.check(&option).is_ok());
def.name = "option2";
assert_eq!(def.check(&option), Err(OptionError::WrongName));
let option = NAOption {name: "option", value: NAValue::String("test".to_string()) };
let mut def = NAOptionDefinition { name: "option", description: "", opt_type: NAOptionDefinitionType::String(None) };
assert!(def.check(&option).is_ok());
def.opt_type = NAOptionDefinitionType::String(Some(&["a string", "test string"]));
assert_eq!(def.check(&option), Err(OptionError::InvalidValue));
def.opt_type = NAOptionDefinitionType::String(Some(&["a string", "test"]));
assert!(def.check(&option).is_ok());
}
#[test]
fn test_option_parsing() {
let mut def = NAOptionDefinition { name: "option", description: "", opt_type: NAOptionDefinitionType::Float(None, None) };
assert_eq!(def.parse("--option", None), Err(OptionError::ParseError));
assert_eq!(def.parse("--nooption", None), Err(OptionError::InvalidFormat));
assert_eq!(def.parse("--option", Some(&"42".to_string())),
Ok((NAOption{name:"option",value: NAValue::Float(42.0)}, 2)));
def.opt_type = NAOptionDefinitionType::Float(None, Some(40.0));
assert_eq!(def.parse("--option", Some(&"42".to_string())),
Err(OptionError::InvalidValue));
let def = NAOptionDefinition { name: "option", description: "", opt_type: NAOptionDefinitionType::Bool };
assert_eq!(def.parse("option", None),
Ok((NAOption{name: "option", value: NAValue::Bool(true) }, 1)));
assert_eq!(def.parse("nooption", None),
Ok((NAOption{name: "option", value: NAValue::Bool(false) }, 1)));
}
}
|