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
use std::borrow::Cow;
use std::num::NonZeroU64;
use std::path::Path;

use dotenv::dotenv_iter;
use serde::Deserialize;

use crate::rules::RuleMap;

#[derive(Clone, Debug, Deserialize)]
pub struct Manifest {
    #[serde(default)]
    pub credentials: Option<Box<str>>,
    #[serde(default)]
    pub database_url: Option<Box<str>>,
    pub rule: RuleMap,
    pub twitter: Twitter,
    #[serde(default)]
    pub skip_duplicate: bool,
    #[serde(skip)]
    _non_exhaustive: (),
}

#[derive(Clone, Debug, Deserialize)]
pub struct Twitter {
    pub user: i64,
    #[serde(default)]
    pub list: Option<NonZeroU64>,
    #[serde(skip)]
    _non_exhaustive: (),
}

impl Manifest {
    pub fn resolve_paths(&mut self, base: &str) {
        let resolve = |path: &mut Box<str>| {
            *path = Path::new(base)
                .join(&**path)
                .into_os_string()
                .into_string()
                .unwrap()
                .into();
        };
        self.credentials.as_mut().map(resolve);
        self.database_url.as_mut().map(resolve);
    }

    pub fn credentials_path(&self) -> &str {
        self.credentials
            .as_ref()
            .map(AsRef::as_ref)
            .unwrap_or("credentials.toml")
    }

    pub fn database_url(&self) -> Cow<'_, str> {
        if let Some(ref url) = self.database_url {
            Cow::Borrowed(url)
        } else if let Some((_, url)) = dotenv_iter()
            .into_iter()
            .flatten()
            .flatten()
            .find(|(k, _)| k == "DATABASE_URL")
        {
            Cow::Owned(url)
        } else {
            Cow::Borrowed("pipitor.sqlite3")
        }
    }
}