From e9d781b15530b5b4ed6535b04aaf5a4346acb0cb Mon Sep 17 00:00:00 2001 From: Julio Biason Date: Tue, 15 Jun 2021 13:06:24 -0300 Subject: [PATCH] Here's an idea: Move backends to their own modules Instead of having a bunch of things in "storage", let's have a "filesystem" module, with its configuration and backend; let's have a "joplin" module, with its configuration and backend; and so on. --- src/config/config.rs | 5 ++++ src/filesystem/config.rs | 27 +++++++++++++++++++ src/filesystem/mod.rs | 2 ++ .../filesystem.rs => filesystem/storage.rs} | 3 ++- src/main.rs | 5 ++-- src/storage/mod.rs | 1 - 6 files changed, 39 insertions(+), 4 deletions(-) create mode 100644 src/filesystem/config.rs create mode 100644 src/filesystem/mod.rs rename src/{storage/filesystem.rs => filesystem/storage.rs} (95%) diff --git a/src/config/config.rs b/src/config/config.rs index 4d86f38..8214b1b 100644 --- a/src/config/config.rs +++ b/src/config/config.rs @@ -27,6 +27,7 @@ use serde_derive::Deserialize; use serde_derive::Serialize; use crate::config::errors::ConfigError; +use crate::filesystem::config::FilesystemConfig; /// The last seen favourite #[derive(Serialize, Deserialize, Debug)] @@ -39,6 +40,9 @@ struct Favourite { struct AccountConfig { favourite: Option, mastodon: Data, + filesystem: Option, + // joplin: Option, + // org: Option, } /// The main configuration @@ -75,6 +79,7 @@ impl Config { let account_data = AccountConfig { favourite: None, mastodon: configuration, + filesystem: None, }; self.0.insert(name.into(), account_data); } diff --git a/src/filesystem/config.rs b/src/filesystem/config.rs new file mode 100644 index 0000000..a89518a --- /dev/null +++ b/src/filesystem/config.rs @@ -0,0 +1,27 @@ +/* + DOWNFAV - Download Favourites + Copyright (C) 2020 Julio Biason + + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU Affero General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. + + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU Affero General Public License for more details. + + You should have received a copy of the GNU Affero General Public License + along with this program. If not, see . +*/ + +use serde_derive::Deserialize; +use serde_derive::Serialize; + +/// Configuration for the Filesystem backend +#[derive(Serialize, Deserialize, Debug)] +pub struct FilesystemConfig { + /// Path where files will be stored. + pub path: String, +} diff --git a/src/filesystem/mod.rs b/src/filesystem/mod.rs new file mode 100644 index 0000000..d5c5ab2 --- /dev/null +++ b/src/filesystem/mod.rs @@ -0,0 +1,2 @@ +pub mod config; +pub mod storage; diff --git a/src/storage/filesystem.rs b/src/filesystem/storage.rs similarity index 95% rename from src/storage/filesystem.rs rename to src/filesystem/storage.rs index 6c68109..75f1d02 100644 --- a/src/storage/filesystem.rs +++ b/src/filesystem/storage.rs @@ -47,7 +47,8 @@ impl Filesystem { /// Make sure the path structure exists for saving the data. fn create_dirs(&self, data: &Data) { - std::fs::create_dir_all(self.dir(data)).expect("Failed to create storage directory"); + std::fs::create_dir_all(self.dir(data)) + .expect("Failed to create storage directory"); } /// Save the content in the directory. diff --git a/src/main.rs b/src/main.rs index c92b4e4..018ee18 100644 --- a/src/main.rs +++ b/src/main.rs @@ -21,14 +21,15 @@ use std::io; use elefren::helpers::cli; use elefren::prelude::*; +use crate::filesystem::storage::Filesystem; use crate::storage::data::Data; -use crate::storage::filesystem::Filesystem; use crate::storage::joplin::Joplin; use crate::storage::org::Org; use crate::storage::storage::Storage; mod args; mod config; +mod filesystem; mod storage; fn main() { @@ -46,7 +47,7 @@ fn add_account(name: &str) { let mut config = config::config::Config::open().unwrap(); let connection_info = connect_to_mastodon(); config.add_account(name, connection_info); - config.save(); + config.save().unwrap(); } /// Retrieve favourites diff --git a/src/storage/mod.rs b/src/storage/mod.rs index 04fbd0f..1cb83a4 100644 --- a/src/storage/mod.rs +++ b/src/storage/mod.rs @@ -18,7 +18,6 @@ pub mod attachment; pub mod data; -pub mod filesystem; pub mod helpers; pub mod joplin; pub mod org;