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
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
use std::{io::ErrorKind, path::PathBuf, sync::OnceLock};

use anyhow::Result;
use ratatui::{layout::Constraint, style::Color};
use serde::{Deserialize, Serialize};
use url::Url;

use crate::utils::{self};

#[derive(Deserialize)]
pub struct MainConfig {
    pub general: General,
    pub connection: Connection,
    #[serde(default)]
    pub torrents_tab: TorrentsTab,
}

#[derive(Deserialize)]
pub struct General {
    #[serde(default)]
    pub auto_hide: bool,
    #[serde(default = "default_accent_color")]
    pub accent_color: Color,
    #[serde(default = "default_beginner_mode")]
    pub beginner_mode: bool,
    #[serde(default)]
    pub headers_hide: bool,
}

fn default_accent_color() -> Color {
    Color::LightMagenta
}

fn default_beginner_mode() -> bool {
    true
}

#[derive(Deserialize)]
pub struct Connection {
    pub username: Option<String>,
    pub password: Option<String>,
    pub url: Url,
    #[serde(default = "default_refresh")]
    pub torrents_refresh: u64,
    #[serde(default = "default_refresh")]
    pub stats_refresh: u64,
    #[serde(default = "default_refresh")]
    pub free_space_refresh: u64,
}

fn default_refresh() -> u64 {
    5
}

#[derive(Serialize, Deserialize, Hash, PartialEq, Eq, Clone, Copy)]
pub enum Header {
    Name,
    SizeWhenDone,
    Progress,
    Eta,
    DownloadRate,
    UploadRate,
    DownloadDir,
    Padding,
    UploadRatio,
    UploadedEver,
    Id,
    ActivityDate,
    AddedDate,
    PeersConnected,
    SmallStatus,
}

impl Header {
    pub fn default_constraint(&self) -> Constraint {
        match self {
            Self::Name => Constraint::Max(70),
            Self::SizeWhenDone => Constraint::Length(12),
            Self::Progress => Constraint::Length(12),
            Self::Eta => Constraint::Length(12),
            Self::DownloadRate => Constraint::Length(12),
            Self::UploadRate => Constraint::Length(12),
            Self::DownloadDir => Constraint::Max(70),
            Self::Padding => Constraint::Length(2),
            Self::UploadRatio => Constraint::Length(6),
            Self::UploadedEver => Constraint::Length(12),
            Self::Id => Constraint::Length(4),
            Self::ActivityDate => Constraint::Length(14),
            Self::AddedDate => Constraint::Length(12),
            Self::PeersConnected => Constraint::Length(6),
            Self::SmallStatus => Constraint::Length(1),
        }
    }

    pub fn header_name(&self) -> &'static str {
        match *self {
            Self::Name => "Name",
            Self::SizeWhenDone => "Size",
            Self::Progress => "Progress",
            Self::Eta => "ETA",
            Self::DownloadRate => "Download",
            Self::UploadRate => "Upload",
            Self::DownloadDir => "Directory",
            Self::Padding => "",
            Self::UploadRatio => "Ratio",
            Self::UploadedEver => "Up Ever",
            Self::Id => "Id",
            Self::ActivityDate => "Last active",
            Self::AddedDate => "Added",
            Self::PeersConnected => "Peers",
            Self::SmallStatus => "",
        }
    }
}

#[derive(Deserialize)]
pub struct TorrentsTab {
    #[serde(default = "default_headers")]
    pub headers: Vec<Header>,
}

fn default_headers() -> Vec<Header> {
    vec![
        Header::Name,
        Header::SizeWhenDone,
        Header::Progress,
        Header::Eta,
        Header::DownloadRate,
        Header::UploadRate,
    ]
}

impl Default for TorrentsTab {
    fn default() -> Self {
        Self {
            headers: default_headers(),
        }
    }
}

impl MainConfig {
    pub(crate) const FILENAME: &'static str = "config.toml";
    const DEFAULT_CONFIG: &'static str = include_str!("../defaults/config.toml");

    pub(crate) fn init() -> Result<Self> {
        match utils::fetch_config::<Self>(Self::FILENAME) {
            Ok(config) => return Ok(config),
            Err(e) => match e {
                utils::ConfigFetchingError::Io(e) if e.kind() == ErrorKind::NotFound => {
                    utils::put_config::<Self>(Self::DEFAULT_CONFIG, Self::FILENAME)?;
                    println!("Update {:?} and start rustmission again", Self::path());
                    std::process::exit(0);
                }
                _ => anyhow::bail!(e),
            },
        };
    }

    pub(crate) fn path() -> &'static PathBuf {
        static PATH: OnceLock<PathBuf> = OnceLock::new();
        PATH.get_or_init(|| utils::get_config_path(Self::FILENAME))
    }
}