Skip to content

Commit 5f9aa6d

Browse files
committed
Fix deserialisation of Token type (#3086)
The `Deserialize` implementation neglects to add the `Bot ` prefix to the string when it is deserialised. This adds `TryFrom` implementations for `&str` and `String` and tells serde to deserialise `Token` using the `TryFrom<&str>` implementation, which will prepend the `Bot ` prefix. Fixes #3085
1 parent c9d4329 commit 5f9aa6d

File tree

1 file changed

+24
-1
lines changed

1 file changed

+24
-1
lines changed

src/secrets.rs

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ impl typesize::TypeSize for SecretString {
4949
/// A type for securely storing and passing around a Discord token.
5050
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
5151
#[derive(Clone, Debug, Deserialize, Serialize)]
52+
#[serde(try_from = "&str")]
5253
pub struct Token(SecretString);
5354

5455
impl Token {
@@ -119,7 +120,29 @@ impl FromStr for Token {
119120
}
120121
}
121122

122-
/// Error that can be returned by [`Token::from_str`] or [`Token::from_env`].
123+
/// Parses a token and validates that is is likely in a valid format.
124+
///
125+
/// Refer to the [`Token::from_str`] implementation for details.
126+
impl TryFrom<&str> for Token {
127+
type Error = TokenError;
128+
129+
fn try_from(s: &str) -> Result<Self, Self::Error> {
130+
s.parse()
131+
}
132+
}
133+
134+
/// Parses a token and validates that is is likely in a valid format.
135+
///
136+
/// Refer to the [`Token::from_str`] implementation for details.
137+
impl TryFrom<String> for Token {
138+
type Error = TokenError;
139+
140+
fn try_from(s: String) -> Result<Self, Self::Error> {
141+
s.parse()
142+
}
143+
}
144+
145+
/// Error that can be returned by [`Token::from_str`], [`Token::try_from`], or [`Token::from_env`].
123146
#[derive(Debug)]
124147
pub enum TokenError {
125148
Env(VarError),

0 commit comments

Comments
 (0)