pub struct PublicKey(/* private fields */);Expand description
A validated age public key.
Wraps a string that is guaranteed to start with "age1" (the standard age
recipient prefix). Construction fails if the provided string does not meet
this requirement.
§Invariants
- The inner string is never empty.
- The inner string always starts with
"age1".
§Examples
use age_setup::PublicKey;
let pk = PublicKey::new("age1abcdef".into())?;
assert_eq!(pk.expose(), "age1abcdef");Invalid input:
ⓘ
use age_setup::PublicKey;
// This will not compile because `new` returns a Result.
let pk: PublicKey = PublicKey::new("bad".into());Implementations§
Source§impl PublicKey
impl PublicKey
Sourcepub fn new(raw: String) -> Result<Self>
pub fn new(raw: String) -> Result<Self>
Creates a new PublicKey after validating the age prefix.
The provided raw string must start with "age1" and must not be empty.
§Errors
Returns Error::Validation with
ValidationError::InvalidPublicKeyFormat
if the key is empty or does not start with "age1".
§Examples
use age_setup::PublicKey;
assert!(PublicKey::new("age1valid".into()).is_ok());
assert!(PublicKey::new("invalid".into()).is_err());
assert!(PublicKey::new("".into()).is_err());Sourcepub fn expose(&self) -> &str
pub fn expose(&self) -> &str
Returns a reference to the underlying public key string.
This intentionally does not implement AsRef<str> directly
(though it is provided via a separate impl) to discourage accidental
logging. Use this method explicitly when you need the raw value.
§Examples
use age_setup::PublicKey;
let pk = PublicKey::new("age1secret".into())?;
assert_eq!(pk.expose(), "age1secret");Trait Implementations§
Auto Trait Implementations§
impl Freeze for PublicKey
impl RefUnwindSafe for PublicKey
impl Send for PublicKey
impl Sync for PublicKey
impl Unpin for PublicKey
impl UnsafeUnpin for PublicKey
impl UnwindSafe for PublicKey
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more