refactor(identity): follow naming conventions for conversion methods

This PR renames some method names that don't follow Rust naming conventions or behave differently from what the name suggests:
- Enforce "try" prefix on all methods that return `Result`.
- Enforce "encode" method name for methods that return encoded bytes.
- Enforce "to_bytes" method name for methods that return raw bytes.
- Enforce "decode" method name for methods that convert encoded key.
- Enforce "from_bytes" method name for methods that convert raw bytes.

Pull-Request: #3775.
This commit is contained in:
DrHuangMHT
2023-04-14 16:55:13 +08:00
committed by GitHub
parent 8ffcff9624
commit 058c2d85ec
23 changed files with 490 additions and 109 deletions

View File

@ -23,6 +23,8 @@
use std::error::Error;
use std::fmt;
use crate::KeyType;
/// An error during decoding of key material.
#[derive(Debug)]
pub struct DecodingError {
@ -156,3 +158,26 @@ impl Error for SigningError {
self.source.as_ref().map(|s| &**s as &dyn Error)
}
}
/// Error produced when failing to convert [`Keypair`](crate::Keypair) to a more concrete keypair.
#[derive(Debug)]
pub struct OtherVariantError {
actual: KeyType,
}
impl OtherVariantError {
pub(crate) fn new(actual: KeyType) -> OtherVariantError {
OtherVariantError { actual }
}
}
impl fmt::Display for OtherVariantError {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&format!(
"Cannot convert to the given type, the actual key type inside is {}",
self.actual
))
}
}
impl Error for OtherVariantError {}