-
Notifications
You must be signed in to change notification settings - Fork 4
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
5 changed files
with
154 additions
and
30 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,54 @@ | ||
use std::borrow::Cow; | ||
|
||
use serde::{Deserialize, Serialize}; | ||
use thiserror::Error; | ||
|
||
#[derive(Error, Debug)] | ||
pub enum CategoryError { | ||
#[error("not empty validation is violated")] | ||
NotEmptyViolated, | ||
#[error("len max validation is violated")] | ||
LenMaxViolated, | ||
} | ||
|
||
#[derive(Clone, Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
pub struct Category<'a>(Cow<'a, str>); | ||
|
||
impl<'a> Category<'a> { | ||
const MAX_LEN: usize = 30; | ||
pub fn new(c: impl Into<Cow<'a, str>>) -> Result<Self, CategoryError> { | ||
let c = c.into().trim().to_ascii_lowercase(); | ||
|
||
match c.len() { | ||
0 => return Err(CategoryError::NotEmptyViolated), | ||
n if n > Self::MAX_LEN => return Err(CategoryError::LenMaxViolated), | ||
_ => {} | ||
} | ||
|
||
Ok(Self(c.into())) | ||
} | ||
|
||
pub fn into_inner(self) -> Cow<'a, str> { | ||
self.0 | ||
} | ||
} | ||
|
||
#[cfg(feature = "graphql")] | ||
#[async_graphql::Scalar] | ||
impl<'s> async_graphql::ScalarType for Category<'s> { | ||
fn parse(value: async_graphql::Value) -> async_graphql::InputValueResult<Self> { | ||
let async_graphql::Value::String(s) = value else { | ||
return Err(async_graphql::InputValueError::expected_type(value)); | ||
}; | ||
|
||
match Category::new(s) { | ||
Ok(c) => Ok(c), | ||
Err(err) => Err(async_graphql::InputValueError::custom(err)), | ||
} | ||
} | ||
|
||
fn to_value(&self) -> async_graphql::Value { | ||
// Is this clone avoidable? | ||
async_graphql::Value::String(self.0.clone().into_owned()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
use serde::{Deserialize, Serialize}; | ||
use std::str::FromStr; | ||
|
||
/// `Requirement` expresses how important the feed is | ||
/// using an analogy to [RFC2119](https://datatracker.ietf.org/doc/html/rfc2119) | ||
#[derive(Clone, Copy, Debug, PartialEq, Eq, Serialize, Deserialize)] | ||
#[cfg_attr(feature = "graphql", derive(async_graphql::Enum))] | ||
pub enum Requirement { | ||
/// `Must` indicates it must be read | ||
Must, | ||
/// `Should` suggests it should be read unless there is a special reason not to | ||
Should, | ||
/// `May` implies it is probably worth reading | ||
May, | ||
} | ||
|
||
impl FromStr for Requirement { | ||
type Err = &'static str; | ||
|
||
fn from_str(s: &str) -> Result<Self, Self::Err> { | ||
match s { | ||
_ if s.eq_ignore_ascii_case("MUST") => Ok(Requirement::Must), | ||
_ if s.eq_ignore_ascii_case("SHOULD") => Ok(Requirement::Should), | ||
_ if s.eq_ignore_ascii_case("MAY") => Ok(Requirement::May), | ||
_ => Err("invalid requirement, should be one of ['must', 'should', 'may']"), | ||
} | ||
} | ||
} |