Skip to content

Commit

Permalink
refactor: start moving common things to intuitils (#120)
Browse files Browse the repository at this point in the history
  • Loading branch information
micielski authored Oct 26, 2024
1 parent c023d1c commit 1ab3e1a
Show file tree
Hide file tree
Showing 22 changed files with 282 additions and 694 deletions.
194 changes: 105 additions & 89 deletions Cargo.lock

Large diffs are not rendered by default.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,8 @@ license = "GPL-3.0-or-later"
rm-config = { version = "0.5", path = "rm-config" }
rm-shared = { version = "0.5", path = "rm-shared" }

intuitils = "0.0.3"

magnetease = "0.3"
anyhow = "1"
serde = { version = "1", features = ["derive"] }
Expand Down
1 change: 1 addition & 0 deletions rm-config/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,3 +22,4 @@ crossterm.workspace = true
thiserror.workspace = true
transmission-rpc.workspace = true
magnetease.workspace = true
intuitils.workspace = true
72 changes: 29 additions & 43 deletions rm-config/src/categories.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,9 @@
use std::{collections::HashMap, io::ErrorKind, path::PathBuf, sync::OnceLock};
use std::collections::HashMap;

use anyhow::{Context, Result};
use intuitils::config::IntuiConfig;
use ratatui::style::Color;
use serde::Deserialize;

use crate::utils::{self, ConfigFetchingError};

#[derive(Deserialize)]
pub struct CategoriesConfig {
#[serde(default)]
Expand All @@ -18,6 +16,33 @@ pub struct CategoriesConfig {
pub max_icon_len: u8,
}

impl IntuiConfig for CategoriesConfig {
fn app_name() -> &'static str {
"rustmission"
}

fn filename() -> &'static str {
"categories.toml"
}

fn default_config() -> &'static str {
include_str!("../defaults/categories.toml")
}

fn should_exit_if_not_found() -> bool {
false
}

fn message_if_not_found() -> Option<String> {
None
}

fn post_init(&mut self) {
self.populate_hashmap();
self.set_lengths();
}
}

#[derive(Deserialize, Clone)]
pub struct Category {
pub name: String,
Expand All @@ -26,50 +51,11 @@ pub struct Category {
pub default_dir: String,
}

impl CategoriesConfig {
pub(crate) const FILENAME: &'static str = "categories.toml";
pub const DEFAULT_CONFIG: &'static str = include_str!("../defaults/categories.toml");

pub(crate) fn init() -> Result<Self> {
match utils::fetch_config::<Self>(Self::FILENAME) {
Ok(mut config) => {
config.after_init();
Ok(config)
}
Err(e) => match e {
ConfigFetchingError::Io(e) if e.kind() == ErrorKind::NotFound => {
let mut config =
utils::put_config::<Self>(Self::DEFAULT_CONFIG, Self::FILENAME)?;
config.after_init();
Ok(config)
}
ConfigFetchingError::Toml(e) => Err(e).with_context(|| {
format!(
"Failed to parse config located at {:?}",
utils::get_config_path(Self::FILENAME)
)
}),
_ => anyhow::bail!(e),
},
}
}

pub(crate) fn path() -> &'static PathBuf {
static PATH: OnceLock<PathBuf> = OnceLock::new();
PATH.get_or_init(|| utils::get_config_path(Self::FILENAME))
}
}

impl CategoriesConfig {
pub fn is_empty(&self) -> bool {
self.categories.is_empty()
}

fn after_init(&mut self) {
self.populate_hashmap();
self.set_lengths();
}

fn populate_hashmap(&mut self) {
for category in &self.categories {
self.map.insert(category.name.clone(), category.clone());
Expand Down
26 changes: 3 additions & 23 deletions rm-config/src/keymap/actions/general.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use intuitils::user_action::UserAction;
use rm_shared::action::Action;
use serde::{Deserialize, Serialize};

use super::UserAction;

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum GeneralAction {
ShowHelp,
Quit,
Expand Down Expand Up @@ -36,25 +35,6 @@ pub enum GeneralActionMergable {
}

impl UserAction for GeneralAction {
fn is_mergable_with(&self, other: &GeneralAction) -> bool {
let other = *other;
match self {
GeneralAction::SwitchToTorrents => other == Self::SwitchToSearch,
GeneralAction::SwitchToSearch => other == Self::SwitchToTorrents,
GeneralAction::Left => other == Self::Right,
GeneralAction::Right => other == Self::Left,
GeneralAction::Down => other == Self::Up,
GeneralAction::Up => other == Self::Down,
GeneralAction::ScrollPageDown => other == Self::ScrollPageUp,
GeneralAction::ScrollPageUp => other == Self::ScrollPageDown,
GeneralAction::GoToBeginning => other == Self::GoToEnd,
GeneralAction::GoToEnd => other == Self::GoToBeginning,
GeneralAction::MoveToColumnLeft => other == Self::MoveToColumnRight,
GeneralAction::MoveToColumnRight => other == Self::MoveToColumnLeft,
_ => false,
}
}

fn desc(&self) -> &'static str {
match self {
GeneralAction::ShowHelp => "toggle help",
Expand All @@ -80,7 +60,7 @@ impl UserAction for GeneralAction {
}
}

fn merged_desc(&self, other: &GeneralAction) -> Option<&'static str> {
fn merge_desc_with(&self, other: &GeneralAction) -> Option<&'static str> {
match (&self, other) {
(Self::Left, Self::Right) => Some("switch to tab left / right"),
(Self::Right, Self::Left) => Some("switch to tab right / left"),
Expand Down
14 changes: 0 additions & 14 deletions rm-config/src/keymap/actions/mod.rs
Original file line number Diff line number Diff line change
@@ -1,17 +1,3 @@
use rm_shared::action::Action;

pub mod general;
pub mod search_tab;
pub mod torrents_tab;

pub trait UserAction: Into<Action> {
fn desc(&self) -> &'static str;
fn merged_desc(&self, other: &Self) -> Option<&'static str> {
let _ = other;
None
}
fn is_mergable_with(&self, other: &Self) -> bool {
let _ = other;
false
}
}
5 changes: 2 additions & 3 deletions rm-config/src/keymap/actions/search_tab.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use intuitils::user_action::UserAction;
use rm_shared::action::Action;
use serde::{Deserialize, Serialize};

use super::UserAction;

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum SearchAction {
ShowProvidersInfo,
}
Expand Down
5 changes: 2 additions & 3 deletions rm-config/src/keymap/actions/torrents_tab.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
use intuitils::user_action::UserAction;
use rm_shared::action::Action;
use serde::{Deserialize, Serialize};

use super::UserAction;

#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord)]
#[derive(Serialize, Deserialize, Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub enum TorrentsAction {
AddMagnet,
MoveTorrent,
Expand Down
Loading

0 comments on commit 1ab3e1a

Please sign in to comment.