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 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;
}
};

View file

@ -9,17 +9,13 @@ pub struct Config {
}
impl Config {
pub fn new(rules: RawConfig) -> Self {
let mut config = Self::default();
pub fn append(&mut self, rules: RawConfig) {
for rule in rules.rules {
let idx = config.queries.insert(rule.queries);
let idx = self.queries.insert(rule.queries);
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]> {
@ -48,24 +44,24 @@ pub struct Definition {
}
pub fn load() -> Config {
pop_launcher::config::find("web")
.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;
}
};
let mut config = Config::default();
match ron::from_str::<RawConfig>(&string) {
Ok(config) => Some(Config::new(config)),
Err(why) => {
tracing::error!("failed to deserialize config: {}", why);
None
}
for path in pop_launcher::config::find("web") {
let string = match std::fs::read_to_string(&path) {
Ok(string) => string,
Err(why) => {
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;
pub async fn main() {
let mut app = App::new();
let mut app = App::default();
let mut requests = json_input_stream(async_stdin());
@ -33,15 +33,17 @@ pub struct App {
out: Unblock<io::Stdout>,
}
impl App {
pub fn new() -> Self {
impl Default for App {
fn default() -> Self {
Self {
config: config::load(),
queries: Vec::new(),
out: async_stdout(),
}
}
}
impl App {
pub async fn activate(&mut self, id: u32) {
if let Some(query) = self.queries.get(id as usize) {
eprintln!("got query: {}", query);