diff options
author | Minijackson <minijackson@riseup.net> | 2018-09-04 18:40:41 +0200 |
---|---|---|
committer | Minijackson <minijackson@riseup.net> | 2018-09-04 18:40:41 +0200 |
commit | e2e2f14accc751573a2a52b43bd8c87e1f1b89db (patch) | |
tree | a23823f18b661c0c142c20208350b28087d42f4c /src | |
parent | f08616b0626d6ca4c503494a5ada3316dda64c6a (diff) | |
download | set_eq-e2e2f14accc751573a2a52b43bd8c87e1f1b89db.tar.gz set_eq-e2e2f14accc751573a2a52b43bd8c87e1f1b89db.zip |
Centralize error context + properly report parse errors
Diffstat (limited to 'src')
-rw-r--r-- | src/main.rs | 24 | ||||
-rw-r--r-- | src/parsing/.gitignore | 1 | ||||
-rw-r--r-- | src/utils.rs | 52 |
3 files changed, 58 insertions, 19 deletions
diff --git a/src/main.rs b/src/main.rs index 310fa1a..360117c 100644 --- a/src/main.rs +++ b/src/main.rs | |||
@@ -12,6 +12,8 @@ extern crate clap_verbosity_flag; | |||
12 | #[macro_use] | 12 | #[macro_use] |
13 | extern crate structopt; | 13 | extern crate structopt; |
14 | 14 | ||
15 | extern crate lalrpop_util; | ||
16 | |||
15 | mod dbus_api; | 17 | mod dbus_api; |
16 | mod parsing; | 18 | mod parsing; |
17 | mod utils; | 19 | mod utils; |
@@ -19,7 +21,7 @@ mod utils; | |||
19 | use utils::*; | 21 | use utils::*; |
20 | 22 | ||
21 | use dbus_api::sink::OrgPulseAudioExtEqualizing1Equalizer; | 23 | use dbus_api::sink::OrgPulseAudioExtEqualizing1Equalizer; |
22 | use failure::{Error, ResultExt}; | 24 | use failure::Error; |
23 | use structopt::StructOpt; | 25 | use structopt::StructOpt; |
24 | 26 | ||
25 | use std::fs::File; | 27 | use std::fs::File; |
@@ -81,10 +83,6 @@ arg_enum! { | |||
81 | #[derive(StructOpt, Debug)] | 83 | #[derive(StructOpt, Debug)] |
82 | struct ResetCli {} | 84 | struct ResetCli {} |
83 | 85 | ||
84 | #[derive(Fail, Debug)] | ||
85 | #[fail(display = "No equalized sink found")] | ||
86 | struct NoEqualizedSink; | ||
87 | |||
88 | #[derive(Debug)] | 86 | #[derive(Debug)] |
89 | pub struct Filter { | 87 | pub struct Filter { |
90 | preamp: f64, | 88 | preamp: f64, |
@@ -136,12 +134,8 @@ fn start() -> Result<(), Error> { | |||
136 | } | 134 | } |
137 | 135 | ||
138 | fn reset(args: ResetCli) -> Result<(), Error> { | 136 | fn reset(args: ResetCli) -> Result<(), Error> { |
139 | let conn = connect().context( | 137 | let conn = connect()?; |
140 | "Could not connect to PulseAudio's D-Bus socket. Have you loaded the 'module-dbus-protocol' module?" | 138 | let conn_sink = get_equalized_sink(&conn)?; |
141 | )?; | ||
142 | let conn_sink = get_equalized_sink(&conn).context( | ||
143 | "Could not find an equalized sink. Have you loaded the 'module-equalizer-sink' module?", | ||
144 | )?; | ||
145 | let filter_rate = conn_sink.get_filter_sample_rate()?; | 139 | let filter_rate = conn_sink.get_filter_sample_rate()?; |
146 | let filter = Filter { | 140 | let filter = Filter { |
147 | preamp: 1f64, | 141 | preamp: 1f64, |
@@ -155,12 +149,8 @@ fn reset(args: ResetCli) -> Result<(), Error> { | |||
155 | } | 149 | } |
156 | 150 | ||
157 | fn load(args: LoadCli) -> Result<(), Error> { | 151 | fn load(args: LoadCli) -> Result<(), Error> { |
158 | let conn = connect().context( | 152 | let conn = connect()?; |
159 | "Could not connect to PulseAudio's D-Bus socket. Have you loaded the 'module-dbus-protocol' module?" | 153 | let conn_sink = get_equalized_sink(&conn)?; |
160 | )?; | ||
161 | let conn_sink = get_equalized_sink(&conn).context( | ||
162 | "Could not find an equalized sink. Have you loaded the 'module-equalizer-sink' module?", | ||
163 | )?; | ||
164 | 154 | ||
165 | let filter = if args.file == "-" { | 155 | let filter = if args.file == "-" { |
166 | let stdin = io::stdin(); | 156 | let stdin = io::stdin(); |
diff --git a/src/parsing/.gitignore b/src/parsing/.gitignore new file mode 100644 index 0000000..cdfabf6 --- /dev/null +++ b/src/parsing/.gitignore | |||
@@ -0,0 +1 @@ | |||
./equalizer_apo.rs | |||
diff --git a/src/utils.rs b/src/utils.rs index 24af366..de9ed3c 100644 --- a/src/utils.rs +++ b/src/utils.rs | |||
@@ -1,4 +1,4 @@ | |||
1 | use {Filter, NoEqualizedSink}; | 1 | use {EqualizerConfFormat, Filter}; |
2 | 2 | ||
3 | use parsing::EqualizerApoParser; | 3 | use parsing::EqualizerApoParser; |
4 | 4 | ||
@@ -8,10 +8,33 @@ use dbus_api::sink::OrgPulseAudioExtEqualizing1Equalizer; | |||
8 | 8 | ||
9 | use dbus::{BusType, ConnPath, Connection}; | 9 | use dbus::{BusType, ConnPath, Connection}; |
10 | use failure::{Error, ResultExt}; | 10 | use failure::{Error, ResultExt}; |
11 | use lalrpop_util; | ||
11 | 12 | ||
13 | use std::fmt; | ||
12 | use std::io; | 14 | use std::io; |
13 | 15 | ||
16 | #[derive(Fail, Debug)] | ||
17 | #[fail(display = "No equalized sink found")] | ||
18 | struct NoEqualizedSink; | ||
19 | |||
20 | #[derive(Fail, Debug)] | ||
21 | #[fail( | ||
22 | display = "Could not parse using the {} format: {}", | ||
23 | format, | ||
24 | message | ||
25 | )] | ||
26 | struct ParseError { | ||
27 | format: EqualizerConfFormat, | ||
28 | message: String, | ||
29 | } | ||
30 | |||
14 | pub fn connect() -> Result<Connection, Error> { | 31 | pub fn connect() -> Result<Connection, Error> { |
32 | Ok(connect_impl().context( | ||
33 | "Could not connect to PulseAudio's D-Bus socket. Have you loaded the 'module-dbus-protocol' module?" | ||
34 | )?) | ||
35 | } | ||
36 | |||
37 | fn connect_impl() -> Result<Connection, Error> { | ||
15 | let pulse_sock_path = get_pulse_dbus_sock()?; | 38 | let pulse_sock_path = get_pulse_dbus_sock()?; |
16 | info!("PulseAudio's D-Bus socket path is: {}", pulse_sock_path); | 39 | info!("PulseAudio's D-Bus socket path is: {}", pulse_sock_path); |
17 | 40 | ||
@@ -20,6 +43,14 @@ pub fn connect() -> Result<Connection, Error> { | |||
20 | } | 43 | } |
21 | 44 | ||
22 | pub fn get_equalized_sink<'a>(conn: &'a Connection) -> Result<ConnPath<'a, &'a Connection>, Error> { | 45 | pub fn get_equalized_sink<'a>(conn: &'a Connection) -> Result<ConnPath<'a, &'a Connection>, Error> { |
46 | Ok(get_equalized_sink_impl(conn).context( | ||
47 | "Could not find an equalized sink. Have you loaded the 'module-equalizer-sink' module?", | ||
48 | )?) | ||
49 | } | ||
50 | |||
51 | fn get_equalized_sink_impl<'a>( | ||
52 | conn: &'a Connection, | ||
53 | ) -> Result<ConnPath<'a, &'a Connection>, Error> { | ||
23 | let conn_manager = conn.with_path("org.PulseAudio.Core1", "/org/pulseaudio/equalizing1", 2000); | 54 | let conn_manager = conn.with_path("org.PulseAudio.Core1", "/org/pulseaudio/equalizing1", 2000); |
24 | 55 | ||
25 | // TODO: make that a command-line option | 56 | // TODO: make that a command-line option |
@@ -55,12 +86,29 @@ where | |||
55 | file.read_to_string(&mut buffer)?; | 86 | file.read_to_string(&mut buffer)?; |
56 | 87 | ||
57 | // TODO: lifetime issue when "throwing" parse error | 88 | // TODO: lifetime issue when "throwing" parse error |
58 | let filter = EqualizerApoParser::new().parse(&buffer).unwrap(); | 89 | let filter = EqualizerApoParser::new() |
90 | .parse(&buffer) | ||
91 | .map_err(|e| convert_parse_error(EqualizerConfFormat::EqualizerAPO, e))?; | ||
59 | trace!("Parsed filter: {:?}", filter); | 92 | trace!("Parsed filter: {:?}", filter); |
60 | 93 | ||
61 | Ok(filter) | 94 | Ok(filter) |
62 | } | 95 | } |
63 | 96 | ||
97 | fn convert_parse_error<L, T, E>( | ||
98 | format: EqualizerConfFormat, | ||
99 | error: lalrpop_util::ParseError<L, T, E>, | ||
100 | ) -> ParseError | ||
101 | where | ||
102 | L: fmt::Display, | ||
103 | T: fmt::Display, | ||
104 | E: fmt::Display, | ||
105 | { | ||
106 | ParseError { | ||
107 | format, | ||
108 | message: format!("{}", error), | ||
109 | } | ||
110 | } | ||
111 | |||
64 | fn get_pulse_dbus_sock() -> Result<String, Error> { | 112 | fn get_pulse_dbus_sock() -> Result<String, Error> { |
65 | trace!("Connecting to the D-Bus' session bus"); | 113 | trace!("Connecting to the D-Bus' session bus"); |
66 | let conn = Connection::get_private(BusType::Session)?; | 114 | let conn = Connection::get_private(BusType::Session)?; |