From 94556d868f835e93a6665ef9456521d6c903d0b9 Mon Sep 17 00:00:00 2001 From: Federico Terzi Date: Sun, 1 Sep 2019 22:00:31 +0200 Subject: [PATCH] Add yaml config loading --- Cargo.lock | 90 ++++++++++++++++++++++++++++++++++++++++ Cargo.toml | 2 + src/config.rs | 27 ++++++++++++ src/engine.rs | 2 +- src/main.rs | 13 +++--- src/matcher/mod.rs | 4 +- src/matcher/scrolling.rs | 2 +- 7 files changed, 130 insertions(+), 10 deletions(-) create mode 100644 src/config.rs diff --git a/Cargo.lock b/Cargo.lock index 3739ef8..fb72a35 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -13,20 +13,110 @@ dependencies = [ "cc 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "dtoa" +version = "0.4.4" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "espanso" version = "0.1.0" dependencies = [ "cmake 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", + "serde_yaml 0.8.9 (registry+https://github.com/rust-lang/crates.io-index)", "widestring 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)", ] +[[package]] +name = "linked-hash-map" +version = "0.5.2" +source = "registry+https://github.com/rust-lang/crates.io-index" + +[[package]] +name = "proc-macro2" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "quote" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "serde_derive 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_derive" +version = "1.0.99" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "serde_yaml" +version = "0.8.9" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "dtoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)", + "linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", + "serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)", + "yaml-rust 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "syn" +version = "1.0.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "proc-macro2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)", + "unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)", +] + +[[package]] +name = "unicode-xid" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" + [[package]] name = "widestring" version = "0.4.0" source = "registry+https://github.com/rust-lang/crates.io-index" +[[package]] +name = "yaml-rust" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +dependencies = [ + "linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)", +] + [metadata] "checksum cc 1.0.41 (registry+https://github.com/rust-lang/crates.io-index)" = "8dae9c4b8fedcae85592ba623c4fd08cfdab3e3b72d6df780c6ead964a69bfff" "checksum cmake 0.1.42 (registry+https://github.com/rust-lang/crates.io-index)" = "81fb25b677f8bf1eb325017cb6bb8452f87969db0fedb4f757b297bee78a7c62" +"checksum dtoa 0.4.4 (registry+https://github.com/rust-lang/crates.io-index)" = "ea57b42383d091c85abcc2706240b94ab2a8fa1fc81c10ff23c4de06e2a90b5e" +"checksum linked-hash-map 0.5.2 (registry+https://github.com/rust-lang/crates.io-index)" = "ae91b68aebc4ddb91978b11a1b02ddd8602a05ec19002801c5666000e05e0f83" +"checksum proc-macro2 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "175a40b9cf564ce9bf050654633dbf339978706b8ead1a907bb970b63185dd95" +"checksum quote 1.0.2 (registry+https://github.com/rust-lang/crates.io-index)" = "053a8c8bcc71fcce321828dc897a98ab9760bef03a4fc36693c231e5b3216cfe" +"checksum serde 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)" = "fec2851eb56d010dc9a21b89ca53ee75e6528bab60c11e89d38390904982da9f" +"checksum serde_derive 1.0.99 (registry+https://github.com/rust-lang/crates.io-index)" = "cb4dc18c61206b08dc98216c98faa0232f4337e1e1b8574551d5bad29ea1b425" +"checksum serde_yaml 0.8.9 (registry+https://github.com/rust-lang/crates.io-index)" = "38b08a9a90e5260fe01c6480ec7c811606df6d3a660415808c3c3fa8ed95b582" +"checksum syn 1.0.5 (registry+https://github.com/rust-lang/crates.io-index)" = "66850e97125af79138385e9b88339cbcd037e3f28ceab8c5ad98e64f0f1f80bf" +"checksum unicode-xid 0.2.0 (registry+https://github.com/rust-lang/crates.io-index)" = "826e7639553986605ec5979c7dd957c7895e93eabed50ab2ffa7f6128a75097c" "checksum widestring 0.4.0 (registry+https://github.com/rust-lang/crates.io-index)" = "effc0e4ff8085673ea7b9b2e3c73f6bd4d118810c9009ed8f1e16bd96c331db6" +"checksum yaml-rust 0.4.3 (registry+https://github.com/rust-lang/crates.io-index)" = "65923dd1784f44da1d2c3dbbc5e822045628c590ba72123e1c73d3c230c4434d" diff --git a/Cargo.toml b/Cargo.toml index aab72fa..c146e23 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -7,6 +7,8 @@ build="build.rs" [dependencies] widestring = "0.4.0" +serde = { version = "1.0", features = ["derive"] } +serde_yaml = "0.8" [build-dependencies] cmake = "0.1.31" \ No newline at end of file diff --git a/src/config.rs b/src/config.rs new file mode 100644 index 0000000..147c546 --- /dev/null +++ b/src/config.rs @@ -0,0 +1,27 @@ +use std::path::Path; +use crate::matcher::Match; +use std::fs::File; +use std::io::Read; +use serde::{Serialize, Deserialize}; + +#[derive(Debug, Serialize, Deserialize)] +pub struct Configs { + pub matches: Vec +} + +impl Configs { + pub fn load(path: &Path) -> Configs { + let mut file_res = File::open(path); + if let Ok(mut file) = file_res { + let mut contents = String::new(); + file.read_to_string(&mut contents); + let config: Configs = serde_yaml::from_str(&contents).unwrap(); + + config + }else{ + panic!("Config file not found...") + } + } + + +} \ No newline at end of file diff --git a/src/engine.rs b/src/engine.rs index 8410cc5..2d437da 100644 --- a/src/engine.rs +++ b/src/engine.rs @@ -14,6 +14,6 @@ impl <'a> Engine<'a> { impl <'a> MatchReceiver for Engine<'a>{ fn on_match(&self, m: &Match) { self.sender.delete_string(m.trigger.len() as i32); - self.sender.send_string(m.result.as_str()); + self.sender.send_string(m.replace.as_str()); } } \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index de4f6ef..2788419 100644 --- a/src/main.rs +++ b/src/main.rs @@ -4,13 +4,16 @@ use crate::matcher::Matcher; use crate::matcher::Match; use crate::matcher::scrolling::ScrollingMatcher; use crate::engine::Engine; +use crate::config::Configs; +use std::path::Path; mod keyboard; mod matcher; mod engine; +mod config; fn main() { - println!("espanso is running!"); + let configs = Configs::load(Path::new("/home/freddy/.espanso")); let (txc, rxc) = mpsc::channel(); @@ -22,12 +25,8 @@ fn main() { let engine = Engine::new(&sender); - let matches = vec![Match{trigger:"e'".to_owned(), result: "รจ".to_owned()}, - Match{trigger:"e/".to_owned(), result: "รฉ".to_owned()}, - Match{trigger:":lol".to_owned(), result: "๐Ÿ˜‚".to_owned()}, - Match{trigger:":llol".to_owned(), result: "๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚๐Ÿ˜‚".to_owned()}, - ]; + println!("espanso is running!"); - let mut matcher = ScrollingMatcher::new(&matches, &engine); + let mut matcher = ScrollingMatcher::new(&configs.matches, &engine); matcher.watch(&rxc); } \ No newline at end of file diff --git a/src/matcher/mod.rs b/src/matcher/mod.rs index 7a4c160..9d673bc 100644 --- a/src/matcher/mod.rs +++ b/src/matcher/mod.rs @@ -1,10 +1,12 @@ use std::sync::mpsc::Receiver; +use serde::{Serialize, Deserialize}; pub(crate) mod scrolling; +#[derive(Debug, Serialize, Deserialize)] pub struct Match { pub trigger: String, - pub result: String + pub replace: String } pub trait MatchReceiver { diff --git a/src/matcher/scrolling.rs b/src/matcher/scrolling.rs index 3c5166a..5e3d983 100644 --- a/src/matcher/scrolling.rs +++ b/src/matcher/scrolling.rs @@ -23,7 +23,7 @@ impl <'a> super::Matcher for ScrollingMatcher<'a> { .filter(|&x| x.remaining.chars().nth(0).unwrap() == c) .map(|x | MatchEntry{remaining: &x.remaining[1..], _match: &x._match}) .collect(); - + self.current_set = old_matches; self.current_set.append(&mut new_matches);