main: extract config path logic to its own method

This commit is contained in:
Harsh Shandilya 2022-01-23 15:37:26 +05:30
parent 2d4776d7d0
commit 675b90f226
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80

View file

@ -2,6 +2,7 @@ mod config;
use std::error::Error;
use std::ops::{Deref, Not};
use std::path::PathBuf;
use anyhow::{anyhow, Result};
use clipboard::{ClipboardContext, ClipboardProvider};
@ -12,16 +13,21 @@ use crate::config::{Act, Match, Replacements};
fn main() -> Result<()> {
pretty_env_logger::init();
let mut config_path = config_dir().ok_or_else(|| anyhow!("Failed to get config dir"))?;
config_path.push("substitutor");
config_path.push("config");
config_path.set_extension("toml");
let config_path = get_config_path()?;
let config_str = std::fs::read_to_string(config_path.as_path()).unwrap_or_default();
let config: Replacements<'_> = toml::from_str(&config_str)?;
loop_clipboard(config);
return Ok(());
}
fn get_config_path() -> Result<PathBuf> {
let mut config_path = config_dir().ok_or_else(|| anyhow!("Failed to get config dir"))?;
config_path.push("substitutor");
config_path.push("config");
config_path.set_extension("toml");
return Ok(config_path);
}
fn loop_clipboard<'a>(config: Replacements<'a>) {
let mut clipboard: ClipboardContext =
ClipboardProvider::new().expect("Failed to get clipboard");