diff options
Diffstat (limited to 'src')
| -rw-r--r-- | src/error.rs | 19 | ||||
| -rw-r--r-- | src/util.rs | 2 | 
2 files changed, 11 insertions, 10 deletions
diff --git a/src/error.rs b/src/error.rs index 3f60af2..dfe1680 100644 --- a/src/error.rs +++ b/src/error.rs @@ -11,6 +11,8 @@ pub enum Error {      CommandError(CommandError),      /// Placeholder for testing.      CommunicationError(CommunicationError), +    /// An error that occured during random number generation. +    RandError(rand_core::Error),  }  impl From<CommandError> for Error { @@ -19,11 +21,18 @@ impl From<CommandError> for Error {      }  } +impl From<rand_core::Error> for Error { +    fn from(error: rand_core::Error) -> Self { +        Error::RandError(error) +    } +} +  impl error::Error for Error {      fn source(&self) -> Option<&(dyn error::Error + 'static)> {          match *self {              Error::CommandError(ref err) => Some(err),              Error::CommunicationError(_) => None, +            Error::RandError(ref err) => Some(err),          }      }  } @@ -33,6 +42,7 @@ impl fmt::Display for Error {          match *self {              Error::CommandError(ref err) => write!(f, "Command error: {}", err),              Error::CommunicationError(_) => write!(f, "Placeholder"), +            Error::RandError(ref err) => write!(f, "RNG error: {}", err),          }      }  } @@ -78,8 +88,6 @@ pub enum CommandError {      InvalidHexString,      /// The target buffer was smaller than the source.      TargetBufferTooSmall, -    /// An error occurred during random number generation. -    RngError,  }  /// Placeholder for testing. @@ -119,7 +127,6 @@ impl CommandError {                  "The supplied string is not in hexadecimal format".into()              }              CommandError::TargetBufferTooSmall => "The target buffer is too small".into(), -            CommandError::RngError => "An error occurred during random number generation".into(),          }      }  } @@ -153,9 +160,3 @@ impl From<raw::c_int> for CommandError {          }      }  } - -impl From<rand_core::Error> for CommandError { -    fn from(_error: rand_core::Error) -> Self { -        CommandError::RngError -    } -} diff --git a/src/util.rs b/src/util.rs index 8855275..06bb854 100644 --- a/src/util.rs +++ b/src/util.rs @@ -71,7 +71,7 @@ pub fn get_last_error() -> Error {  }  pub fn generate_password(length: usize) -> Result<Vec<u8>, Error> { -    let mut rng = OsRng::new().map_err(CommandError::from)?; +    let mut rng = OsRng::new()?;      let mut data = vec![0u8; length];      rng.fill_bytes(&mut data[..]);      Ok(data)  | 
