Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix not to use confy #38

Merged
merged 3 commits into from
Oct 21, 2024
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
47 changes: 25 additions & 22 deletions Cargo.lock

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

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,6 @@ aws-sdk-s3 = "1.57.0"
aws-smithy-types = "1.2.4"
chrono = "0.4.38"
clap = { version = "4.5.20", features = ["derive"] }
confy = "0.6.1"
dirs = "5.0.1"
humansize = "2.1.3"
image = "0.25.4"
Expand All @@ -34,15 +33,18 @@ open = "5.3.0"
ratatui = { version = "0.28.1", features = ["unstable-widget-ref"] }
ratatui-image = "2.0.1"
serde = { version = "1.0.210", features = ["derive"] }
smart-default = "0.7.1"
syntect = { version = "5.2.0", default-features = false, features = [
"default-fancy",
] }
textwrap = "0.16.1"
tokio = { version = "1.40.0", features = ["full"] }
toml = "0.8.19"
tracing = "0.1.40"
tracing-log = "0.2.0"
tracing-subscriber = { version = "0.3.18", features = ["chrono"] }
tui-input = "0.10.1"
umbra = "0.1.0"

[dev-dependencies]
rstest = "0.23.0"
Expand Down
5 changes: 3 additions & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -112,8 +112,9 @@ Detailed operations on each view can be displayed by pressing `?` key.
Config is loaded from `$STU_ROOT_DIR/config.toml`.

- If `STU_ROOT_DIR` environment variable is not set, `~/.stu` is used by default.
- If the file does not exist, it will be created automatically at startup.
- If no value is set, the default value will be set.
- If the `STU_ROOT_DIR` directory does not exist, it will be created automatically.
- If the config file does not exist, the default values will be used for all items.
- If the config file exists but some items are not set, the default values will be used for those unset items.

#### Config file format

Expand Down
112 changes: 33 additions & 79 deletions src/config.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
use std::{env, path::PathBuf};

use anyhow::Context;
use serde::{Deserialize, Serialize};
use serde::Deserialize;
use smart_default::SmartDefault;
use umbra::optional;

const STU_ROOT_DIR_ENV_VAR: &str = "STU_ROOT_DIR";

Expand All @@ -14,90 +16,53 @@ const PREVIEW_THEME_DIR: &str = "preview_theme";
const PREVIEW_SYNTAX_DIR: &str = "preview_syntax";
const CACHE_FILE_NAME: &str = "cache.txt";

#[derive(Serialize, Deserialize, Debug, Clone)]
#[optional(derives = ["Deserialize"])]
#[derive(Debug, Clone, SmartDefault)]
pub struct Config {
#[serde(default = "default_download_dir")]
#[default(_code = "default_download_dir()")]
pub download_dir: String,
#[serde(default = "default_default_region")]
#[default = "us-east-1"]
pub default_region: String,
#[serde(default)]
#[nested]
pub ui: UiConfig,
#[serde(default)]
#[nested]
pub preview: PreviewConfig,
}

#[derive(Serialize, Deserialize, Debug, Default, Clone)]
#[optional(derives = ["Deserialize"])]
#[derive(Debug, Clone, SmartDefault)]
pub struct UiConfig {
#[serde(default)]
#[nested]
pub object_list: UiObjectListConfig,
#[serde(default)]
#[nested]
pub object_detail: UiObjectDetailConfig,
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[optional(derives = ["Deserialize"])]
#[derive(Debug, Clone, SmartDefault)]
pub struct UiObjectListConfig {
#[serde(default = "default_ui_object_list_date_format")]
#[default = "%Y-%m-%d %H:%M:%S"]
pub date_format: String,
#[serde(default = "default_ui_object_list_date_width")]
#[default = 19] // // "2021-01-01 12:34:56".len()
pub date_width: usize,
}

impl Default for UiObjectListConfig {
fn default() -> Self {
Self {
date_format: default_ui_object_list_date_format(),
date_width: default_ui_object_list_date_width(),
}
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[optional(derives = ["Deserialize"])]
#[derive(Debug, Clone, SmartDefault)]
pub struct UiObjectDetailConfig {
#[serde(default = "default_ui_object_detail_date_format")]
#[default = "%Y-%m-%d %H:%M:%S"]
pub date_format: String,
}

impl Default for UiObjectDetailConfig {
fn default() -> Self {
Self {
date_format: default_ui_object_detail_date_format(),
}
}
}

#[derive(Serialize, Deserialize, Debug, Clone)]
#[optional(derives = ["Deserialize"])]
#[derive(Debug, Clone, SmartDefault)]
pub struct PreviewConfig {
#[serde(default)]
pub highlight: bool,
#[serde(default = "default_preview_highlight_theme")]
#[default = "base16-ocean.dark"]
pub highlight_theme: String,
#[serde(default)]
pub image: bool,
}

impl Default for PreviewConfig {
fn default() -> Self {
Self {
highlight: false,
highlight_theme: default_preview_highlight_theme(),
image: false,
}
}
}

impl Default for Config {
fn default() -> Self {
let download_dir = default_download_dir();
let default_region = default_default_region();
Self {
download_dir,
default_region,
ui: UiConfig::default(),
preview: PreviewConfig::default(),
}
}
}

fn default_download_dir() -> String {
match Config::get_app_base_dir() {
Ok(dir) => {
Expand All @@ -108,31 +73,20 @@ fn default_download_dir() -> String {
}
}

fn default_default_region() -> String {
"us-east-1".to_string()
}

fn default_ui_object_list_date_format() -> String {
"%Y-%m-%d %H:%M:%S".to_string()
}

fn default_ui_object_list_date_width() -> usize {
19 // "2021-01-01 12:34:56".len()
}

fn default_ui_object_detail_date_format() -> String {
"%Y-%m-%d %H:%M:%S".to_string()
}

fn default_preview_highlight_theme() -> String {
"base16-ocean.dark".to_string()
}

impl Config {
pub fn load() -> anyhow::Result<Config> {
let dir = Config::get_app_base_dir()?;
if !dir.exists() {
std::fs::create_dir_all(&dir)?;
}
let path = dir.join(CONFIG_FILE_NAME);
confy::load_path(path).context("Failed to load config file")
if path.exists() {
let content = std::fs::read_to_string(path)?;
let config: OptionalConfig = toml::from_str(&content)?;
Ok(config.into())
} else {
Ok(Config::default())
}
}

pub fn download_file_path(&self, name: &str) -> PathBuf {
Expand Down