Skip to content

Commit

Permalink
Merge pull request #561 from 4e554c4c/async_config
Browse files Browse the repository at this point in the history
Reload config in an async manner
  • Loading branch information
tarkah committed Sep 16, 2024
2 parents 8d3c4e8 + 90872b4 commit 9eb03f4
Show file tree
Hide file tree
Showing 8 changed files with 213 additions and 182 deletions.
5 changes: 3 additions & 2 deletions Cargo.lock

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

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ futures = "0.3.30"
itertools = "0.13.0"
rodio = "0.19.0"
strum = { version = "0.26.3", features = ["derive"] }
tokio-stream = {version = "0.1.16", features = ["fs"] }

[dependencies.uuid]
version = "1.0"
Expand Down
4 changes: 1 addition & 3 deletions data/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1042,9 +1042,7 @@ impl Client {
channel.topic.content = Some(message::parse_fragments(text.clone(), &[]));
}

channel.topic.who = message
.user()
.map(|user| user.nickname().to_string());
channel.topic.who = message.user().map(|user| user.nickname().to_string());
channel.topic.time = Some(server_time(&message));
}
}
Expand Down
31 changes: 20 additions & 11 deletions data/src/config.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
use std::fs;
use std::path::PathBuf;

use tokio_stream::wrappers::ReadDirStream;
use tokio_stream::StreamExt;

use rand::prelude::*;
use rand_chacha::ChaCha8Rng;

Expand Down Expand Up @@ -128,7 +130,9 @@ impl Config {
Self::config_dir().join(environment::CONFIG_FILE_NAME)
}

pub fn load() -> Result<Self, Error> {
pub async fn load() -> Result<Self, Error> {
use tokio::fs;

#[derive(Deserialize)]
pub struct Configuration {
#[serde(default)]
Expand All @@ -154,7 +158,9 @@ impl Config {
}

let path = Self::path();
let content = fs::read_to_string(path).map_err(|e| Error::Read(e.to_string()))?;
let content = fs::read_to_string(path)
.await
.map_err(|e| Error::Read(e.to_string()))?;

let Configuration {
theme,
Expand All @@ -170,11 +176,11 @@ impl Config {
tooltips,
} = toml::from_str(content.as_ref()).map_err(|e| Error::Parse(e.to_string()))?;

servers.read_password_files()?;
servers.read_password_files().await?;

let loaded_notifications = notifications.load_sounds()?;

let themes = Self::load_themes(&theme).unwrap_or_default();
let themes = Self::load_themes(&theme).await.unwrap_or_default();

Ok(Config {
themes,
Expand All @@ -191,7 +197,9 @@ impl Config {
})
}

fn load_themes(default_key: &str) -> Result<Themes, Error> {
async fn load_themes(default_key: &str) -> Result<Themes, Error> {
use tokio::fs;

#[derive(Deserialize)]
#[serde(untagged)]
pub enum Data {
Expand All @@ -202,8 +210,8 @@ impl Config {
V2(Colors),
}

let read_entry = |entry: fs::DirEntry| {
let content = fs::read_to_string(entry.path()).ok()?;
let read_entry = |entry: fs::DirEntry| async move {
let content = fs::read_to_string(entry.path()).await.ok()?;

let data: Data = toml::from_str(content.as_ref()).ok()?;
let name = entry.path().file_stem()?.to_string_lossy().to_string();
Expand All @@ -218,7 +226,8 @@ impl Config {
let mut default = Theme::default();
let mut has_halloy_theme = false;

for entry in fs::read_dir(Self::themes_dir())? {
let mut stream = ReadDirStream::new(fs::read_dir(Self::themes_dir()).await?);
while let Some(entry) = stream.next().await {
let Ok(entry) = entry else {
continue;
};
Expand All @@ -228,7 +237,7 @@ impl Config {
};

if file_name.ends_with(".toml") {
if let Some(theme) = read_entry(entry) {
if let Some(theme) = read_entry(entry).await {
if file_name.strip_suffix(".toml").unwrap_or_default() == default_key
|| file_name == default_key
{
Expand Down Expand Up @@ -267,7 +276,7 @@ impl Config {
// Create configuration path.
let config_path = Self::config_dir().join("config.toml");

let _ = fs::write(config_path, config_bytes);
let _ = std::fs::write(config_path, config_bytes);
}
}

Expand Down
11 changes: 6 additions & 5 deletions data/src/server.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::collections::BTreeMap;
use std::fmt;
use std::fs;

use tokio::fs;

use futures::channel::mpsc::Sender;
use irc::proto;
Expand Down Expand Up @@ -72,15 +73,15 @@ impl Map {
self.0.iter().map(Entry::from)
}

pub fn read_password_files(&mut self) -> Result<(), Error> {
pub async fn read_password_files(&mut self) -> Result<(), Error> {
for (_, config) in self.0.iter_mut() {
if let Some(pass_file) = &config.password_file {
if config.password.is_some() {
return Err(Error::Parse(
"Only one of password and password_file can be set.".to_string(),
));
}
let pass = fs::read_to_string(pass_file)?;
let pass = fs::read_to_string(pass_file).await?;
config.password = Some(pass);
}
if let Some(nick_pass_file) = &config.nick_password_file {
Expand All @@ -89,7 +90,7 @@ impl Map {
"Only one of nick_password and nick_password_file can be set.".to_string(),
));
}
let nick_pass = fs::read_to_string(nick_pass_file)?;
let nick_pass = fs::read_to_string(nick_pass_file).await?;
config.nick_password = Some(nick_pass);
}
if let Some(sasl) = &mut config.sasl {
Expand All @@ -106,7 +107,7 @@ impl Map {
password_file: Some(pass_file),
..
} => {
let pass = fs::read_to_string(pass_file)?;
let pass = fs::read_to_string(pass_file).await?;
*password = Some(pass);
}
_ => {}
Expand Down
Loading

0 comments on commit 9eb03f4

Please sign in to comment.