thoth_api/series/
model.rs

1use serde::{Deserialize, Serialize};
2use std::fmt;
3use std::str::FromStr;
4use uuid::Uuid;
5
6use crate::errors::ThothError;
7#[cfg(feature = "backend")]
8use crate::schema::series;
9
10#[cfg_attr(feature = "backend", derive(DbEnum, juniper::GraphQLEnum))]
11#[cfg_attr(feature = "backend", DieselType = "Series_type")]
12#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
13#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
14pub enum SeriesType {
15    Journal,
16    #[cfg_attr(feature = "backend", db_rename = "book-series")]
17    BookSeries,
18}
19
20#[cfg_attr(feature = "backend", derive(Queryable))]
21pub struct Series {
22    pub series_id: Uuid,
23    pub series_type: SeriesType,
24    pub series_name: String,
25    pub issn_print: String,
26    pub issn_digital: String,
27    pub series_url: Option<String>,
28    pub imprint_id: Uuid,
29}
30
31#[cfg_attr(
32    feature = "backend",
33    derive(juniper::GraphQLInputObject, Insertable),
34    table_name = "series"
35)]
36pub struct NewSeries {
37    pub series_type: SeriesType,
38    pub series_name: String,
39    pub issn_print: String,
40    pub issn_digital: String,
41    pub series_url: Option<String>,
42    pub imprint_id: Uuid,
43}
44
45#[cfg_attr(
46    feature = "backend",
47    derive(juniper::GraphQLInputObject, AsChangeset),
48    changeset_options(treat_none_as_null = "true"),
49    table_name = "series"
50)]
51pub struct PatchSeries {
52    pub series_id: Uuid,
53    pub series_type: SeriesType,
54    pub series_name: String,
55    pub issn_print: String,
56    pub issn_digital: String,
57    pub series_url: Option<String>,
58    pub imprint_id: Uuid,
59}
60
61impl Default for SeriesType {
62    fn default() -> SeriesType {
63        SeriesType::BookSeries
64    }
65}
66
67impl fmt::Display for SeriesType {
68    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
69        match self {
70            SeriesType::Journal => write!(f, "Journal"),
71            SeriesType::BookSeries => write!(f, "Book Series"),
72        }
73    }
74}
75
76impl FromStr for SeriesType {
77    type Err = ThothError;
78
79    fn from_str(input: &str) -> Result<SeriesType, ThothError> {
80        match input {
81            "Journal" => Ok(SeriesType::Journal),
82            "Book Series" => Ok(SeriesType::BookSeries),
83            _ => Err(ThothError::InvalidSeriesType(input.to_string())),
84        }
85    }
86}
87
88#[test]
89fn test_seriestype_default() {
90    let seriestype: SeriesType = Default::default();
91    assert_eq!(seriestype, SeriesType::BookSeries);
92}
93
94#[test]
95fn test_seriestype_display() {
96    assert_eq!(format!("{}", SeriesType::Journal), "Journal");
97    assert_eq!(format!("{}", SeriesType::BookSeries), "Book Series");
98}
99
100#[test]
101fn test_seriestype_fromstr() {
102    assert_eq!(
103        SeriesType::from_str("Journal").unwrap(),
104        SeriesType::Journal
105    );
106    assert_eq!(
107        SeriesType::from_str("Book Series").unwrap(),
108        SeriesType::BookSeries
109    );
110
111    assert!(SeriesType::from_str("bookseries").is_err());
112    assert!(SeriesType::from_str("Collection").is_err());
113}