feat(web): Merge configs from all paths

This commit is contained in:
Michael Aaron Murphy 2021-08-16 13:26:52 +02:00
parent 0195939ed8
commit aa17efd34d
3 changed files with 27 additions and 29 deletions

View file

@ -87,7 +87,7 @@ impl App {
let (tx, rx) = flume::unbounded::<ScriptInfo>(); let (tx, rx) = flume::unbounded::<ScriptInfo>();
let script_sender = async move { let script_sender = async move {
for path in path_rx.recv_async().await { while let Ok(path) = path_rx.recv_async().await {
load_from(&path, &path_tx, tx.clone()).await; load_from(&path, &path_tx, tx.clone()).await;
} }
}; };

View file

@ -9,17 +9,13 @@ pub struct Config {
} }
impl Config { impl Config {
pub fn new(rules: RawConfig) -> Self { pub fn append(&mut self, rules: RawConfig) {
let mut config = Self::default();
for rule in rules.rules { for rule in rules.rules {
let idx = config.queries.insert(rule.queries); let idx = self.queries.insert(rule.queries);
for keyword in rule.matches { for keyword in rule.matches {
config.matches.insert(keyword, idx as u32); self.matches.insert(keyword, idx as u32);
} }
} }
config
} }
pub fn get(&self, word: &str) -> Option<&[Definition]> { pub fn get(&self, word: &str) -> Option<&[Definition]> {
@ -48,24 +44,24 @@ pub struct Definition {
} }
pub fn load() -> Config { pub fn load() -> Config {
pop_launcher::config::find("web") let mut config = Config::default();
.next()
.and_then(|path| {
let string = match std::fs::read_to_string(&path) {
Ok(string) => string,
Err(why) => {
tracing::error!("failed to read config: {}", why);
return None;
}
};
match ron::from_str::<RawConfig>(&string) { for path in pop_launcher::config::find("web") {
Ok(config) => Some(Config::new(config)), let string = match std::fs::read_to_string(&path) {
Err(why) => { Ok(string) => string,
tracing::error!("failed to deserialize config: {}", why); Err(why) => {
None tracing::error!("failed to read config: {}", why);
} continue;
} }
}) };
.unwrap_or_default()
match ron::from_str::<RawConfig>(&string) {
Ok(raw) => config.append(raw),
Err(why) => {
tracing::error!("failed to deserialize config: {}", why);
}
}
}
config
} }

View file

@ -8,7 +8,7 @@ use smol::Unblock;
use std::io; use std::io;
pub async fn main() { pub async fn main() {
let mut app = App::new(); let mut app = App::default();
let mut requests = json_input_stream(async_stdin()); let mut requests = json_input_stream(async_stdin());
@ -33,15 +33,17 @@ pub struct App {
out: Unblock<io::Stdout>, out: Unblock<io::Stdout>,
} }
impl App { impl Default for App {
pub fn new() -> Self { fn default() -> Self {
Self { Self {
config: config::load(), config: config::load(),
queries: Vec::new(), queries: Vec::new(),
out: async_stdout(), out: async_stdout(),
} }
} }
}
impl App {
pub async fn activate(&mut self, id: u32) { pub async fn activate(&mut self, id: u32) {
if let Some(query) = self.queries.get(id as usize) { if let Some(query) = self.queries.get(id as usize) {
eprintln!("got query: {}", query); eprintln!("got query: {}", query);