autopulse_service/settings/triggers/
readarr.rs1use crate::settings::rewrite::Rewrite;
2use crate::settings::timer::{EventTimers, Timer};
3use crate::settings::triggers::{TriggerConfig, TriggerRequest};
4use serde::{Deserialize, Serialize};
5
6#[derive(Serialize, Deserialize, Clone)]
7pub struct Readarr {
8 pub rewrite: Option<Rewrite>,
10 pub timer: Option<Timer>,
12 #[serde(default)]
14 pub excludes: Vec<String>,
15 pub event_timers: Option<EventTimers>,
17}
18
19impl TriggerConfig for Readarr {
20 fn rewrite(&self) -> Option<&Rewrite> {
21 self.rewrite.as_ref()
22 }
23
24 fn timer(&self) -> Option<&Timer> {
25 self.timer.as_ref()
26 }
27
28 fn excludes(&self) -> &Vec<String> {
29 &self.excludes
30 }
31
32 fn event_timers(&self) -> Option<&EventTimers> {
33 self.event_timers.as_ref()
34 }
35}
36
37#[derive(Deserialize, Clone)]
38#[serde(rename_all = "camelCase")]
39#[doc(hidden)]
40pub struct BookFile {
41 path: String,
42}
43
44#[derive(Deserialize, Clone)]
45#[serde(rename_all = "camelCase")]
46#[doc(hidden)]
47pub struct RenamedBookFile {
48 path: String,
49 previous_path: String,
50}
51
52#[derive(Deserialize, Clone)]
53#[serde(rename_all = "camelCase")]
54#[doc(hidden)]
55pub struct Author {
56 path: String,
57}
58
59#[derive(Deserialize, Clone)]
61#[serde(tag = "eventType")]
62#[doc(hidden)]
63pub enum ReadarrRequest {
64 #[serde(rename = "Download")]
65 #[serde(rename_all = "camelCase")]
66 Download { book_files: Vec<BookFile> },
67 #[serde(rename = "Rename")]
68 #[serde(rename_all = "camelCase")]
69 Rename {
70 renamed_book_files: Vec<RenamedBookFile>,
71 },
72 #[serde(rename = "AuthorDelete")]
73 #[serde(rename_all = "camelCase")]
74 AuthorDelete { author: Author },
75 #[serde(rename = "BookDelete")]
76 #[serde(rename_all = "camelCase")]
77 BookDelete { author: Author },
78 #[serde(rename = "BookFileDelete")]
79 #[serde(rename_all = "camelCase")]
80 BookFileDelete { book_file: BookFile },
81 #[serde(rename = "Test")]
82 Test,
83}
84
85impl TriggerRequest for ReadarrRequest {
86 fn from_json(json: serde_json::Value) -> anyhow::Result<Self> {
87 serde_json::from_value(json).map_err(|e| anyhow::anyhow!(e))
88 }
89 fn paths(&self) -> Vec<(String, bool)> {
90 match self {
91 Self::Download { book_files } => book_files
92 .iter()
93 .map(|book_file| (book_file.path.clone(), true))
94 .collect(),
95 Self::Rename { renamed_book_files } => {
96 let mut paths = vec![];
97
98 for file in renamed_book_files {
99 paths.push((file.previous_path.clone(), false));
100 paths.push((file.path.clone(), true));
101 }
102
103 paths
104 }
105 Self::AuthorDelete { author } | Self::BookDelete { author } => {
106 vec![(author.path.clone(), false)]
107 }
108 Self::BookFileDelete { book_file } => {
109 vec![(book_file.path.clone(), false)]
110 }
111 Self::Test => vec![],
112 }
113 }
114}