Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Move enum to common types #24

Merged
merged 3 commits into from
May 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 2 additions & 3 deletions src/bin/import_from_v1_backup.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use bookshelf_api::{
common::types::{BookFormat, BookStore},
domain::entity::{
author::{Author, AuthorId, AuthorName},
book::{
Book, BookFormat, BookId, BookStore, BookTitle, Isbn, OwnedFlag, Priority, ReadFlag,
},
book::{Book, BookId, BookTitle, Isbn, OwnedFlag, Priority, ReadFlag},
user::UserId,
},
infrastructure::{
Expand Down
1 change: 1 addition & 0 deletions src/common.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub mod types;
82 changes: 82 additions & 0 deletions src/common/types.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,82 @@
use derive_more::Display;
use thiserror::Error;

#[derive(Debug, Clone, PartialEq, Eq, Display)]
pub enum BookFormat {
#[display(fmt = "eBook")]
EBook,
Printed,
Unknown,
}

impl TryFrom<&str> for BookFormat {
type Error = ParseBookFormatError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"eBook" => Ok(BookFormat::EBook),
"Printed" => Ok(BookFormat::Printed),
"Unknown" => Ok(BookFormat::Unknown),
_ => Err(ParseBookFormatError(format!(
"{} is not valid format",
value
))),
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, Display)]
pub enum BookStore {
Kindle,
Unknown,
}

impl TryFrom<&str> for BookStore {
type Error = ParseBookStoreError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"Kindle" => Ok(BookStore::Kindle),
"Unknown" => Ok(BookStore::Unknown),
_ => Err(ParseBookStoreError(format!("{} is not valid store", value))),
}
}
}

#[derive(Debug, Error)]
#[error("{0}")]
pub struct ParseBookFormatError(String);

#[derive(Debug, Error)]
#[error("{0}")]
pub struct ParseBookStoreError(String);

#[cfg(test)]
mod test {
use crate::common::types::{BookFormat, BookStore};

#[test]
fn book_format_ebook_to_string() {
assert_eq!(BookFormat::EBook.to_string(), "eBook");
}

#[test]
fn book_format_printed_to_string() {
assert_eq!(BookFormat::Printed.to_string(), "Printed");
}

#[test]
fn book_format_unknown_to_string() {
assert_eq!(BookFormat::Unknown.to_string(), "Unknown");
}

#[test]
fn book_store_kindle_to_string() {
assert_eq!(BookStore::Kindle.to_string(), "Kindle");
}

#[test]
fn book_store_unknown_to_string() {
assert_eq!(BookStore::Unknown.to_string(), "Unknown");
}
}
79 changes: 5 additions & 74 deletions src/domain/entity/book.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,15 @@
use derive_more::Display;
use getset::{Getters, Setters};
use once_cell::sync::Lazy;
use regex::Regex;
use time::OffsetDateTime;
use uuid::Uuid;
use validator::Validate;

use crate::{domain::error::DomainError, impl_string_value_object};
use crate::{
common::types::{BookFormat, BookStore},
domain::error::DomainError,
impl_string_value_object,
};

use super::author::AuthorId;

Expand Down Expand Up @@ -112,51 +115,6 @@ impl Priority {
}
}

#[derive(Debug, Clone, PartialEq, Eq, Display)]
pub enum BookFormat {
#[display(fmt = "eBook")]
EBook,
Printed,
Unknown,
}

impl TryFrom<&str> for BookFormat {
type Error = DomainError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"eBook" => Ok(BookFormat::EBook),
"Printed" => Ok(BookFormat::Printed),
"Unknown" => Ok(BookFormat::Unknown),
_ => Err(DomainError::Validation(format!(
"{} is not valid format",
value
))),
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, Display)]
pub enum BookStore {
Kindle,
Unknown,
}

impl TryFrom<&str> for BookStore {
type Error = DomainError;

fn try_from(value: &str) -> Result<Self, Self::Error> {
match value {
"Kindle" => Ok(BookStore::Kindle),
"Unknown" => Ok(BookStore::Unknown),
_ => Err(DomainError::Validation(format!(
"{} is not valid store",
value
))),
}
}
}

