mangadex_api_types_rust/
username.rs

1use serde::{Deserialize, Serialize};
2
3use crate::error::{Error, Result};
4
5const MIN_LEN: usize = 1;
6const MAX_LEN: usize = 64;
7
8/// Type to ensure usernames sent to the API are valid.
9#[derive(Debug, Serialize, Clone, Deserialize)]
10pub struct Username(String);
11
12impl Username {
13    /// Validate and instantiate a new `Username`.
14    pub fn parse<T: Into<String>>(username: T) -> Result<Self> {
15        let username = username.into();
16
17        // The length checks should check grapheme count instead of raw character count.
18        let is_too_short = username.len() < MIN_LEN;
19
20        let is_too_long = username.len() > MAX_LEN;
21
22        if is_too_short || is_too_long {
23            Err(Error::UsernameError(format!(
24                "The username must be between {MIN_LEN} and {MAX_LEN} characters"
25            )))
26        } else {
27            Ok(Self(username))
28        }
29    }
30}
31
32#[cfg(feature = "async-graphql")]
33#[cfg_attr(feature = "async-graphql", async_graphql::Scalar)]
34impl async_graphql::ScalarType for Username {
35    fn parse(value: async_graphql::Value) -> async_graphql::InputValueResult<Self> {
36        if let async_graphql::Value::String(username) = value {
37            Ok(Username::parse(username)?)
38        } else {
39            Err(async_graphql::InputValueError::expected_type(value))
40        }
41    }
42
43    fn to_value(&self) -> async_graphql::Value {
44        async_graphql::Value::String(self.as_ref().to_string())
45    }
46}
47
48impl AsRef<str> for Username {
49    fn as_ref(&self) -> &str {
50        &self.0
51    }
52}
53
54#[cfg(test)]
55mod tests {
56    use super::*;
57
58    #[test]
59    fn username_fewer_than_1_char_parses_error() {
60        let short_username = Username::parse("a".repeat(MIN_LEN - 1));
61
62        assert!(short_username.is_err());
63    }
64
65    #[test]
66    fn password_more_than_64_char_parses_error() {
67        let long_username = Username::parse("a".repeat(MAX_LEN + 1));
68
69        assert!(long_username.is_err());
70    }
71}