mangadex_api_types_rust/
username.rs1use serde::{Deserialize, Serialize};
2
3use crate::error::{Error, Result};
4
5const MIN_LEN: usize = 1;
6const MAX_LEN: usize = 64;
7
8#[derive(Debug, Serialize, Clone, Deserialize)]
10pub struct Username(String);
11
12impl Username {
13 pub fn parse<T: Into<String>>(username: T) -> Result<Self> {
15 let username = username.into();
16
17 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 {} and {} characters",
25 MIN_LEN, MAX_LEN
26 )))
27 } else {
28 Ok(Self(username))
29 }
30 }
31}
32
33#[cfg(feature = "async-graphql")]
34#[cfg_attr(feature = "async-graphql", async_graphql::Scalar)]
35impl async_graphql::ScalarType for Username {
36 fn parse(value: async_graphql::Value) -> async_graphql::InputValueResult<Self> {
37 if let async_graphql::Value::String(username) = value {
38 Ok(Username::parse(username)?)
39 } else {
40 Err(async_graphql::InputValueError::expected_type(value))
41 }
42 }
43
44 fn to_value(&self) -> async_graphql::Value {
45 async_graphql::Value::String(self.as_ref().to_string())
46 }
47}
48
49impl AsRef<str> for Username {
50 fn as_ref(&self) -> &str {
51 &self.0
52 }
53}
54
55#[cfg(test)]
56mod tests {
57 use super::*;
58
59 #[test]
60 fn username_fewer_than_1_char_parses_error() {
61 let short_username = Username::parse("a".repeat(MIN_LEN - 1));
62
63 assert!(short_username.is_err());
64 }
65
66 #[test]
67 fn password_more_than_64_char_parses_error() {
68 let long_username = Username::parse("a".repeat(MAX_LEN + 1));
69
70 assert!(long_username.is_err());
71 }
72}