gutenberg_rs/
settings.rs

1use crate::error::Error;
2use serde_json::Value;
3use std::path::Path;
4
5/// These are the essential settings for building your cache
6pub struct GutenbergCacheSettings {
7    /// This is the link used to download the rdf tar archive of rdfs from gutenberg
8    pub cache_rdf_download_link: String,
9    /// This is the filename of the cache db
10    pub cache_filename: String,
11    /// this is the directory used to unpack the rdf tar archive downloaded from gutenberg
12    pub cache_rdf_unpack_directory: String,
13    /// this is the archive filename in which we download
14    pub cache_rdf_archive_name: String,
15    /// this is the folder used to hold all the raw text data you download
16    pub text_files_cache_folder: String,
17    /// this will make the cache in memory (it will not save it on disk), it is used in tests
18    pub db_in_memory: bool,
19}
20
21impl Default for GutenbergCacheSettings {
22    fn default() -> GutenbergCacheSettings {
23        GutenbergCacheSettings {
24            db_in_memory: false,
25            text_files_cache_folder: "text_cache".to_string(),
26            cache_rdf_download_link: "https://www.gutenberg.org/cache/epub/feeds/rdf-files.tar.bz2"
27                .to_string(),
28            cache_filename: "gutenbergindex.db".to_string(),
29            cache_rdf_unpack_directory: Path::new("cache")
30                .join("epub")
31                .as_path()
32                .display()
33                .to_string(),
34            cache_rdf_archive_name: "rdf-files.tar.bz2".to_string(),
35        }
36    }
37}
38
39impl GutenbergCacheSettings {
40    pub fn from(json: &Value) -> Result<GutenbergCacheSettings, Error> {
41        let mut settings = GutenbergCacheSettings::default();
42        if let Some(field) = json.get("CacheFilename") {
43            if let Some(v) = field.as_str() {
44                settings.cache_filename = v.to_string();
45            } else {
46                return Err(Error::InvalidSettingsField("CacheFilename".to_string()));
47            }
48        }
49        if let Some(field) = json.get("CacheUnpackDir") {
50            if let Some(v) = field.as_str() {
51                settings.cache_rdf_unpack_directory = v.to_string();
52            } else {
53                return Err(Error::InvalidSettingsField("CacheUnpackDir".to_string()));
54            }
55        }
56        if let Some(field) = json.get("CacheArchiveName") {
57            if let Some(v) = field.as_str() {
58                settings.cache_rdf_archive_name = v.to_string();
59            } else {
60                return Err(Error::InvalidSettingsField("CacheArchiveName".to_string()));
61            }
62        }
63        if let Some(field) = json.get("CacheRDFDownloadLink") {
64            if let Some(v) = field.as_str() {
65                settings.cache_rdf_download_link = v.to_string();
66            } else {
67                return Err(Error::InvalidSettingsField(
68                    "CacheRDFDownloadLink".to_string(),
69                ));
70            }
71        }
72        if let Some(field) = json.get("TextFilesCacheFolder") {
73            if let Some(v) = field.as_str() {
74                settings.text_files_cache_folder = v.to_string();
75            } else {
76                return Err(Error::InvalidSettingsField(
77                    "TextFilesCacheFolder".to_string(),
78                ));
79            }
80        }
81        Ok(settings)
82    }
83}