From 628d8d78467631b04307e6e2e41f3dcf767cb86f Mon Sep 17 00:00:00 2001 From: Daniel Liu Date: Fri, 30 Dec 2022 19:18:14 -0500 Subject: [PATCH] (master) load config relative to home --- Cargo.lock | 32 ++++++++++++++++++++++++++++++++ matsuba_common/src/conversion.rs | 4 ---- matsuba_default.toml | 2 +- matsuba_server/Cargo.toml | 1 + matsuba_server/src/config.rs | 20 +++++++++++++++----- matsuba_server/src/main.rs | 3 ++- services/matsuba.service | 3 +-- 7 files changed, 52 insertions(+), 13 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 303b214..d3ec292 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -597,6 +597,26 @@ dependencies = [ "crypto-common", ] +[[package]] +name = "dirs" +version = "4.0.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ca3aa72a6f96ea37bbc5aa912f6788242832f75369bdfdadcb0e38423f100059" +dependencies = [ + "dirs-sys", +] + +[[package]] +name = "dirs-sys" +version = "0.3.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "1b1d1d91c932ef41c0f2663aa8b0ca0342d444d842c06914aa0a7e352d0bada6" +dependencies = [ + "libc", + "redox_users", + "winapi", +] + [[package]] name = "dispatch" version = "0.2.0" @@ -1335,6 +1355,7 @@ version = "0.1.0" dependencies = [ "cgmath", "config", + "dirs", "env_logger", "flate2", "lazy_static", @@ -2083,6 +2104,17 @@ dependencies = [ "bitflags", ] +[[package]] +name = "redox_users" +version = "0.4.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b033d837a7cf162d7993aded9304e30a83213c648b6e389db233191f891e5c2b" +dependencies = [ + "getrandom", + "redox_syscall", + "thiserror", +] + [[package]] name = "regex" version = "1.7.0" diff --git a/matsuba_common/src/conversion.rs b/matsuba_common/src/conversion.rs index c84f16e..9dee0e3 100644 --- a/matsuba_common/src/conversion.rs +++ b/matsuba_common/src/conversion.rs @@ -6,7 +6,6 @@ pub static REPEATABLE_CHARACTERS: &[char] = &[ ]; pub static CONVERSION_TABLE: &[(&str, &str, &str)] = &[ - // monographs ("a", "あ", "ア"), ("i", "い", "イ"), @@ -81,7 +80,6 @@ pub static CONVERSION_TABLE: &[(&str, &str, &str)] = &[ ("pu", "ぷ", "プ"), ("pe", "ぺ", "ペ"), ("po", "ぽ", "ポ"), - // digraphs ("kya", "きゃ", "キャ"), ("kyu", "きゅ", "キュ"), @@ -116,7 +114,6 @@ pub static CONVERSION_TABLE: &[(&str, &str, &str)] = &[ ("pya", "ぴゃ", "ピャ"), ("pyu", "ぴゅ", "ピュ"), ("pyo", "ぴょ", "ピョ"), - // small characters ("xa", "ぁ", "ァ"), ("xi", "ぃ", "ィ"), @@ -130,6 +127,5 @@ pub static CONVERSION_TABLE: &[(&str, &str, &str)] = &[ ("xwa", "ゎ", "ヮ"), ("xka", "ヵ", "ヵ"), ("xke", "ヶ", "ヶ"), - // (hentaigana for fun?) + extended katakana etc チェ + halfwidth? ]; diff --git a/matsuba_default.toml b/matsuba_default.toml index 1962976..6f56174 100644 --- a/matsuba_default.toml +++ b/matsuba_default.toml @@ -24,4 +24,4 @@ completion_bg = "#AAAAAA" completion_fg = "#333333" [database] -cache_dir = "/home/pinosaur/.config/matsuba" +cache_dir = "/usr/share/matsuba" diff --git a/matsuba_server/Cargo.toml b/matsuba_server/Cargo.toml index 095b3fb..255c3e7 100644 --- a/matsuba_server/Cargo.toml +++ b/matsuba_server/Cargo.toml @@ -45,3 +45,4 @@ pino_utils = "0.1.0" env_logger = "0.9" log = "0.4" lazy_static = "1.4" +dirs = "4.0" \ No newline at end of file diff --git a/matsuba_server/src/config.rs b/matsuba_server/src/config.rs index 65804a4..a46bc87 100644 --- a/matsuba_server/src/config.rs +++ b/matsuba_server/src/config.rs @@ -243,12 +243,22 @@ impl<'de> Visitor<'de> for KeybindingVisitor { impl Settings { pub fn load() -> Result { - let conf = Config::builder() - .add_source(File::with_name("matsuba_default.toml")) - // .add_source(File::with_name("matsuba.toml")) - .build()?; + use std::path::Path; - conf.try_deserialize() + let mut conf = Config::builder() + .add_source(File::with_name("/usr/share/matsuba/matsuba_default.toml")); + + if let Some(home) = dirs::home_dir() { + let mut path = home.clone(); + path.push(Path::new(".config/matsuba/matsuba.toml")); + if let Some(path) = path.to_str() { + conf = conf.add_source(File::with_name(path).required(false)); + } + } + + let conf_built = conf.build()?; + + conf_built.try_deserialize() } } diff --git a/matsuba_server/src/main.rs b/matsuba_server/src/main.rs index 0ba7c07..0b0a254 100644 --- a/matsuba_server/src/main.rs +++ b/matsuba_server/src/main.rs @@ -9,6 +9,7 @@ mod xmlparse; use log::info; use tonic::transport::Server; +use crate::config::SETTINGS; use crate::service::{MatsubaServer, MatsubaService}; #[tokio::main] @@ -17,7 +18,7 @@ async fn main() -> Result<(), Box> { tokio::spawn(async move { // manually trigger lazy static call (sorta hacky) - let listen_address = &config::SETTINGS.server.listen_address; + let listen_address = &SETTINGS.server.listen_address; info!("starting tonic server at {}", listen_address); diff --git a/services/matsuba.service b/services/matsuba.service index 8ed8042..59f657e 100644 --- a/services/matsuba.service +++ b/services/matsuba.service @@ -4,9 +4,8 @@ Description=matsuba - the lightweight japanese ime [Service] Type=simple -User=root WorkingDirectory= -ExecStart=matsuba run +ExecStart=matsud Restart=always [Install]