Extract config into cove-config crate
This commit is contained in:
parent
288a5f97dd
commit
5b5370d2df
9 changed files with 37 additions and 15 deletions
|
|
@ -4,6 +4,8 @@ version = { workspace = true }
|
|||
edition = { workspace = true }
|
||||
|
||||
[dependencies]
|
||||
cove-config = { path = "../cove-config" }
|
||||
|
||||
anyhow = "1.0.70"
|
||||
async-trait = "0.1.68"
|
||||
clap = { version = "4.2.1", features = ["derive", "deprecated"] }
|
||||
|
|
@ -17,11 +19,9 @@ once_cell = "1.17.1"
|
|||
open = "4.0.1"
|
||||
parking_lot = "0.12.1"
|
||||
rusqlite = { version = "0.29.0", features = ["bundled", "time"] }
|
||||
serde = { version = "1.0.159", features = ["derive"] }
|
||||
serde_json = "1.0.95"
|
||||
thiserror = "1.0.40"
|
||||
tokio = { version = "1.27.0", features = ["full"] }
|
||||
toml = "0.7.3"
|
||||
unicode-segmentation = "1.10.1"
|
||||
unicode-width = "0.1.10"
|
||||
|
||||
|
|
|
|||
|
|
@ -1,61 +0,0 @@
|
|||
use std::collections::HashMap;
|
||||
use std::fs;
|
||||
use std::path::{Path, PathBuf};
|
||||
|
||||
use serde::Deserialize;
|
||||
|
||||
use crate::macros::ok_or_return;
|
||||
|
||||
#[derive(Debug, Clone, Copy, Default, Deserialize)]
|
||||
#[serde(rename_all = "snake_case")]
|
||||
pub enum RoomsSortOrder {
|
||||
#[default]
|
||||
Alphabet,
|
||||
Importance,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Default, Deserialize)]
|
||||
pub struct EuphRoom {
|
||||
// TODO Mark favourite rooms via printable ascii characters
|
||||
#[serde(default)]
|
||||
pub autojoin: bool,
|
||||
pub username: Option<String>,
|
||||
#[serde(default)]
|
||||
pub force_username: bool,
|
||||
pub password: Option<String>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize)]
|
||||
pub struct Euph {
|
||||
pub rooms: HashMap<String, EuphRoom>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Default, Deserialize)]
|
||||
pub struct Config {
|
||||
pub data_dir: Option<PathBuf>,
|
||||
#[serde(default)]
|
||||
pub ephemeral: bool,
|
||||
#[serde(default)]
|
||||
pub offline: bool,
|
||||
#[serde(default)]
|
||||
pub rooms_sort_order: RoomsSortOrder,
|
||||
// TODO Invoke external notification command?
|
||||
pub euph: Euph,
|
||||
}
|
||||
|
||||
impl Config {
|
||||
pub fn load(path: &Path) -> Self {
|
||||
let content = ok_or_return!(fs::read_to_string(path), Self::default());
|
||||
match toml::from_str(&content) {
|
||||
Ok(config) => config,
|
||||
Err(err) => {
|
||||
eprintln!("Error loading config file: {err}");
|
||||
Self::default()
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
pub fn euph_room(&self, name: &str) -> EuphRoom {
|
||||
self.euph.rooms.get(name).cloned().unwrap_or_default()
|
||||
}
|
||||
}
|
||||
|
|
@ -14,7 +14,6 @@
|
|||
// TODO Time zones other than UTC
|
||||
// TODO Fix password room auth
|
||||
|
||||
mod config;
|
||||
mod euph;
|
||||
mod export;
|
||||
mod logger;
|
||||
|
|
@ -28,12 +27,12 @@ use std::path::PathBuf;
|
|||
|
||||
use clap::Parser;
|
||||
use cookie::CookieJar;
|
||||
use cove_config::Config;
|
||||
use directories::{BaseDirs, ProjectDirs};
|
||||
use log::info;
|
||||
use tokio::sync::mpsc;
|
||||
use toss::Terminal;
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::logger::Logger;
|
||||
use crate::ui::Ui;
|
||||
use crate::vault::Vault;
|
||||
|
|
|
|||
|
|
@ -10,6 +10,7 @@ use std::io;
|
|||
use std::sync::{Arc, Weak};
|
||||
use std::time::{Duration, Instant};
|
||||
|
||||
use cove_config::Config;
|
||||
use parking_lot::FairMutex;
|
||||
use tokio::sync::mpsc::error::TryRecvError;
|
||||
use tokio::sync::mpsc::{self, UnboundedReceiver, UnboundedSender};
|
||||
|
|
@ -17,7 +18,6 @@ use tokio::task;
|
|||
use toss::widgets::BoxedAsync;
|
||||
use toss::{Terminal, WidgetExt};
|
||||
|
||||
use crate::config::Config;
|
||||
use crate::logger::{LogMsg, Logger};
|
||||
use crate::macros::{logging_unwrap, ok_or_return, some_or_return};
|
||||
use crate::util::InfallibleExt;
|
||||
|
|
|
|||
|
|
@ -11,7 +11,6 @@ use tokio::sync::{mpsc, oneshot};
|
|||
use toss::widgets::{BoxedAsync, EditorState, Join2, Layer, Text};
|
||||
use toss::{Style, Styled, Terminal, Widget, WidgetExt};
|
||||
|
||||
use crate::config;
|
||||
use crate::euph;
|
||||
use crate::macros::logging_unwrap;
|
||||
use crate::ui::chat::{ChatState, Reaction};
|
||||
|
|
@ -46,7 +45,7 @@ type EuphChatState = ChatState<euph::SmallMessage, EuphRoomVault>;
|
|||
|
||||
pub struct EuphRoom {
|
||||
server_config: ServerConfig,
|
||||
config: config::EuphRoom,
|
||||
config: cove_config::EuphRoom,
|
||||
ui_event_tx: mpsc::UnboundedSender<UiEvent>,
|
||||
|
||||
room: Option<euph::Room>,
|
||||
|
|
@ -64,7 +63,7 @@ pub struct EuphRoom {
|
|||
impl EuphRoom {
|
||||
pub fn new(
|
||||
server_config: ServerConfig,
|
||||
config: config::EuphRoom,
|
||||
config: cove_config::EuphRoom,
|
||||
vault: EuphRoomVault,
|
||||
ui_event_tx: mpsc::UnboundedSender<UiEvent>,
|
||||
) -> Self {
|
||||
|
|
|
|||
|
|
@ -2,6 +2,7 @@ use std::collections::{HashMap, HashSet};
|
|||
use std::iter;
|
||||
use std::sync::{Arc, Mutex};
|
||||
|
||||
use cove_config::{Config, RoomsSortOrder};
|
||||
use crossterm::style::Stylize;
|
||||
use euphoxide::api::SessionType;
|
||||
use euphoxide::bot::instance::{Event, ServerConfig};
|
||||
|
|
@ -11,7 +12,6 @@ use tokio::sync::mpsc;
|
|||
use toss::widgets::{BoxedAsync, EditorState, Empty, Join2, Text};
|
||||
use toss::{Style, Styled, Terminal, Widget, WidgetExt};
|
||||
|
||||
use crate::config::{Config, RoomsSortOrder};
|
||||
use crate::euph;
|
||||
use crate::macros::logging_unwrap;
|
||||
use crate::vault::Vault;
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue