Authentication service
This commit is contained in:
+29
-2
@@ -1,4 +1,4 @@
|
||||
pub type Result<T = ()> = std::result::Result<T, Box<dyn std::error::Error>>;
|
||||
pub type Result<T = (), E = Box<dyn std::error::Error>> = std::result::Result<T, E>;
|
||||
|
||||
#[allow(async_fn_in_trait)]
|
||||
pub trait CRUD<C> {
|
||||
@@ -17,7 +17,34 @@ pub trait CRUD<C> {
|
||||
async fn delete(connection: &mut C, data: Self::Unique) -> Result;
|
||||
}
|
||||
|
||||
const TOO_LONG: &str = "too long";
|
||||
trait CharLength {
|
||||
fn length(&self) -> usize;
|
||||
}
|
||||
impl CharLength for String {
|
||||
fn length(&self) -> usize {
|
||||
self.chars().count()
|
||||
}
|
||||
}
|
||||
impl CharLength for Option<String> {
|
||||
fn length(&self) -> usize {
|
||||
self.as_ref().map_or(0, CharLength::length)
|
||||
}
|
||||
}
|
||||
|
||||
trait MaxLength {
|
||||
type Inner: CharLength;
|
||||
const MAX_LENGTH: usize;
|
||||
|
||||
fn validate(value: &Self::Inner) -> Result<(), &'static str> {
|
||||
if value.length() > Self::MAX_LENGTH {
|
||||
Err("too long")
|
||||
} else {
|
||||
Ok(())
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// const TOO_LONG: &str = "too long";
|
||||
|
||||
pub mod base;
|
||||
pub mod package;
|
||||
|
||||
+14
-12
@@ -1,3 +1,4 @@
|
||||
use super::MaxLength;
|
||||
pub use super::{CRUD, Result};
|
||||
|
||||
pub use chrono::{DateTime, Utc};
|
||||
@@ -14,29 +15,30 @@ pub trait BaseRepository<C>:
|
||||
|
||||
#[derive(Clone, Deref, Into)]
|
||||
pub struct Name(String);
|
||||
impl MaxLength for Name {
|
||||
type Inner = String;
|
||||
const MAX_LENGTH: usize = 127;
|
||||
}
|
||||
impl TryFrom<String> for Name {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(value: String) -> std::result::Result<Self, Self::Error> {
|
||||
if value.chars().count() > 127 {
|
||||
Err(super::TOO_LONG)
|
||||
} else {
|
||||
Ok(Self(value))
|
||||
}
|
||||
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||
Self::validate(&value)?;
|
||||
Ok(Self(value))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Deref, Into)]
|
||||
pub struct Description(Option<String>);
|
||||
impl MaxLength for Description {
|
||||
type Inner = Option<String>;
|
||||
const MAX_LENGTH: usize = 510;
|
||||
}
|
||||
impl TryFrom<Option<String>> for Description {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(value: Option<String>) -> std::result::Result<Self, Self::Error> {
|
||||
if let Some(x) = &value {
|
||||
if x.chars().count() > 510 {
|
||||
return Err(super::TOO_LONG);
|
||||
}
|
||||
}
|
||||
fn try_from(value: Option<String>) -> Result<Self, Self::Error> {
|
||||
Self::validate(&value)?;
|
||||
Ok(Self(value))
|
||||
}
|
||||
}
|
||||
|
||||
@@ -1,3 +1,4 @@
|
||||
use super::MaxLength;
|
||||
pub use super::{CRUD, Result, base::Base};
|
||||
|
||||
pub use chrono::{DateTime, Utc};
|
||||
@@ -11,58 +12,60 @@ pub trait PackageRepository<C>:
|
||||
|
||||
#[derive(Clone, Deref, Into)]
|
||||
pub struct Name(String);
|
||||
impl MaxLength for Name {
|
||||
type Inner = String;
|
||||
const MAX_LENGTH: usize = 127;
|
||||
}
|
||||
impl TryFrom<String> for Name {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(value: String) -> std::result::Result<Self, Self::Error> {
|
||||
if value.chars().count() > 127 {
|
||||
Err(super::TOO_LONG)
|
||||
} else {
|
||||
Ok(Self(value))
|
||||
}
|
||||
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||
Self::validate(&value)?;
|
||||
Ok(Self(value))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Deref, Into)]
|
||||
pub struct Version(String);
|
||||
impl MaxLength for Version {
|
||||
type Inner = String;
|
||||
const MAX_LENGTH: usize = 127;
|
||||
}
|
||||
impl TryFrom<String> for Version {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(value: String) -> std::result::Result<Self, Self::Error> {
|
||||
if value.chars().count() > 127 {
|
||||
Err(super::TOO_LONG)
|
||||
} else {
|
||||
Ok(Self(value))
|
||||
}
|
||||
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||
Self::validate(&value)?;
|
||||
Ok(Self(value))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Deref, Into)]
|
||||
pub struct Description(Option<String>);
|
||||
impl MaxLength for Description {
|
||||
type Inner = Option<String>;
|
||||
const MAX_LENGTH: usize = 255;
|
||||
}
|
||||
impl TryFrom<Option<String>> for Description {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(value: Option<String>) -> std::result::Result<Self, Self::Error> {
|
||||
if let Some(x) = &value {
|
||||
if x.chars().count() > 255 {
|
||||
return Err(super::TOO_LONG);
|
||||
}
|
||||
}
|
||||
fn try_from(value: Option<String>) -> Result<Self, Self::Error> {
|
||||
Self::validate(&value)?;
|
||||
Ok(Self(value))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Deref, Into)]
|
||||
pub struct URL(Option<String>);
|
||||
impl MaxLength for URL {
|
||||
type Inner = Option<String>;
|
||||
const MAX_LENGTH: usize = 510;
|
||||
}
|
||||
impl TryFrom<Option<String>> for URL {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(value: Option<String>) -> std::result::Result<Self, Self::Error> {
|
||||
if let Some(x) = &value {
|
||||
if x.chars().count() > 510 {
|
||||
return Err(super::TOO_LONG);
|
||||
}
|
||||
}
|
||||
fn try_from(value: Option<String>) -> Result<Self, Self::Error> {
|
||||
Self::validate(&value)?;
|
||||
Ok(Self(value))
|
||||
}
|
||||
}
|
||||
|
||||
+22
-18
@@ -1,3 +1,4 @@
|
||||
use super::MaxLength;
|
||||
pub use super::{CRUD, Result};
|
||||
|
||||
pub use chrono::{DateTime, Utc};
|
||||
@@ -11,43 +12,46 @@ pub trait UserRepository<C>:
|
||||
|
||||
#[derive(Clone, Deref, Into)]
|
||||
pub struct Name(String);
|
||||
impl MaxLength for Name {
|
||||
type Inner = String;
|
||||
const MAX_LENGTH: usize = 31;
|
||||
}
|
||||
impl TryFrom<String> for Name {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(value: String) -> std::result::Result<Self, Self::Error> {
|
||||
if value.chars().count() > 31 {
|
||||
Err(super::TOO_LONG)
|
||||
} else {
|
||||
Ok(Self(value))
|
||||
}
|
||||
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||
Self::validate(&value)?;
|
||||
Ok(Self(value))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Deref, Into)]
|
||||
pub struct Email(String);
|
||||
impl MaxLength for Email {
|
||||
type Inner = String;
|
||||
const MAX_LENGTH: usize = 255;
|
||||
}
|
||||
impl TryFrom<String> for Email {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(value: String) -> std::result::Result<Self, Self::Error> {
|
||||
if value.chars().count() > 255 {
|
||||
Err(super::TOO_LONG)
|
||||
} else {
|
||||
Ok(Self(value))
|
||||
}
|
||||
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||
Self::validate(&value)?;
|
||||
Ok(Self(value))
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Deref, Into)]
|
||||
pub struct Password(String);
|
||||
impl MaxLength for Password {
|
||||
type Inner = String;
|
||||
const MAX_LENGTH: usize = 255;
|
||||
}
|
||||
impl TryFrom<String> for Password {
|
||||
type Error = &'static str;
|
||||
|
||||
fn try_from(value: String) -> std::result::Result<Self, Self::Error> {
|
||||
if value.chars().count() > 255 {
|
||||
Err(super::TOO_LONG)
|
||||
} else {
|
||||
Ok(Self(value))
|
||||
}
|
||||
fn try_from(value: String) -> Result<Self, Self::Error> {
|
||||
Self::validate(&value)?;
|
||||
Ok(Self(value))
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user