use alloc::string::String;
use core::fmt;
use super::{FromField, ValueError};
pub struct Secret(String);
impl Secret {
#[must_use]
pub fn expose(&self) -> &str {
&self.0
}
#[must_use]
pub fn len(&self) -> usize {
self.0.len()
}
#[must_use]
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
}
impl From<String> for Secret {
fn from(s: String) -> Secret {
Secret(s)
}
}
impl fmt::Debug for Secret {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str("Secret(..)")
}
}
impl Drop for Secret {
fn drop(&mut self) {
crate::wipe::wipe_string(&mut self.0);
}
}
impl FromField for Secret {
type Slot = Option<Secret>;
fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError> {
if slot.is_some() {
return Err(ValueError::Duplicate);
}
*slot = Some(Secret(String::from(value)));
Ok(())
}
fn finish(slot: Self::Slot) -> Result<Self, ValueError> {
slot.ok_or(ValueError::Missing)
}
}