Skip to content

Commit 96fbfea

Browse files
committed
Simplify errors
1 parent c935d88 commit 96fbfea

File tree

8 files changed

+109
-135
lines changed

8 files changed

+109
-135
lines changed

rups/src/blocking/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -142,7 +142,7 @@ impl TcpConnection {
142142

143143
// Parse args by splitting whitespace, minding quotes for args with multiple words
144144
let args = shell_words::split(&raw)
145-
.map_err(|e| NutError::Generic(format!("Parsing server response failed: {}", e)))?;
145+
.map_err(|e| NutError::generic(format!("Parsing server response failed: {}", e)))?;
146146

147147
Ok(args)
148148
}

rups/src/cmd.rs

Lines changed: 71 additions & 113 deletions
Large diffs are not rendered by default.

rups/src/error.rs

Lines changed: 15 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,11 +36,18 @@ impl fmt::Display for NutError {
3636
"Given hostname cannot be used for a strict SSL connection"
3737
),
3838
Self::FeatureNotConfigured => write!(f, "Feature not configured by server"),
39-
Self::Generic(msg) => write!(f, "Internal client error: {}", msg),
39+
Self::Generic(msg) => write!(f, "Client error: {}", msg),
4040
}
4141
}
4242
}
4343

44+
impl NutError {
45+
/// Constructs a generic rups error.
46+
pub fn generic<T: ToString>(message: T) -> Self {
47+
Self::Generic(message.to_string())
48+
}
49+
}
50+
4451
impl std::error::Error for NutError {}
4552

4653
/// Encapsulation for errors emitted by the client library.
@@ -52,6 +59,13 @@ pub enum ClientError {
5259
Nut(NutError),
5360
}
5461

62+
impl ClientError {
63+
/// Constructs a generic rups error.
64+
pub fn generic<T: ToString>(message: T) -> Self {
65+
NutError::generic(message.to_string()).into()
66+
}
67+
}
68+
5569
impl fmt::Display for ClientError {
5670
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
5771
match self {

rups/src/lib.rs

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
88
pub use config::*;
99
pub use error::*;
10+
pub use util::*;
1011
pub use var::*;
1112

1213
/// Blocking client implementation for NUT.
@@ -20,4 +21,5 @@ mod config;
2021
mod error;
2122
#[cfg(feature = "ssl")]
2223
mod ssl;
24+
mod util;
2325
mod var;

rups/src/tokio/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl TcpConnection {
148148

149149
// Parse args by splitting whitespace, minding quotes for args with multiple words
150150
let args = shell_words::split(&raw)
151-
.map_err(|e| NutError::Generic(format!("Parsing server response failed: {}", e)))?;
151+
.map_err(|e| NutError::generic(format!("Parsing server response failed: {}", e)))?;
152152

153153
Ok(args)
154154
}
Lines changed: 13 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1,17 +1,21 @@
1-
use anyhow::Context;
21
use std::convert::{TryFrom, TryInto};
32
use std::fmt;
43

4+
/// The default upsd hostname.
55
pub const DEFAULT_HOSTNAME: &str = "localhost";
6+
/// The default upsd port.
67
pub const DEFAULT_PORT: u16 = 3493;
78

8-
/// Connection information for a upsd server.
9+
/// TCP connection information for a upsd server.
910
///
1011
/// The upsname is optional depending on context.
1112
#[derive(Debug, Clone, Copy, Eq, PartialEq)]
1213
pub struct UpsdName<'a> {
14+
/// The name of the ups device, if specified.
1315
pub upsname: Option<&'a str>,
16+
/// The hostname of the upsd server.
1417
pub hostname: &'a str,
18+
/// The port of the upsd server.
1519
pub port: u16,
1620
}
1721

@@ -26,9 +30,9 @@ impl<'a> Default for UpsdName<'a> {
2630
}
2731

2832
impl<'a> TryFrom<&'a str> for UpsdName<'a> {
29-
type Error = anyhow::Error;
33+
type Error = crate::ClientError;
3034

31-
fn try_from(value: &'a str) -> anyhow::Result<UpsdName<'a>> {
35+
fn try_from(value: &'a str) -> crate::Result<UpsdName<'a>> {
3236
let mut upsname: Option<&str> = None;
3337
let mut hostname = DEFAULT_HOSTNAME;
3438
let mut port = DEFAULT_PORT;
@@ -40,7 +44,7 @@ impl<'a> TryFrom<&'a str> for UpsdName<'a> {
4044
.next()
4145
.unwrap()
4246
.parse::<u16>()
43-
.with_context(|| "invalid port number")?;
47+
.map_err(|_| crate::ClientError::generic("Invalid port number"))?;
4448
if prefix.contains('@') {
4549
let mut split = prefix.splitn(2, '@');
4650
upsname = Some(split.next().unwrap());
@@ -64,13 +68,13 @@ impl<'a> TryFrom<&'a str> for UpsdName<'a> {
6468
}
6569
}
6670

67-
impl<'a> TryInto<rups::Host> for UpsdName<'a> {
68-
type Error = anyhow::Error;
71+
impl<'a> TryInto<crate::Host> for UpsdName<'a> {
72+
type Error = crate::ClientError;
6973

70-
fn try_into(self) -> anyhow::Result<rups::Host> {
74+
fn try_into(self) -> crate::Result<crate::Host> {
7175
(self.hostname.to_owned(), self.port)
7276
.try_into()
73-
.with_context(|| "Invalid hostname/port")
77+
.map_err(|_| crate::ClientError::generic("Invalid hostname/port"))
7478
}
7579
}
7680

rups/src/var.rs

Lines changed: 3 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -200,17 +200,13 @@ impl TryFrom<&str> for VariableType {
200200
.nth(1)
201201
.map(|s| s.parse().ok())
202202
.flatten()
203-
.ok_or_else(|| {
204-
crate::ClientError::Nut(crate::NutError::Generic(
205-
"Invalid STRING definition".into(),
206-
))
207-
})?;
203+
.ok_or_else(|| crate::ClientError::generic("Invalid STRING definition"))?;
208204
Ok(Self::String(size))
209205
} else {
210-
Err(crate::ClientError::Nut(crate::NutError::Generic(format!(
206+
Err(crate::ClientError::generic(format!(
211207
"Unrecognized variable type: {}",
212208
value
213-
))))
209+
)))
214210
}
215211
}
216212
}

rupsc/src/main.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,9 +8,9 @@ use core::convert::TryInto;
88
use anyhow::Context;
99
use clap::{App, Arg};
1010

11-
use crate::parser::UpsdName;
11+
use rups::UpsdName;
12+
1213
mod cmd;
13-
mod parser;
1414

1515
fn main() -> anyhow::Result<()> {
1616
let args = App::new(clap::crate_name!())
@@ -72,7 +72,7 @@ fn main() -> anyhow::Result<()> {
7272
)
7373
.get_matches();
7474

75-
let server: parser::UpsdName = args.value_of("upsd-server").map_or_else(
75+
let server: UpsdName = args.value_of("upsd-server").map_or_else(
7676
|| Ok(UpsdName::default()),
7777
|s| s.try_into().with_context(|| "Invalid upsd server name"),
7878
)?;

0 commit comments

Comments
 (0)