papers/
sourcemd_command.rs1use std::str::FromStr;
2
3use mysql_async as my;
4
5#[derive(Debug, Clone, PartialEq)]
6pub enum SourceMDcommandMode {
7 Dummy,
8 CreatePaperById,
9 AddAutthorToPublication,
10 AddOrcidMetadataToAuthor,
11 EditPaperForOrcidAuthor,
12 CreateBookFromIsbn,
13}
14
15impl FromStr for SourceMDcommandMode {
16 type Err = String;
17
18 fn from_str(s: &str) -> Result<Self, Self::Err> {
19 match s.to_uppercase().trim() {
20 "CREATE_PAPER_BY_ID" => Ok(SourceMDcommandMode::CreatePaperById),
21 "ADD_AUTHOR_TO_PUBLICATION" => Ok(SourceMDcommandMode::AddAutthorToPublication),
22 "ADD_METADATA_FROM_ORCID_TO_AUTHOR" => {
23 Ok(SourceMDcommandMode::AddOrcidMetadataToAuthor)
24 },
25 "EDIT_PAPER_FOR_ORCID_AUTHOR" => Ok(SourceMDcommandMode::EditPaperForOrcidAuthor),
26 "CREATE_BOOK_FROM_ISBN" => Ok(SourceMDcommandMode::CreateBookFromIsbn),
27 _ => Err(format!("Invalid command: {s}")),
28 }
29 }
30}
31
32impl std::fmt::Display for SourceMDcommandMode {
33 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
34 write!(f, "{}", match self {
35 SourceMDcommandMode::Dummy => "DUMMY",
36 SourceMDcommandMode::CreatePaperById => "CREATE_PAPER_BY_ID",
37 SourceMDcommandMode::AddAutthorToPublication => "ADD_AUTHOR_TO_PUBLICATION",
38 SourceMDcommandMode::AddOrcidMetadataToAuthor => "ADD_METADATA_FROM_ORCID_TO_AUTHOR",
39 SourceMDcommandMode::EditPaperForOrcidAuthor => "EDIT_PAPER_FOR_ORCID_AUTHOR",
40 SourceMDcommandMode::CreateBookFromIsbn => "CREATE_BOOK_FROM_ISBN",
41 })
42 }
43}
44
45#[derive(Debug, Clone)]
46pub struct SourceMDcommand {
47 pub id: i64,
48 pub batch_id: i64,
49 pub serial_number: i64,
50 pub mode: SourceMDcommandMode,
51 pub identifier: String,
52 pub status: String,
53 pub note: String,
54 pub q: String,
55 pub auto_escalate: bool,
56}
57
58impl SourceMDcommand {
59 pub fn new_dummy(identifier: &str) -> Self {
60 Self {
61 id: 0,
62 batch_id: 0,
63 serial_number: 0,
64 mode: SourceMDcommandMode::Dummy,
65 identifier: identifier.to_string(),
66 status: "TODO".to_string(),
67 note: "".to_string(),
68 q: "".to_string(),
69 auto_escalate: false,
70 }
71 }
72
73 pub fn new_from_row(row: my::Row) -> Option<Self> {
74 Some(Self {
75 id: SourceMDcommand::rowvalue_as_i64(&row["id"]),
76 batch_id: SourceMDcommand::rowvalue_as_i64(&row["batch_id"]),
77 serial_number: SourceMDcommand::rowvalue_as_i64(&row["serial_number"]),
78 mode: SourceMDcommandMode::from_str(&SourceMDcommand::rowvalue_as_string(&row["mode"]))
79 .ok()?,
80 identifier: SourceMDcommand::rowvalue_as_string(&row["identifier"]),
81 status: SourceMDcommand::rowvalue_as_string(&row["status"]),
82 note: SourceMDcommand::rowvalue_as_string(&row["note"]),
83 q: SourceMDcommand::rowvalue_as_string(&row["q"]),
84 auto_escalate: SourceMDcommand::rowvalue_as_i64(&row["auto_escalate"]) == 1,
85 })
86 }
87
88 fn rowvalue_as_i64(v: &my::Value) -> i64 {
89 match v {
90 my::Value::Int(x) => *x,
91 _ => 0,
92 }
93 }
94
95 fn rowvalue_as_string(v: &my::Value) -> String {
96 match v {
97 my::Value::Bytes(x) => String::from_utf8_lossy(x).to_string(),
98 _ => String::from(""),
99 }
100 }
101}
102
103#[cfg(test)]
104mod tests {
105 use super::*;
106 #[test]
109 fn test_new_dummy() {
110 let cmd = SourceMDcommand::new_dummy("123");
111 assert_eq!(cmd.id, 0);
112 assert_eq!(cmd.batch_id, 0);
113 assert_eq!(cmd.serial_number, 0);
114 assert_eq!(cmd.mode, SourceMDcommandMode::Dummy);
115 assert_eq!(cmd.identifier, "123");
116 assert_eq!(cmd.status, "TODO");
117 assert_eq!(cmd.note, "");
118 assert_eq!(cmd.q, "");
119 assert!(!cmd.auto_escalate);
120 }
121
122 #[test]
123 fn test_rowvalue_as_i64() {
124 let v = my::Value::Int(123);
125 assert_eq!(SourceMDcommand::rowvalue_as_i64(&v), 123);
126 }
127
128 #[test]
129 fn test_rowvalue_as_string() {
130 let v = my::Value::Bytes(b"abc".to_vec());
131 assert_eq!(SourceMDcommand::rowvalue_as_string(&v), "abc");
132 }
133
134 }