1- use anyhow:: Context ;
21use std:: convert:: { TryFrom , TryInto } ;
32use std:: fmt;
43
4+ /// The default upsd hostname.
55pub const DEFAULT_HOSTNAME : & str = "localhost" ;
6+ /// The default upsd port.
67pub 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 ) ]
1213pub 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
2832impl < ' 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
0 commit comments