winget_types/locale/
author.rs1use core::{fmt, str::FromStr};
2
3use compact_str::CompactString;
4use thiserror::Error;
5
6#[derive(Clone, Debug, Eq, PartialEq, Ord, PartialOrd, Hash)]
7#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
8#[cfg_attr(feature = "serde", serde(try_from = "&str"))]
9#[repr(transparent)]
10pub struct Author(CompactString);
11
12#[derive(Debug, Error, Eq, PartialEq)]
13pub enum AuthorError {
14 #[error(
15 "Author must have at least {} characters but has {_0}",
16 Author::MIN_CHAR_LENGTH
17 )]
18 TooShort(usize),
19 #[error(
20 "Author must not have more than {} characters but has {_0}",
21 Author::MAX_CHAR_LENGTH
22 )]
23 TooLong(usize),
24}
25
26impl Author {
27 pub const MIN_CHAR_LENGTH: usize = 2;
28 pub const MAX_CHAR_LENGTH: usize = 256;
29
30 pub fn new<T: AsRef<str> + Into<CompactString>>(author: T) -> Result<Self, AuthorError> {
51 match author.as_ref().chars().count() {
52 count if count < Self::MIN_CHAR_LENGTH => Err(AuthorError::TooShort(count)),
53 count if count > Self::MAX_CHAR_LENGTH => Err(AuthorError::TooLong(count)),
54 _ => Ok(Self(author.into())),
55 }
56 }
57
58 #[must_use]
65 #[inline]
66 pub unsafe fn new_unchecked<T: Into<CompactString>>(author: T) -> Self {
67 Self(author.into())
68 }
69
70 #[must_use]
72 #[inline]
73 pub fn as_str(&self) -> &str {
74 self.0.as_str()
75 }
76}
77
78impl AsRef<str> for Author {
79 #[inline]
80 fn as_ref(&self) -> &str {
81 self.as_str()
82 }
83}
84
85impl fmt::Display for Author {
86 fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
87 self.0.fmt(f)
88 }
89}
90
91impl FromStr for Author {
92 type Err = AuthorError;
93
94 #[inline]
95 fn from_str(s: &str) -> Result<Self, Self::Err> {
96 Self::new(s)
97 }
98}
99
100impl TryFrom<&str> for Author {
101 type Error = AuthorError;
102
103 #[inline]
104 fn try_from(value: &str) -> Result<Self, Self::Error> {
105 Self::new(value)
106 }
107}