From bf3ec671ff16935e177f8b47bcefdef29c846ed8 Mon Sep 17 00:00:00 2001 From: Loong <40141251+wangl-cc@users.noreply.github.com> Date: Sun, 8 Sep 2024 16:57:53 +0100 Subject: [PATCH] refactor: split dirs module into a separate crate --- Cargo.lock | 10 ++++++++++ Cargo.toml | 2 +- maa-cli/Cargo.toml | 1 + maa-cli/src/activity.rs | 1 + maa-cli/src/cleanup.rs | 1 + maa-cli/src/config/asst.rs | 1 + maa-cli/src/config/init.rs | 1 + maa-cli/src/config/mod.rs | 1 + maa-cli/src/main.rs | 4 +--- maa-cli/src/run/mod.rs | 1 + maa-dirs/Cargo.toml | 12 ++++++++++++ maa-cli/src/dirs.rs => maa-dirs/src/lib.rs | 3 ++- 12 files changed, 33 insertions(+), 5 deletions(-) create mode 100644 maa-dirs/Cargo.toml rename maa-cli/src/dirs.rs => maa-dirs/src/lib.rs (99%) diff --git a/Cargo.lock b/Cargo.lock index 65973d8e..57e9ba62 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -981,6 +981,7 @@ dependencies = [ "git2", "indicatif", "log", + "maa-dirs", "maa-sys", "prettytable", "regex", @@ -999,6 +1000,15 @@ dependencies = [ "zip", ] +[[package]] +name = "maa-dirs" +version = "0.1.0" +dependencies = [ + "directories", + "dunce", + "log", +] + [[package]] name = "maa-sys" version = "0.3.2" diff --git a/Cargo.toml b/Cargo.toml index b86edd1c..2788d254 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [workspace] resolver = "2" -members = ["maa-cli", "maa-sys"] +members = ["maa-cli", "maa-dirs", "maa-sys"] [workspace.package] license = "AGPL-3.0-only" diff --git a/maa-cli/Cargo.toml b/maa-cli/Cargo.toml index 2f13e3e6..e8c9931d 100644 --- a/maa-cli/Cargo.toml +++ b/maa-cli/Cargo.toml @@ -42,6 +42,7 @@ maa-sys = { path = "../maa-sys", version = "0.3.1", features = [ "runtime", "serde", ] } +maa-dirs = { path = "../maa-dirs", version = "0.1.0" } directories = "5" anyhow = "1" diff --git a/maa-cli/src/activity.rs b/maa-cli/src/activity.rs index 4f78526b..0911520d 100644 --- a/maa-cli/src/activity.rs +++ b/maa-cli/src/activity.rs @@ -5,6 +5,7 @@ use std::{io::Write, path::Path, sync::OnceLock}; use anyhow::{bail, Context, Result}; use chrono::{DateTime, FixedOffset, NaiveDateTime}; use log::warn; +use maa_dirs::join; use serde::Deserialize; use serde_json::Value as JsonValue; diff --git a/maa-cli/src/cleanup.rs b/maa-cli/src/cleanup.rs index bb423211..042fa8bd 100644 --- a/maa-cli/src/cleanup.rs +++ b/maa-cli/src/cleanup.rs @@ -11,6 +11,7 @@ use std::{ }; use anyhow::{bail, Result}; +use maa_dirs::join; pub trait PathProvider { /// Path to a directory to be cleaned up diff --git a/maa-cli/src/config/asst.rs b/maa-cli/src/config/asst.rs index e038f6db..be87a93b 100644 --- a/maa-cli/src/config/asst.rs +++ b/maa-cli/src/config/asst.rs @@ -4,6 +4,7 @@ use std::{borrow::Cow, path::PathBuf}; use anyhow::{Context, Result}; use log::{debug, info, warn}; +use maa_dirs::join; use maa_sys::{Assistant, InstanceOptionKey, StaticOptionKey, TouchMode}; use serde::Deserialize; diff --git a/maa-cli/src/config/init.rs b/maa-cli/src/config/init.rs index f12e669d..ba9b1039 100644 --- a/maa-cli/src/config/init.rs +++ b/maa-cli/src/config/init.rs @@ -9,6 +9,7 @@ use crate::{ use std::path::PathBuf; use anyhow::{bail, Result}; +use maa_dirs::join; fn asst_config_template() -> MAAValue { object!( diff --git a/maa-cli/src/config/mod.rs b/maa-cli/src/config/mod.rs index dc1f3394..0ff55912 100644 --- a/maa-cli/src/config/mod.rs +++ b/maa-cli/src/config/mod.rs @@ -357,6 +357,7 @@ mod tests { use crate::assert_matches; + use maa_dirs::join; use serde::Deserialize; use serde_json::{json, Value as JsonValue}; diff --git a/maa-cli/src/main.rs b/maa-cli/src/main.rs index 95b99ad2..b0793549 100644 --- a/maa-cli/src/main.rs +++ b/maa-cli/src/main.rs @@ -1,6 +1,3 @@ -#[macro_use] -mod dirs; - mod log; mod activity; @@ -15,6 +12,7 @@ use crate::command::{Command, Component, Dir, CLI}; use anyhow::{Context, Result}; use clap::{CommandFactory, Parser}; +use maa_dirs as dirs; fn main() -> Result<()> { let cli = command::CLI::parse(); diff --git a/maa-cli/src/run/mod.rs b/maa-cli/src/run/mod.rs index 38205101..ce6171b9 100644 --- a/maa-cli/src/run/mod.rs +++ b/maa-cli/src/run/mod.rs @@ -22,6 +22,7 @@ use std::{ use anyhow::{bail, Context, Result}; use clap::Args; use log::{debug, warn}; +use maa_dirs::join; use maa_sys::Assistant; use signal_hook::consts::TERM_SIGNALS; diff --git a/maa-dirs/Cargo.toml b/maa-dirs/Cargo.toml new file mode 100644 index 00000000..75fa9381 --- /dev/null +++ b/maa-dirs/Cargo.toml @@ -0,0 +1,12 @@ +[package] +name = "maa-dirs" +version = "0.1.0" +edition = "2021" +license.workspace = true +homepage.workspace = true +repository.workspace = true + +[dependencies] +directories = "5" +dunce = "1.0.4" +log = "0.4.20" diff --git a/maa-cli/src/dirs.rs b/maa-dirs/src/lib.rs similarity index 99% rename from maa-cli/src/dirs.rs rename to maa-dirs/src/lib.rs index 60215d2b..e10b03bb 100644 --- a/maa-cli/src/dirs.rs +++ b/maa-dirs/src/lib.rs @@ -28,7 +28,8 @@ pub fn maa_lib_name() -> &'static str { /// The expression after `;` is optional, which is used to set the extension of the final path. /// /// Note: Because we reuse the first path, the first path will be consumed. -/// Thus, if you want to reuse the first path, you should pass a Path instead of a PathBuf. +/// Thus, if you want to reuse the first path, you should pass a Path instead of a PathBuf +#[macro_export] macro_rules! join { ($path:expr, $($paths:expr),+ $(; $ext:expr)?) => {{ let mut path: ::std::path::PathBuf = $path.into();