#[derive(Debug, Clone, PartialEq, Eq, Getters, Setters)]
pub struct Book {
#[getset(get = "pub")]
Expand Down Expand Up @@ -247,8 +205,6 @@ impl Book {

#[cfg(test)]
mod test {
use crate::domain::entity::book::{BookFormat, BookStore};

use super::{Isbn, Priority};

#[test]
Expand Down Expand Up @@ -298,29 +254,4 @@ mod test {
let priority = Priority::new(101);
assert!(matches!(priority, Err(_)));
}

#[test]
fn book_format_ebook_to_string() {
assert_eq!(BookFormat::EBook.to_string(), "eBook");
}

#[test]
fn book_format_printed_to_string() {
assert_eq!(BookFormat::Printed.to_string(), "Printed");
}

#[test]
fn book_format_unknown_to_string() {
assert_eq!(BookFormat::Unknown.to_string(), "Unknown");
}

#[test]
fn book_store_kindle_to_string() {
assert_eq!(BookStore::Kindle.to_string(), "Kindle");
}

#[test]
fn book_store_unknown_to_string() {
assert_eq!(BookStore::Unknown.to_string(), "Unknown");
}
}
14 changes: 14 additions & 0 deletions src/domain/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use thiserror::Error;
use validator::ValidationErrors;

use crate::common::types::{ParseBookFormatError, ParseBookStoreError};

#[derive(Debug, Error)]
pub enum DomainError {
#[error("{0}")]
Expand All @@ -22,3 +24,15 @@ impl From<ValidationErrors> for DomainError {
DomainError::Validation(err.to_string())
}
}

impl From<ParseBookStoreError> for DomainError {
fn from(err: ParseBookStoreError) -> Self {
DomainError::Validation(err.to_string())
}
}

impl From<ParseBookFormatError> for DomainError {
fn from(err: ParseBookFormatError) -> Self {
DomainError::Validation(err.to_string())
}
}
17 changes: 9 additions & 8 deletions src/infrastructure/book_repository.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,16 +4,17 @@ use sqlx::{PgConnection, PgPool};
use time::OffsetDateTime;
use uuid::Uuid;

use crate::domain::{
entity::{
author::AuthorId,
book::{
Book, BookFormat, BookId, BookStore, BookTitle, Isbn, OwnedFlag, Priority, ReadFlag,
use crate::{
common::types::{BookFormat, BookStore},
domain::{
entity::{
author::AuthorId,
book::{Book, BookId, BookTitle, Isbn, OwnedFlag, Priority, ReadFlag},
user::UserId,
},
user::UserId,
error::DomainError,
repository::book_repository::BookRepository,
},
error::DomainError,
repository::book_repository::BookRepository,
};

#[derive(sqlx::FromRow)]
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod common;
pub mod dependency_injection;
pub mod domain;
pub mod extractors;
Expand Down
35 changes: 17 additions & 18 deletions src/presentational/graphql/object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,11 @@ use async_graphql::dataloader::DataLoader;
use async_graphql::{ComplexObject, Context, Enum, Result};
use async_graphql::{InputObject, SimpleObject, ID};

use crate::common::types::{BookFormat as CommonBookFormat, BookStore as CommonBookStore};
use crate::dependency_injection::QI;
use crate::domain;
use crate::use_case::dto::author::AuthorDto;
use crate::use_case::dto::author::CreateAuthorDto;
use crate::use_case::dto::book::{BookDto, CreateBookDto, UpdateBookDto};
use domain::entity::book::{BookFormat as DomainBookFormat, BookStore as DomainBookStore};

use super::loader::AuthorLoader;

Expand All @@ -29,22 +28,22 @@ pub enum BookFormat {
Unknown,
}

impl From<DomainBookFormat> for BookFormat {
fn from(book_format: DomainBookFormat) -> Self {
impl From<CommonBookFormat> for BookFormat {
fn from(book_format: CommonBookFormat) -> Self {
match book_format {
DomainBookFormat::EBook => BookFormat::EBook,
DomainBookFormat::Printed => BookFormat::Printed,
DomainBookFormat::Unknown => BookFormat::Unknown,
CommonBookFormat::EBook => BookFormat::EBook,
CommonBookFormat::Printed => BookFormat::Printed,
CommonBookFormat::Unknown => BookFormat::Unknown,
}
}
}

impl From<BookFormat> for DomainBookFormat {
impl From<BookFormat> for CommonBookFormat {
fn from(book_format: BookFormat) -> Self {
match book_format {
BookFormat::EBook => DomainBookFormat::EBook,
BookFormat::Printed => DomainBookFormat::Printed,
BookFormat::Unknown => DomainBookFormat::Unknown,
BookFormat::EBook => CommonBookFormat::EBook,
BookFormat::Printed => CommonBookFormat::Printed,
BookFormat::Unknown => CommonBookFormat::Unknown,
}
}
}
Expand All @@ -55,20 +54,20 @@ pub enum BookStore {
Unknown,
}

impl From<DomainBookStore> for BookStore {
fn from(book_format: DomainBookStore) -> Self {
impl From<CommonBookStore> for BookStore {
fn from(book_format: CommonBookStore) -> Self {
match book_format {
DomainBookStore::Kindle => BookStore::Kindle,
DomainBookStore::Unknown => BookStore::Unknown,
CommonBookStore::Kindle => BookStore::Kindle,
CommonBookStore::Unknown => BookStore::Unknown,
}
}
}

impl From<BookStore> for DomainBookStore {
impl From<BookStore> for CommonBookStore {
fn from(book_format: BookStore) -> Self {
match book_format {
BookStore::Kindle => DomainBookStore::Kindle,
BookStore::Unknown => DomainBookStore::Unknown,
BookStore::Kindle => CommonBookStore::Kindle,
BookStore::Unknown => CommonBookStore::Unknown,
}
}
}
Expand Down
6 changes: 2 additions & 4 deletions src/use_case/dto/book.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,13 +2,11 @@ use time::OffsetDateTime;
use uuid::Uuid;

use crate::{
common::types::{BookFormat, BookStore},
domain::{
entity::{
author::AuthorId,
book::{
Book, BookFormat, BookId, BookStore, BookTitle, DestructureBook, Isbn, OwnedFlag,
Priority, ReadFlag,
},
book::{Book, BookId, BookTitle, DestructureBook, Isbn, OwnedFlag, Priority, ReadFlag},
},
error::DomainError,
},
Expand Down