rustypaste_cli/
config.rs

1use crate::args::Args;
2use serde::{Deserialize, Serialize};
3use std::fs;
4
5/// Configuration values.
6#[derive(Debug, Clone, Default, Serialize, Deserialize)]
7pub struct Config {
8    /// Server configuration.
9    pub server: ServerConfig,
10    /// Paste configuration.
11    pub paste: PasteConfig,
12    /// Style configuration.
13    pub style: Option<StyleConfig>,
14}
15
16/// Server configuration.
17#[derive(Debug, Clone, Default, Serialize, Deserialize)]
18pub struct ServerConfig {
19    /// Server address.
20    pub address: String,
21    /// Token for authentication.
22    pub auth_token: Option<String>,
23    /// A file containing the token for authentication.
24    ///
25    /// Leading and trailing whitespace will be trimmed.
26    pub auth_token_file: Option<String>,
27    /// Token for deleting files.
28    pub delete_token: Option<String>,
29    /// A file containing the token for deleting files.
30    ///
31    /// Leading and trailing whitespace will be trimmed.
32    pub delete_token_file: Option<String>,
33}
34
35/// Paste configuration.
36#[derive(Debug, Clone, Default, Serialize, Deserialize)]
37pub struct PasteConfig {
38    /// Whether if the file will disappear after being viewed once.
39    pub oneshot: Option<bool>,
40    /// Expiration time for the link.
41    pub expire: Option<String>,
42    /// Filename.
43    #[serde(skip_deserializing)]
44    pub filename: Option<String>,
45}
46
47/// Style configuration.
48#[derive(Debug, Clone, Default, Serialize, Deserialize)]
49pub struct StyleConfig {
50    /// Whether if the output will be prettified.
51    pub prettify: bool,
52}
53
54impl Config {
55    /// Override the configuration file with arguments.
56    pub fn update_from_args(&mut self, args: &Args) {
57        if let Some(server_address) = &args.server {
58            self.server.address = server_address.to_string();
59        }
60        if args.auth.is_some() {
61            self.server.auth_token = args.auth.as_ref().cloned();
62            if args.delete {
63                self.server.delete_token = args.auth.as_ref().cloned();
64            }
65        }
66        if args.oneshot {
67            self.paste.oneshot = Some(true);
68        }
69        if args.expire.is_some() {
70            self.paste.expire = args.expire.as_ref().cloned();
71        }
72        if args.filename.is_some() {
73            self.paste.filename = args.filename.as_ref().cloned();
74        }
75    }
76
77    /// Parses the files referenced by [Config::auth_token_file] and [Config::delete_token_file].
78    ///
79    /// Updates the respective token variables with the contents of the files.
80    pub fn parse_token_files(&mut self) {
81        if let Some(path) = &self.server.auth_token_file {
82            let path = shellexpand::tilde(path).to_string();
83            match fs::read_to_string(path) {
84                Ok(token) => self.server.auth_token = Some(token.trim().to_string()),
85                Err(e) => eprintln!("Error while reading token file: {e}"),
86            };
87        };
88
89        if let Some(path) = &self.server.delete_token_file {
90            let path = shellexpand::tilde(path).to_string();
91            match fs::read_to_string(path) {
92                Ok(token) => self.server.delete_token = Some(token.trim().to_string()),
93                Err(e) => eprintln!("Error while reading token file: {e}"),
94            };
95        };
96    }
97}
98
99#[cfg(test)]
100mod tests {
101    use super::*;
102
103    #[test]
104    /// Test that the token file is being properly processed.
105    fn test_parse_token_files_no_whitespace() {
106        let mut cfg = Config::default();
107        let token = "KBRRHMxlJfFo1".to_string();
108
109        cfg.server.auth_token_file = Some("tests/token_file_parsing/token.txt".to_string());
110        cfg.parse_token_files();
111        assert_eq!(cfg.server.auth_token, Some(token));
112    }
113
114    #[test]
115    /// Test that whitespace is being properly trimmed.
116    fn test_parse_token_files_whitespaced() {
117        let mut cfg = Config::default();
118        let token = "nhJuLuY5vxUrO".to_string();
119
120        cfg.server.auth_token_file =
121            Some("tests/token_file_parsing/token_whitespaced.txt".to_string());
122        cfg.parse_token_files();
123        assert_eq!(cfg.server.auth_token, Some(token));
124    }
125}