ibdl_common/
lib.rs

1#![deny(clippy::all)]
2use std::fmt::{Display, Formatter};
3use std::{
4    env,
5    fs::create_dir_all,
6    io,
7    path::{Path, PathBuf},
8    str::FromStr,
9};
10// Public Exports
11pub use bincode;
12pub use directories;
13pub use log;
14pub use reqwest;
15pub use serde;
16pub use serde_json;
17pub use tokio;
18
19use directories::ProjectDirs;
20
21use serde::{Deserialize, Serialize};
22
23pub mod macros;
24pub mod post;
25
26/// All currently supported imageboards and their underlying attributes
27#[derive(Debug, Copy, Clone, Ord, PartialOrd, PartialEq, Eq, Serialize, Deserialize)]
28pub enum ImageBoards {
29    /// Represents the website ```https://danbooru.donmai.us``` or it's safe variant ```https://safebooru.donmai.us```.
30    Danbooru,
31    /// Represents the website ```https://e621.net``` or it's safe variant ```https://e926.net```.
32    E621,
33    /// Represents the website ```http://realbooru.com```
34    GelbooruV0_2,
35    /// Represents the website ```https://konachan.com``` or it's safe variant ```https://konachan.net```.
36    Moebooru,
37    /// Represents the website ```https://gelbooru.com```.
38    Gelbooru,
39}
40
41impl Display for ImageBoards {
42    fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
43        match self {
44            Self::Danbooru => write!(f, "Danbooru"),
45            Self::E621 => write!(f, "e621"),
46            Self::GelbooruV0_2 => write!(f, "Gelbooru Beta V0.2.0"),
47            Self::Moebooru => write!(f, "Moebooru"),
48            Self::Gelbooru => write!(f, "Gelbooru"),
49        }
50    }
51}
52
53impl FromStr for ImageBoards {
54    type Err = String;
55
56    fn from_str(s: &str) -> Result<Self, Self::Err> {
57        match s {
58            "gelbooru" => Ok(Self::Gelbooru),
59            "gelbooru_020" | "gelbooru beta 0.2" | "realbooru" => Ok(Self::GelbooruV0_2),
60            "danbooru" => Ok(Self::Danbooru),
61            "e621" => Ok(Self::E621),
62            "moebooru" => Ok(Self::Moebooru),
63            _ => Err(String::from("Invalid imageboard type.")),
64        }
65    }
66}
67
68impl ImageBoards {
69    /// Returns a `PathBuf` pointing to the imageboard's authentication cache.
70    ///
71    /// This is XDG-compliant and saves cache files to
72    /// `$XDG_CONFIG_HOME/imageboard-downloader/<imageboard>` on Linux or
73    /// `%APPDATA%/FerrahWolfeh/imageboard-downloader/<imageboard>` on Windows
74    ///
75    /// Or you can set the env var `IBDL_CACHE_DIR` to point it to a custom location.
76    #[inline]
77    pub fn auth_cache_dir() -> Result<PathBuf, io::Error> {
78        let cfg_path = env::var("IBDL_CACHE_DIR").unwrap_or({
79            let cdir = ProjectDirs::from("com", "FerrahWolfeh", "imageboard-downloader").unwrap();
80            cdir.config_dir().to_string_lossy().to_string()
81        });
82
83        let cfold = Path::new(&cfg_path);
84
85        if !cfold.exists() {
86            create_dir_all(cfold)?;
87        }
88
89        Ok(cfold.to_path_buf())
90    }
91}