Initial commit

This commit is contained in:
Harsh Shandilya 2021-11-11 20:33:17 +05:30
commit 05201d7de7
No known key found for this signature in database
GPG key ID: 366D7BBAD1031E80
10 changed files with 640 additions and 0 deletions

38
src/config.rs Normal file
View file

@ -0,0 +1,38 @@
#![allow(dead_code)]
use serde_derive::Deserialize;
#[derive(Clone, Debug, Default, Deserialize)]
pub struct Replacements {
#[serde(rename = "substitutor")]
pub substitutors: Vec<Substitutor>,
}
#[derive(Clone, Debug, Deserialize)]
pub struct Substitutor {
pub matcher: Matcher,
pub action: Action,
}
#[derive(Clone, Debug, Deserialize)]
pub enum Matcher {
#[serde(rename = "starts_with")]
StartsWith { prefix: String },
#[serde(rename = "ends_with")]
EndsWith { suffix: String },
#[serde(rename = "contains")]
Contains { substring: String },
#[serde(rename = "regex")]
Regex { pattern: String },
}
#[derive(Clone, Debug, Deserialize)]
pub enum Action {
#[serde(rename = "replace")]
Replace { from: String, to: String },
#[serde(rename = "prefix")]
Prefix { prefix: String },
#[serde(rename = "suffix")]
Suffix { suffix: String },
#[serde(rename = "remove")]
Remove { substring: String },
}

20
src/main.rs Normal file
View file

@ -0,0 +1,20 @@
mod config;
use anyhow::{anyhow, Result};
use dirs::config_dir;
use crate::config::Replacements;
fn main() -> Result<()> {
let mut config_path = config_dir().ok_or(anyhow!("Failed to get config dir"))?;
config_path.push("substitutor");
config_path.push("config");
config_path.set_extension("toml");
let config: Replacements = if config_path.exists() {
let config_str = std::fs::read_to_string(config_path.as_path())?;
toml::from_str(&config_str)?
} else {
Replacements::default()
};
Ok(())
}