Browse Source

Using log-derive and breaking things into smaller modules

master
Julio Biason 3 years ago
parent
commit
e372fd7d4a
  1. 91
      Cargo.lock
  2. 3
      Cargo.toml
  3. 14
      src/commands/mod.rs
  4. 65
      src/config/account.rs
  5. 49
      src/config/config.rs
  6. 50
      src/config/favourite.rs
  7. 2
      src/config/mod.rs
  8. 2
      src/main.rs

91
Cargo.lock generated

@ -197,7 +197,7 @@ dependencies = [
"ansi_term",
"atty",
"bitflags",
"strsim",
"strsim 0.8.0",
"textwrap",
"unicode-width",
"vec_map",
@ -321,6 +321,41 @@ dependencies = [
"lazy_static",
]
[[package]]
name = "darling"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0d706e75d87e35569db781a9b5e2416cff1236a47ed380831f959382ccd5f858"
dependencies = [
"darling_core",
"darling_macro",
]
[[package]]
name = "darling_core"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f0c960ae2da4de88a91b2d920c2a7233b400bc33cb28453a2987822d8392519b"
dependencies = [
"fnv",
"ident_case",
"proc-macro2 1.0.4",
"quote 1.0.2",
"strsim 0.9.3",
"syn 1.0.5",
]
[[package]]
name = "darling_macro"
version = "0.10.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d9b5a2f4ac4969822c62224815d069952656cadc7084fdca9751e6d959189b72"
dependencies = [
"darling_core",
"quote 1.0.2",
"syn 1.0.5",
]
[[package]]
name = "data-encoding"
version = "2.1.2"
@ -382,10 +417,10 @@ dependencies = [
"clap",
"directories",
"elefren",
"env_logger",
"html2md",
"html5ever 0.25.1",
"log",
"log-derive",
"markup5ever 0.10.1",
"markup5ever_rcdom",
"reqwest",
@ -440,19 +475,6 @@ dependencies = [
"cfg-if 0.1.10",
]
[[package]]
name = "env_logger"
version = "0.8.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "17392a012ea30ef05a610aa97dfb49496e71c9f676b27879922ea5bdf60d9d3f"
dependencies = [
"atty",
"humantime",
"log",
"regex",
"termcolor",
]
[[package]]
name = "error-chain"
version = "0.12.1"
@ -690,12 +712,6 @@ version = "1.3.4"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cd179ae861f0c2e53da70d892f5f3029f9594be0c41dc5269cd371691b1dc2f9"
[[package]]
name = "humantime"
version = "2.1.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a3a5bfb195931eeb336b2a7b4d761daec841b97f947d34394601737a7bba5e4"
[[package]]
name = "hyper"
version = "0.12.35"
@ -756,6 +772,12 @@ dependencies = [
"tokio-io",
]
[[package]]
name = "ident_case"
version = "1.0.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b9e0384b61958566e926dc50660321d12159025e767c18e043daf26b70104c39"
[[package]]
name = "idna"
version = "0.1.5"
@ -877,6 +899,18 @@ dependencies = [
"cfg-if 0.1.10",
]
[[package]]
name = "log-derive"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6a42526bb432bcd1b43571d5f163984effa25409a29f1a3242a54d0577d55bcf"
dependencies = [
"darling",
"proc-macro2 1.0.4",
"quote 1.0.2",
"syn 1.0.5",
]
[[package]]
name = "mac"
version = "0.1.1"
@ -1844,6 +1878,12 @@ version = "0.8.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ea5119cdb4c55b55d432abb513a0429384878c15dde60cc77b1c99de1a95a6a"
[[package]]
name = "strsim"
version = "0.9.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "6446ced80d6c486436db5c078dde11a9f73d42b57fb273121e160b84f63d894c"
[[package]]
name = "syn"
version = "0.15.44"
@ -1919,15 +1959,6 @@ dependencies = [
"utf-8",
]
[[package]]
name = "termcolor"
version = "1.1.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2dfed899f0eb03f32ee8c6a0aabdb8a7949659e3466561fc0adf54e26d88c5f4"
dependencies = [
"winapi-util",
]
[[package]]
name = "textwrap"
version = "0.11.0"

3
Cargo.toml

@ -10,10 +10,11 @@ chrono = "0.4"
clap = "2.33"
directories = "3.0"
elefren = { version = "0.20", features = ["toml"] }
env_logger = "0.8"
# env_logger = "0.8"
html2md = "0.2"
html5ever = "0.25"
log = "0.4"
log-derive = "0.4"
markup5ever = "0.10"
markup5ever_rcdom = "0.1"
reqwest = "0.9"

14
src/commands/mod.rs

@ -26,7 +26,7 @@ use elefren::helpers::cli;
use elefren::prelude::*;
use self::errors::CommandError;
use crate::config::config::AccountConfig;
use crate::config::account::AccountConfig;
use crate::config::config::Config;
use crate::config::Configurable;
use crate::storage::data::Data;
@ -145,12 +145,12 @@ fn add_storage(account: &str, storage: &StorageType) -> CommandResult {
fn fetch_all() -> CommandResult {
let mut config = Config::open()?;
for (name, mut account_config) in config {
log::debug!("Fetching new items from {:?}", name);
let new_top_favourite = fetch_account(&mut account_config)?;
// XXX implement
config.set_new_favourite(new_top_favourite);
}
// for (name, mut account_config) in &config {
// log::debug!("Fetching new items from {:?}", name);
// let new_top_favourite = fetch_account(&mut account_config)?;
// // XXX implement
// // config.set_new_favourite(new_top_favourite);
// }
config.save()?;
Ok(())
}

65
src/config/account.rs

@ -0,0 +1,65 @@
/*
DOWNFAV - Download Favourites
Copyright (C) 2020-2021 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 <https://www.gnu.org/licenses/>.
*/
use elefren::Data;
use log_derive::logfn;
use log_derive::logfn_inputs;
use serde_derive::Deserialize;
use serde_derive::Serialize;
use super::favourite::Favourite;
use crate::storage::markdown::config::MarkdownConfig;
/// Account configuration
#[derive(Serialize, Deserialize, Debug)]
pub struct AccountConfig {
favourite: Favourite,
mastodon: Data,
markdown: Option<MarkdownConfig>,
// joplin: Option<JoplinConfig>,
// org: Option<OrgConfig>,
}
impl AccountConfig {
/// Create an empty account configuration.
#[logfn(Trace)]
pub fn new(configuration: Data) -> Self {
Self {
mastodon: configuration,
favourite: Favourite::default(),
markdown: None,
}
}
/// Return the top favourite for the account.
#[logfn(Trace)]
pub fn top_favourite(&self) -> String {
self.favourite.last()
}
/// The Mastodon configuration for the account.
pub fn mastodon(&self) -> Data {
self.mastodon.clone()
}
/// Set the Markdown configuration.
#[logfn_inputs(Trace)]
pub fn set_markdown(&mut self, config: MarkdownConfig) {
self.markdown = Some(config);
}
}

49
src/config/config.rs

@ -26,38 +26,10 @@ use elefren::Data;
use serde_derive::Deserialize;
use serde_derive::Serialize;
use super::account::AccountConfig;
use crate::config::errors::ConfigError;
use crate::storage::markdown::config::MarkdownConfig;
/// The last seen favourite
#[derive(Serialize, Deserialize, Debug)]
struct Favourite {
last: String,
}
/// Account configuration
#[derive(Serialize, Deserialize, Debug)]
pub struct AccountConfig {
pub favourite: Option<Favourite>,
mastodon: Data,
markdown: Option<MarkdownConfig>,
// joplin: Option<JoplinConfig>,
// org: Option<OrgConfig>,
}
impl AccountConfig {
pub fn top_favourite(&self) -> String {
match self.favourite {
Some(favourite) => favourite.last.into(),
None => "0".into(),
}
}
pub fn mastodon(&self) -> Data {
self.mastodon.clone()
}
}
/// The main configuration
#[derive(Serialize, Deserialize, Debug)]
pub struct Config(HashMap<String, AccountConfig>);
@ -88,11 +60,7 @@ impl Config {
/// Add a new account to the configuration file
pub fn add_account(&mut self, name: &str, configuration: Data) {
let account_data = AccountConfig {
favourite: None,
mastodon: configuration,
markdown: None,
};
let account_data = AccountConfig::new(configuration);
self.0.insert(name.into(), account_data);
}
@ -108,7 +76,7 @@ impl Config {
config: MarkdownConfig,
) {
match self.0.get_mut(account.into()) {
Some(account) => account.markdown = Some(config),
Some(account_config) => account_config.set_markdown(config),
None => {}
}
}
@ -123,14 +91,3 @@ impl Config {
Ok(())
}
}
/// Produce an iterator for all the accounts in the configuration.
impl IntoIterator for Config {
type Item = (String, AccountConfig);
type IntoIter =
<HashMap<std::string::String, AccountConfig> as IntoIterator>::IntoIter;
fn into_iter(self) -> Self::IntoIter {
self.0.into_iter()
}
}

50
src/config/favourite.rs

@ -0,0 +1,50 @@
/*
DOWNFAV - Download Favourites
Copyright (C) 2020-2021 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 <https://www.gnu.org/licenses/>.
*/
use serde_derive::Deserialize;
use serde_derive::Serialize;
/// The last seen favourite
#[derive(Serialize, Deserialize, Debug, Default)]
pub struct Favourite {
last: Option<String>,
}
impl Favourite {
/// Return the last favourite
///
/// For example, if there is a favourite ID, returns it.
///
/// ```
/// let favourite = Favourite { last: Some("123".into()) };
/// assert_eq!(favourite.last(), "123".into());
/// ```
///
/// ... but if there isn't, it returns 0.
///
/// ```
/// let favourite = Favourite { last: None };
/// assert_eq!(favourite.last(), "0".into());
/// ```
pub fn last(&self) -> String {
match &self.last {
Some(last) => last.to_string(),
None => "0".into(),
}
}
}

2
src/config/mod.rs

@ -16,8 +16,10 @@
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
pub mod account;
pub mod config;
pub mod errors;
pub mod favourite;
use self::errors::ConfigError;
pub trait Configurable {

2
src/main.rs

@ -22,8 +22,6 @@ mod config;
mod storage;
fn main() {
env_logger::init();
match args::parse() {
Ok(command) => command.execute().unwrap(),
Err(e) => println!("Error: {:?}", e),

Loading…
Cancel
Save