form_net/extract/
secret.rs1use alloc::string::String;
4use core::fmt;
5
6use super::{FromField, ValueError};
7
8pub struct Secret(String);
25
26impl Secret {
27 #[must_use]
29 pub fn expose(&self) -> &str {
30 &self.0
31 }
32
33 #[must_use]
35 pub fn len(&self) -> usize {
36 self.0.len()
37 }
38
39 #[must_use]
41 pub fn is_empty(&self) -> bool {
42 self.0.is_empty()
43 }
44}
45
46impl From<String> for Secret {
47 fn from(s: String) -> Secret {
48 Secret(s)
49 }
50}
51
52impl fmt::Debug for Secret {
53 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
54 f.write_str("Secret(..)")
55 }
56}
57
58impl Drop for Secret {
59 fn drop(&mut self) {
60 crate::wipe::wipe_string(&mut self.0);
61 }
62}
63
64impl FromField for Secret {
65 type Slot = Option<Secret>;
66
67 fn accept_text(slot: &mut Self::Slot, value: &str) -> Result<(), ValueError> {
68 if slot.is_some() {
69 return Err(ValueError::Duplicate);
70 }
71 *slot = Some(Secret(String::from(value)));
72 Ok(())
73 }
74
75 fn finish(slot: Self::Slot) -> Result<Self, ValueError> {
76 slot.ok_or(ValueError::Missing)
77 }
78}