diff --git a/Cargo.toml b/Cargo.toml index 0eb1f0d..4d95072 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -26,6 +26,7 @@ get_if_addrs = "0.5.3" regex = "1.3.7" once_cell = "1.3.1" async-stream = "0.2.1" +thiserror = "1.0.20" [[example]] name = "ssb-cli" diff --git a/examples/.ssb-cli.rs.swp b/examples/.ssb-cli.rs.swp deleted file mode 100644 index 89bddf9..0000000 Binary files a/examples/.ssb-cli.rs.swp and /dev/null differ diff --git a/examples/ssb-cli.rs b/examples/ssb-cli.rs index 1e0b2a5..dcc5552 100644 --- a/examples/ssb-cli.rs +++ b/examples/ssb-cli.rs @@ -91,7 +91,7 @@ where RecvMsg::ErrorResponse(message) => { return std::result::Result::Err(Box::new(AppError::new(message))); } - _ => { } + _ => {} } } } @@ -119,7 +119,7 @@ where return std::result::Result::Err(Box::new(AppError::new(message))); } RecvMsg::CancelStreamRespose() => break, - _ => { } + _ => {} } } } @@ -189,34 +189,37 @@ async fn main() -> SolarResult<()> { let req_id = client.whoami_req_send().await?; let whoami = match get_async(&mut rpc_reader, req_id, whoami_res_parse).await { - Ok(res) => { - println!("😊 server says hello to {}", res.id); - id - } - Err(err) => { - if !err.to_string().contains("method:whoami is not in list of allowed methods") { - println!("Cannot ask for whoami {}",err); - } - id - } + Ok(res) => { + println!("😊 server says hello to {}", res.id); + id + } + Err(err) => { + if !err + .to_string() + .contains("method:whoami is not in list of allowed methods") + { + println!("Cannot ask for whoami {}", err); + } + id + } }; - loop { - let mut line_buffer = String::new(); - print!("> "); std::io::stdout().flush(); + let mut line_buffer = String::new(); + print!("> "); + let _ = std::io::stdout().flush(); match std::io::stdin().read_line(&mut line_buffer) { - Err(_) => break, - _ => { } + Err(_) => break, + _ => {} }; let args: Vec = line_buffer .replace("\n", "") .split_whitespace() .map(|arg| arg.to_string()) .collect(); - + if args.len() == 0 { - continue; + continue; } match (args[0].as_str(), args.len()) { ("exit", 1) => { diff --git a/src/api/error.rs b/src/api/error.rs index ddd9309..d2684f5 100644 --- a/src/api/error.rs +++ b/src/api/error.rs @@ -1,59 +1,11 @@ -#[derive(Debug)] +use thiserror::Error; + +#[derive(Error, Debug)] pub enum Error { - InvalidSequenceNo, - ServerMessage(String), - Rpc(crate::rpc::Error), - InvalidInviteCode, - HomeNotFound, - InvalidConfig, - Json(serde_json::Error), - ParseInt(std::num::ParseIntError), - CryptoFormat(crate::crypto::Error), - Io(std::io::Error), - Feed(crate::feed::Error), + #[error("rpc")] + Rpc(#[from] crate::rpc::Error), + #[error("json decode")] + Json(#[from] serde_json::Error), } -impl From for Error { - fn from(err: crate::rpc::Error) -> Self { - Error::Rpc(err) - } -} - -impl From for Error { - fn from(err: serde_json::Error) -> Self { - Error::Json(err) - } -} - -impl From for Error { - fn from(err: std::num::ParseIntError) -> Self { - Error::ParseInt(err) - } -} - -impl From for Error { - fn from(err: crate::crypto::Error) -> Self { - Error::CryptoFormat(err) - } -} - -impl From for Error { - fn from(err: std::io::Error) -> Self { - Error::Io(err) - } -} - -impl From for Error { - fn from(err: crate::feed::Error) -> Self { - Error::Feed(err) - } -} - -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{:?}", self) - } -} -impl std::error::Error for Error {} - pub type Result = std::result::Result; diff --git a/src/crypto/error.rs b/src/crypto/error.rs index 25fe8b1..78263b3 100644 --- a/src/crypto/error.rs +++ b/src/crypto/error.rs @@ -1,23 +1,19 @@ -#[derive(Debug)] +use thiserror::Error; + +#[derive(Error, Debug)] pub enum Error { - Base64Decode(base64::DecodeError), + #[error("error decoding base64")] + Base64Decode(#[from] base64::DecodeError), + #[error("bad public key")] BadPublicKey, + #[error("bad secret key")] BadSecretKey, + #[error("invalid digest")] InvalidDigest, + #[error("invalid suffix")] InvalidSuffix, + #[error("cannot create signature")] CannotCreateSignature, } -impl From for Error { - fn from(err: base64::DecodeError) -> Self { - Error::Base64Decode(err) - } -} -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{:?}", self) - } -} -impl std::error::Error for Error {} - pub type Result = std::result::Result; diff --git a/src/discovery/error.rs b/src/discovery/error.rs index 12b7d1f..488bed8 100644 --- a/src/discovery/error.rs +++ b/src/discovery/error.rs @@ -1,35 +1,17 @@ -#[derive(Debug)] +use thiserror::Error; + +#[derive(Error, Debug)] pub enum Error { - ParseInt(std::num::ParseIntError), + #[error("invalid integer")] + ParseInt(#[from] std::num::ParseIntError), + #[error("invalid invite code")] InvalidInviteCode, + #[error("invalid broadcast message")] InvalidBroadcastMessage, - CryptoFormat(crate::crypto::Error), - Io(std::io::Error), + #[error("invalid crypto format")] + CryptoFormat(#[from] crate::crypto::Error), + #[error("i/o")] + Io(#[from] std::io::Error), } -impl From for Error { - fn from(err: crate::crypto::Error) -> Self { - Error::CryptoFormat(err) - } -} - -impl From for Error { - fn from(err: std::num::ParseIntError) -> Self { - Error::ParseInt(err) - } -} - -impl From for Error { - fn from(err: std::io::Error) -> Self { - Error::Io(err) - } -} - -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{:?}", self) - } -} -impl std::error::Error for Error {} - pub type Result = std::result::Result; diff --git a/src/feed/error.rs b/src/feed/error.rs index e7f9af4..aed9ec5 100644 --- a/src/feed/error.rs +++ b/src/feed/error.rs @@ -1,49 +1,35 @@ -#[derive(Debug)] +use thiserror::Error; + +#[derive(Error, Debug)] pub enum Error { - Base64Decode(base64::DecodeError), + #[error("base64 decoding")] + Base64Decode(#[from] base64::DecodeError), + #[error("feed digest mismatch")] FeedDigestMismatch, - SystemTimeError(std::time::SystemTimeError), + #[error("time error")] + SystemTimeError(#[from] std::time::SystemTimeError), + #[error("invalid json")] InvalidJson, + #[error("invalid signature")] InvalidSignature, + #[error("failed to decipher")] FailedToDecipher, + #[error("cannot create key")] CannotCreateKey, + #[error("cannot read nonce")] CannotReadNonce, + #[error("crypto scalar mult failed")] CryptoScalarMultFailed, + #[error("empty plaintext")] EmptyPlaintext, + #[error("bad recipent")] BadRecipientCount, + #[error("invalid key from group")] CryptoKeyFromGrupFailed, - CryptoFormat(crate::crypto::Error), - Json(serde_json::Error), + #[error("invalid key format")] + CryptoFormat(#[from] crate::crypto::Error), + #[error("invalid json")] + Json(#[from] serde_json::Error), } -impl From for Error { - fn from(err: base64::DecodeError) -> Self { - Error::Base64Decode(err) - } -} -impl From for Error { - fn from(err: crate::crypto::Error) -> Self { - Error::CryptoFormat(err) - } -} - -impl From for Error { - fn from(err: std::time::SystemTimeError) -> Self { - Error::SystemTimeError(err) - } -} - -impl From for Error { - fn from(err: serde_json::Error) -> Self { - Error::Json(err) - } -} - -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{:?}", self) - } -} -impl std::error::Error for Error {} - pub type Result = std::result::Result; diff --git a/src/keystore/error.rs b/src/keystore/error.rs index 4566904..4867c19 100644 --- a/src/keystore/error.rs +++ b/src/keystore/error.rs @@ -1,34 +1,17 @@ -#[derive(Debug)] +use thiserror::Error; + +#[derive(Error, Debug)] pub enum Error { + #[error("$HOME not found")] HomeNotFound, + #[error("invalid configuration file")] InvalidConfig, - Serde(serde_json::Error), - CryptoFormat(crate::crypto::Error), - SyncIo(std::io::Error), + #[error("json deserialization")] + Serde(#[from] serde_json::Error), + #[error("crypto format")] + CryptoFormat(#[from] crate::crypto::Error), + #[error("i/o")] + SyncIo(#[from] std::io::Error), } -impl From for Error { - fn from(err: crate::crypto::Error) -> Self { - Error::CryptoFormat(err) - } -} - -impl From for Error { - fn from(err: std::io::Error) -> Self { - Error::SyncIo(err) - } -} - -impl From for Error { - fn from(err: serde_json::Error) -> Self { - Error::Serde(err) - } -} - -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{:?}", self) - } -} -impl std::error::Error for Error {} pub type Result = std::result::Result; diff --git a/src/lib.rs b/src/lib.rs index 7948eb5..a6764a4 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,6 +4,7 @@ pub extern crate kuska_handshake as handshake; extern crate serde; extern crate async_std; extern crate serde_json; +extern crate thiserror; pub mod api; pub mod crypto; diff --git a/src/rpc/error.rs b/src/rpc/error.rs index 9148f1e..7dd3f59 100644 --- a/src/rpc/error.rs +++ b/src/rpc/error.rs @@ -1,28 +1,15 @@ -#[derive(Debug)] +use thiserror::Error; + +#[derive(Error, Debug)] pub enum Error { + #[error("header size too small")] HeaderSizeTooSmall, + #[error("invalid body type: {0}")] InvalidBodyType(u8), - Io(async_std::io::Error), - Json(serde_json::Error), + #[error("i/o")] + Io(#[from] async_std::io::Error), + #[error("json decoding")] + Json(#[from] serde_json::Error), } -impl From for Error { - fn from(err: async_std::io::Error) -> Self { - Error::Io(err) - } -} - -impl From for Error { - fn from(err: serde_json::Error) -> Self { - Error::Json(err) - } -} - -impl std::fmt::Display for Error { - fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result { - write!(f, "{:?}", self) - } -} -impl std::error::Error for Error {} - pub type Result = std::result::Result;