Extract config into cove-config crate

This commit is contained in:
Joscha 2023-04-20 14:11:32 +02:00
parent 288a5f97dd
commit 5b5370d2df
9 changed files with 37 additions and 15 deletions

11
Cargo.lock generated
View file

@ -249,6 +249,7 @@ dependencies = [
"async-trait",
"clap",
"cookie",
"cove-config",
"crossterm",
"directories",
"edit",
@ -259,19 +260,25 @@ dependencies = [
"open",
"parking_lot",
"rusqlite",
"serde",
"serde_json",
"thiserror",
"time",
"tokio",
"tokio-tungstenite",
"toml",
"toss",
"unicode-segmentation",
"unicode-width",
"vault",
]
[[package]]
name = "cove-config"
version = "0.6.1"
dependencies = [
"serde",
"toml",
]
[[package]]
name = "cpufeatures"
version = "0.2.6"

View file

@ -1,6 +1,6 @@
[workspace]
resolver = "2"
members = ["cove"]
members = ["cove", "cove-*"]
[workspace.package]
version = "0.6.1"

8
cove-config/Cargo.toml Normal file
View file

@ -0,0 +1,8 @@
[package]
name = "cove-config"
version = { workspace = true }
edition = { workspace = true }
[dependencies]
serde = { version = "1.0.159", features = ["derive"] }
toml = "0.7.3"

View file

@ -1,11 +1,20 @@
#![forbid(unsafe_code)]
// Rustc lint groups
#![warn(future_incompatible)]
#![warn(rust_2018_idioms)]
#![warn(unused)]
// Rustc lints
#![warn(noop_method_call)]
#![warn(single_use_lifetimes)]
// Clippy lints
#![warn(clippy::use_self)]
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 {
@ -45,7 +54,7 @@ pub struct Config {
impl Config {
pub fn load(path: &Path) -> Self {
let content = ok_or_return!(fs::read_to_string(path), Self::default());
let Ok(content) = fs::read_to_string(path) else { return Self::default(); };
match toml::from_str(&content) {
Ok(config) => config,
Err(err) => {

View file

@ -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"

View file

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

View file

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

View file

@ -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 {

View file

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