1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
//! 一个密码类,String 的壳子,用来在调试输出时挡住真实密码,防止泄露

use std::{
    fmt::{Debug, Display},
    ops::Deref,
};

use serde::{de::Visitor, Deserialize, Deserializer, Serialize};

/// 一个密码类,String 的壳子,用来在调试输出时挡住真实密码,防止泄露
///
/// 任何格式化输出都会返回 `***Password***`,所以如果需要取用密码,请使用 [`Password::take_string`] [`Password::to_owned_string`] [`Password::as_string`]
#[derive(Clone, PartialEq, Eq, Default)]
pub struct Password(String);

impl Serialize for Password {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: serde::Serializer,
    {
        serializer.serialize_str(&self.0)
    }
}

impl<'de> Deserialize<'de> for Password {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        struct PasswordVisitor;
        impl<'de> Visitor<'de> for PasswordVisitor {
            type Value = Password;
            fn expecting(&self, formatter: &mut std::fmt::Formatter) -> std::fmt::Result {
                formatter.write_str("a password as string")
            }

            fn visit_borrowed_str<E>(self, v: &'de str) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                Ok(Password(v.to_string()))
            }

            fn visit_str<E>(self, v: &str) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                Ok(Password(v.to_string()))
            }

            fn visit_string<E>(self, v: String) -> Result<Self::Value, E>
            where
                E: serde::de::Error,
            {
                Ok(Password(v.to_string()))
            }
        }
        deserializer.deserialize_str(PasswordVisitor)
    }
}

impl Password {
    /// 从密码类中拿出原始字符串,请注意保护密码安全
    pub fn take_string(self) -> String {
        self.0
    }

    /// 从密码类中复制出原始字符串,请注意保护密码安全
    pub fn to_owned_string(&self) -> String {
        self.0.to_owned()
    }

    /// 从密码类中借出原始字符串,请注意保护密码安全
    pub fn as_string(&self) -> &String {
        &self.0
    }
}

impl Debug for Password {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("***Password***")
    }
}

impl Display for Password {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str("***Password***")
    }
}

impl Deref for Password {
    type Target = String;
    fn deref(&self) -> &String {
        &self.0
    }
}

impl From<Password> for String {
    fn from(a: Password) -> Self {
        a.0
    }
}

impl From<String> for Password {
    fn from(a: String) -> Self {
        Self(a)
    }
}

impl From<&str> for Password {
    fn from(a: &str) -> Self {
        Self(a.to_owned())
    }
}