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::publication;
9
10#[cfg_attr(feature = "backend", derive(DbEnum, juniper::GraphQLEnum))]
11#[cfg_attr(feature = "backend", DieselType = "Publication_type")]
12#[derive(Debug, Clone, PartialEq, Deserialize, Serialize)]
13#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
14pub enum PublicationType {
15 #[cfg_attr(feature = "backend", db_rename = "Paperback")]
16 Paperback,
17 #[cfg_attr(feature = "backend", db_rename = "Hardback")]
18 Hardback,
19 #[cfg_attr(feature = "backend", db_rename = "PDF")]
20 #[serde(rename = "PDF")]
21 PDF,
22 #[cfg_attr(feature = "backend", db_rename = "HTML")]
23 #[serde(rename = "HTML")]
24 HTML,
25 #[cfg_attr(feature = "backend", db_rename = "XML")]
26 #[serde(rename = "XML")]
27 XML,
28 #[cfg_attr(feature = "backend", db_rename = "Epub")]
29 Epub,
30 #[cfg_attr(feature = "backend", db_rename = "Mobi")]
31 Mobi,
32}
33
34#[cfg_attr(feature = "backend", derive(Queryable))]
35pub struct Publication {
36 pub publication_id: Uuid,
37 pub publication_type: PublicationType,
38 pub work_id: Uuid,
39 pub isbn: Option<String>,
40 pub publication_url: Option<String>,
41}
42
43#[cfg_attr(
44 feature = "backend",
45 derive(juniper::GraphQLInputObject, Insertable),
46 table_name = "publication"
47)]
48pub struct NewPublication {
49 pub publication_type: PublicationType,
50 pub work_id: Uuid,
51 pub isbn: Option<String>,
52 pub publication_url: Option<String>,
53}
54
55#[cfg_attr(
56 feature = "backend",
57 derive(juniper::GraphQLInputObject, AsChangeset),
58 changeset_options(treat_none_as_null = "true"),
59 table_name = "publication"
60)]
61pub struct PatchPublication {
62 pub publication_id: Uuid,
63 pub publication_type: PublicationType,
64 pub work_id: Uuid,
65 pub isbn: Option<String>,
66 pub publication_url: Option<String>,
67}
68
69impl Default for PublicationType {
70 fn default() -> PublicationType {
71 PublicationType::Paperback
72 }
73}
74
75impl fmt::Display for PublicationType {
76 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
77 match self {
78 PublicationType::Paperback => write!(f, "Paperback"),
79 PublicationType::Hardback => write!(f, "Hardback"),
80 PublicationType::PDF => write!(f, "PDF"),
81 PublicationType::HTML => write!(f, "HTML"),
82 PublicationType::XML => write!(f, "XML"),
83 PublicationType::Epub => write!(f, "Epub"),
84 PublicationType::Mobi => write!(f, "Mobi"),
85 }
86 }
87}
88
89impl FromStr for PublicationType {
90 type Err = ThothError;
91
92 fn from_str(input: &str) -> Result<PublicationType, ThothError> {
93 match input {
94 "Paperback" => Ok(PublicationType::Paperback),
95 "Hardback" => Ok(PublicationType::Hardback),
96 "PDF" => Ok(PublicationType::PDF),
97 "HTML" => Ok(PublicationType::HTML),
98 "XML" => Ok(PublicationType::XML),
99 "Epub" => Ok(PublicationType::Epub),
100 "Mobi" => Ok(PublicationType::Mobi),
101 _ => Err(ThothError::InvalidPublicationType(input.to_string())),
102 }
103 }
104}
105
106#[test]
107fn test_publicationtype_default() {
108 let pubtype: PublicationType = Default::default();
109 assert_eq!(pubtype, PublicationType::Paperback);
110}
111
112#[test]
113fn test_publicationtype_display() {
114 assert_eq!(format!("{}", PublicationType::Paperback), "Paperback");
115 assert_eq!(format!("{}", PublicationType::Hardback), "Hardback");
116 assert_eq!(format!("{}", PublicationType::PDF), "PDF");
117 assert_eq!(format!("{}", PublicationType::HTML), "HTML");
118 assert_eq!(format!("{}", PublicationType::XML), "XML");
119 assert_eq!(format!("{}", PublicationType::Epub), "Epub");
120 assert_eq!(format!("{}", PublicationType::Mobi), "Mobi");
121}
122
123#[test]
124fn test_publicationtype_fromstr() {
125 assert_eq!(
126 PublicationType::from_str("Paperback").unwrap(),
127 PublicationType::Paperback
128 );
129 assert_eq!(
130 PublicationType::from_str("Hardback").unwrap(),
131 PublicationType::Hardback
132 );
133 assert_eq!(
134 PublicationType::from_str("PDF").unwrap(),
135 PublicationType::PDF
136 );
137 assert_eq!(
138 PublicationType::from_str("HTML").unwrap(),
139 PublicationType::HTML
140 );
141 assert_eq!(
142 PublicationType::from_str("XML").unwrap(),
143 PublicationType::XML
144 );
145 assert_eq!(
146 PublicationType::from_str("Epub").unwrap(),
147 PublicationType::Epub
148 );
149 assert_eq!(
150 PublicationType::from_str("Mobi").unwrap(),
151 PublicationType::Mobi
152 );
153
154 assert!(PublicationType::from_str("PNG").is_err());
155 assert!(PublicationType::from_str("Latex").is_err());
156}