Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix deserialisation of Token type #3086

Merged
merged 1 commit into from
Jan 9, 2025
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
25 changes: 24 additions & 1 deletion src/secrets.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ impl typesize::TypeSize for SecretString {
/// A type for securely storing and passing around a Discord token.
#[cfg_attr(feature = "typesize", derive(typesize::derive::TypeSize))]
#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(try_from = "&str")]
pub struct Token(SecretString);

impl Token {
Expand Down Expand Up @@ -119,7 +120,29 @@ impl FromStr for Token {
}
}

/// Error that can be returned by [`Token::from_str`] or [`Token::from_env`].
/// Parses a token and validates that is is likely in a valid format.
///
/// Refer to the [`Token::from_str`] implementation for details.
impl TryFrom<&str> for Token {
type Error = TokenError;

fn try_from(s: &str) -> Result<Self, Self::Error> {
s.parse()
}
}

/// Parses a token and validates that is is likely in a valid format.
///
/// Refer to the [`Token::from_str`] implementation for details.
impl TryFrom<String> for Token {
type Error = TokenError;

fn try_from(s: String) -> Result<Self, Self::Error> {
s.parse()
}
}

/// Error that can be returned by [`Token::from_str`], [`Token::try_from`], or [`Token::from_env`].
#[derive(Debug)]
pub enum TokenError {
Env(VarError),
Expand Down
Loading