Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 7 additions & 7 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ members = [
]

[workspace.package]
version = "0.0.5"
version = "0.0.6"
publish = false
edition = "2024"

Expand Down
2 changes: 1 addition & 1 deletion DictypeFcitx/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ include("${FCITX_INSTALL_CMAKECONFIG_DIR}/Fcitx5Utils/Fcitx5Macros.cmake")
include("${FCITX_INSTALL_CMAKECONFIG_DIR}/Fcitx5Utils/Fcitx5CompilerSettings.cmake")
include("${FCITX_INSTALL_CMAKECONFIG_DIR}/Fcitx5Utils/Fcitx5UtilsConfigVersion.cmake")

set(DICTYPE_FCITX_VERSION 0.0.5)
set(DICTYPE_FCITX_VERSION 0.0.6)

add_library(dictype MODULE
src/DictypeFcitx.cpp
Expand Down
4 changes: 2 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -53,7 +53,7 @@ Setup

# You can have up to 5 profiles at the same time, starting with Profile1.
# Each profile may have different formats depending on the model (Backend).
[Profile1]
[Profiles.Profile1]
Backend = "ParaformerV2"
Config = {
dashscope_api_key = "...", # required
Expand All @@ -67,7 +67,7 @@ Setup
inverse_text_normalization_enabled = true, # optional
}

[Profile2]
[Profiles.Profile2]
Backend = "QwenV3"
Config = {
dashscope_api_key = "...", # required
Expand Down
60 changes: 50 additions & 10 deletions crates/config-tool/src/config_store.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::collections::BTreeMap;
use std::fs;
use std::path::PathBuf;

use serde::{Deserialize, Serialize};
Expand All @@ -15,18 +14,13 @@ pub struct ConfigFile {
#[serde(rename = "PulseAudio", default)]
pulseaudio: PulseAudioConfig,

#[serde(flatten)]
#[serde(rename = "Profiles", default)]
profiles: BTreeMap<String, ProfileConfig>,
}

impl ConfigFile {
pub fn load() -> Result<Self, ConfigStoreError> {
let path = config_path()?;
if !path.exists() {
return Ok(Self::default());
}
let data = fs::read_to_string(&path)?;
let config = toml::from_str(&data)?;
pub fn parse(content: &str) -> Result<Self, ConfigStoreError> {
let config = toml::from_str(content)?;
Ok(config)
}

Expand All @@ -41,10 +35,56 @@ impl ConfigFile {
}
}

fn config_path() -> Result<PathBuf, ConfigStoreError> {
pub fn get_config_path() -> Result<PathBuf, ConfigStoreError> {
let home = std::env::var_os("HOME").ok_or(ConfigStoreError::MissingHome)?;
let mut path = PathBuf::from(home);
path.push(".config");
path.push("dictype.toml");
Ok(path)
}

#[cfg(test)]
mod tests {
use super::*;

#[test]
fn test_get_config_path() {
assert!(get_config_path().is_ok());
}

#[test]
fn test_load_profiles() {
let config = r#"
[Profiles.Profile1]
Backend = "ParaformerV2"
Config = { dashscope_api_key = "fake" }
"#;

let config = ConfigFile::parse(config).unwrap();
assert_eq!(config.profiles.len(), 1);
}

#[test]
fn test_load_profiles_with_pulseaudio() {
let config = r#"
[PulseAudio]

[Profiles.Profile1]
Backend = "ParaformerV2"
Config = { dashscope_api_key = "fake" }
"#;

let config = ConfigFile::parse(config).unwrap();
assert_eq!(config.profiles.len(), 1);
}

#[test]
fn test_reject_known_sections() {
let config = r"
[Unknown]
a = 1
";

assert!(ConfigFile::parse(config).is_err());
}
}
17 changes: 11 additions & 6 deletions crates/dictyped/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,15 +17,14 @@ use std::{
io,
path::{Path, PathBuf},
};

use tokio::net::UnixListener;
use tokio_stream::wrappers::UnixListenerStream;
use tonic::transport::Server;
use tracing::{info, warn};

use base_client::audio_stream::AudioCapture;
use base_client::grpc_server::DictypeServer;
use config_tool::config_store::ConfigFile;
use config_tool::config_store::{ConfigFile, get_config_path};
use pulseaudio_recorder::PulseAudioRecorder;

use crate::service::DictypeService;
Expand All @@ -51,10 +50,16 @@ async fn main() -> Result<(), Box<dyn std::error::Error>> {
warn!("failed to adjust socket permissions: {err}");
}

let config = ConfigFile::load().unwrap_or_else(|err| {
warn!("failed to load config, using defaults: {err}");
ConfigFile::default()
});
let config = {
let path = get_config_path()?;
let content = fs::read_to_string(&path)?;

ConfigFile::parse(&content).unwrap_or_else(|err| {
warn!("failed to load config, using defaults: {err}");
ConfigFile::default()
})
};

let recorder = PulseAudioRecorder::new(config.pulseaudio().clone())?;
let service = DictypeService::<PulseAudioRecorder>::new(
client_store::ClientStore::load(&config),
Expand Down