audio_streams_conformance_test/
args.rs1use std::fmt;
6use std::str::FromStr;
7
8use argh::FromArgs;
9use audio_streams::*;
10use serde::Serialize;
11
12use super::error::Error;
13use super::sys::StreamSource as SysStreamSource;
14
15#[derive(Copy, Clone, Debug, PartialEq, Serialize)]
17pub enum StreamSourceEnum {
18 NoopStream,
19 Sys(SysStreamSource),
20}
21
22impl fmt::Display for StreamSourceEnum {
23 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
24 match self {
25 StreamSourceEnum::NoopStream => write!(f, "noop"),
26 StreamSourceEnum::Sys(stream_source) => stream_source.fmt(f),
27 }
28 }
29}
30
31impl FromStr for StreamSourceEnum {
32 type Err = Error;
33 fn from_str(s: &str) -> ::std::result::Result<StreamSourceEnum, Self::Err> {
34 match s {
35 "noop" => Ok(StreamSourceEnum::NoopStream),
36 _ => SysStreamSource::try_from(s).map(StreamSourceEnum::Sys),
37 }
38 }
39}
40
41fn default_channels() -> usize {
42 2
43}
44
45fn default_sample_format() -> SampleFormat {
46 SampleFormat::S16LE
47}
48
49fn default_rate() -> u32 {
50 48000
51}
52
53fn default_buffer_frames() -> usize {
54 240
55}
56
57fn default_iterations() -> usize {
58 10
59}
60
61fn default_stream_source() -> StreamSourceEnum {
62 StreamSourceEnum::NoopStream
63}
64
65fn parse_stream_source(value: &str) -> Result<StreamSourceEnum, String> {
66 StreamSourceEnum::from_str(value).map_err(|e| e.to_string())
67}
68
69fn parse_format(value: &str) -> Result<SampleFormat, String> {
70 SampleFormat::from_str(value).map_err(|e| e.to_string())
71}
72
73#[derive(Copy, Clone, Debug, FromArgs, Serialize)]
74pub struct Args {
76 #[argh(
78 option,
79 short = 'P',
80 default = "default_stream_source()",
81 from_str_fn(parse_stream_source)
82 )]
83 pub stream_source: StreamSourceEnum,
84 #[argh(option, short = 'c', default = "default_channels()")]
86 pub channels: usize,
87 #[argh(
89 option,
90 short = 'f',
91 default = "default_sample_format()",
92 from_str_fn(parse_format)
93 )]
94 pub format: SampleFormat,
95 #[argh(option, short = 'r', default = "default_rate()")]
97 pub rate: u32,
98 #[argh(option, short = 'b', default = "default_buffer_frames()")]
100 pub buffer_frames: usize,
101 #[argh(option, default = "default_iterations()")]
103 pub iterations: usize,
104 #[argh(switch)]
106 pub json: bool,
107 #[argh(switch)]
109 pub debug: bool,
110}
111
112impl fmt::Display for Args {
113 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
114 write!(
115 f,
116 r#"
117Playback Source: {:?}
118Channels: {}
119Format: {:?}
120Sample rate: {} frames/s
121Buffer size: {} frames
122Iterations: {}
123 "#,
124 self.stream_source,
125 self.channels,
126 self.format,
127 self.rate,
128 self.buffer_frames,
129 self.iterations
130 )
131 }
132}