1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
use crate::settings::{rewrite::Rewrite, timer::Timer, trigger::TriggerRequest};
use crate::utils::join_path::join_path;
use serde::Deserialize;

#[derive(Deserialize, Clone)]
pub struct Radarr {
    /// Rewrite path
    pub rewrite: Option<Rewrite>,
    /// Timer settings
    #[serde(default)]
    pub timer: Timer,
    /// Targets to ignore
    #[serde(default)]
    pub excludes: Vec<String>,
}

#[derive(Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
#[doc(hidden)]
pub struct MovieFile {
    pub relative_path: String,
}

#[derive(Deserialize, Clone)]
#[serde(rename_all = "camelCase")]
#[doc(hidden)]
pub struct Movie {
    pub folder_path: String,
}

#[derive(Deserialize, Clone)]
#[serde(tag = "eventType")]
#[doc(hidden)]
pub enum RadarrRequest {
    #[serde(rename = "Download")]
    #[serde(rename_all = "camelCase")]
    Download { movie_file: MovieFile, movie: Movie },
    #[serde(rename = "MovieDelete")]
    #[serde(rename_all = "camelCase")]
    MovieDelete { movie: Movie },
    #[serde(rename = "MovieFileDelete")]
    #[serde(rename_all = "camelCase")]
    MovieFileDelete { movie_file: MovieFile, movie: Movie },
    #[serde(rename = "Rename")]
    #[serde(rename_all = "camelCase")]
    Rename { movie: Movie },
    #[serde(rename = "Test")]
    Test,
}

impl TriggerRequest for RadarrRequest {
    fn from_json(json: serde_json::Value) -> anyhow::Result<Self> {
        serde_json::from_value(json).map_err(|e| anyhow::anyhow!(e))
    }
    fn paths(&self) -> Vec<(String, bool)> {
        match self {
            Self::MovieFileDelete { movie, movie_file } => {
                vec![(
                    join_path(&movie.folder_path, &movie_file.relative_path),
                    false,
                )]
            }
            Self::Rename { movie } => {
                vec![(movie.folder_path.clone(), true)]
            }
            Self::MovieDelete { movie } => {
                vec![(movie.folder_path.clone(), false)]
            }
            Self::Download { movie, movie_file } => {
                vec![(
                    join_path(&movie.folder_path, &movie_file.relative_path),
                    true,
                )]
            }
            Self::Test => vec![],
        }
    }
}