Browse Source

Exposing the markdown configuration to download to it

master
Julio Biason 3 years ago
parent
commit
39267a461c
  1. 7
      src/args/mod.rs
  2. 22
      src/commands/mod.rs
  3. 19
      src/config/config.rs
  4. 1
      src/config/mod.rs

7
src/args/mod.rs

@ -37,7 +37,7 @@ pub fn parse() -> Result<Command, ParsingError> {
.arg(
Arg::with_name("account")
.help("Account alias")
.required(true),
.required(false),
)
.subcommand(SubCommand::with_name("create").about("Create the account"))
.subcommand(SubCommand::with_name("remove").about("Remove the account"))
@ -85,7 +85,7 @@ pub fn parse() -> Result<Command, ParsingError> {
);
let matches = parser.get_matches();
let account_name = matches.value_of("account").unwrap(); // we can unwrap 'cause it is required
if let Some(account_name) = matches.value_of("account") {
match matches.subcommand() {
("create", _) => Ok(Command::add_account(account_name.into())),
("remove", _) => Ok(Command::remove_account(account_name.into())),
@ -104,4 +104,7 @@ pub fn parse() -> Result<Command, ParsingError> {
},
_ => Err(ParsingError::UnknownCommand),
}
} else {
Ok(Command::fetch_all())
}
}

22
src/commands/mod.rs

@ -67,6 +67,9 @@ pub enum Command {
/// Add a storage in an account
AddStorage(String, StorageType),
/// Fetch favourites from all accounts
FetchAll,
}
impl Command {
@ -82,6 +85,10 @@ impl Command {
Command::AddStorage(account.into(), storage)
}
pub fn fetch_all() -> Self {
Command::FetchAll
}
/// Execute the command, based on its value
pub fn execute(&self) -> CommandResult {
match self {
@ -90,6 +97,7 @@ impl Command {
Command::AddStorage(account, storage) => {
add_storage(account, storage)
}
Command::FetchAll => fetch_all(),
}
}
}
@ -132,3 +140,17 @@ fn add_storage(account: &str, storage: &StorageType) -> CommandResult {
config.save()?;
Ok(())
}
fn fetch_all() -> CommandResult {
let mut config = Config::open()?;
for (name, account_config) in config {
log::debug!("Fetching new items from {:?}", name);
if let Some(markdown_config) = account_config.markdown {
log::debug!(
"Markdown set to download to {:?}",
markdown_config.path
);
}
}
Ok(())
}

19
src/config/config.rs

@ -37,10 +37,10 @@ struct Favourite {
/// Account configuration
#[derive(Serialize, Deserialize, Debug)]
struct AccountConfig {
pub struct AccountConfig {
favourite: Option<Favourite>,
mastodon: Data,
markdown: Option<MarkdownConfig>,
pub markdown: Option<MarkdownConfig>,
// joplin: Option<JoplinConfig>,
// org: Option<OrgConfig>,
}
@ -61,7 +61,7 @@ impl Config {
/// Open the configuration file; if it doesn't exist, returns an empty set.
pub fn open() -> Result<Self, ConfigError> {
let filename = Config::filename()?;
log::debug!("Trying to open file \"{:?}\"", filename);
log::debug!("Trying to open file {:?}", filename);
match File::open(filename) {
Ok(mut fp) => {
let mut contents = String::new();
@ -104,9 +104,20 @@ impl Config {
pub fn save(&self) -> Result<(), ConfigError> {
let content = toml::to_string(&self.0)?;
let filename = Config::filename()?;
log::debug!("Saving configuration to file \"{:?}\"", filename);
log::debug!("Saving configuration to file {:?}", filename);
let mut fp = File::create(filename)?;
fp.write_all(content.as_bytes())?;
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()
}
}

1
src/config/mod.rs

@ -20,7 +20,6 @@ pub mod config;
pub mod errors;
use self::errors::ConfigError;
pub trait Configurable {
fn config() -> Result<Self, ConfigError>
where

Loading…
Cancel
Save