Skip to main content

j_cli/command/
category.rs

1use crate::config::YamlConfig;
2use crate::constants::{self, section};
3use crate::{error, info, usage};
4
5/// 支持标记的分类列表(引用全局常量)
6const VALID_CATEGORIES: &[&str] = constants::NOTE_CATEGORIES;
7
8/// 处理 note 命令: j note <alias> <category>
9/// 将别名标记为指定分类(browser/editor/vpn/outer_url/script)
10pub fn handle_note(alias: &str, category: &str, config: &mut YamlConfig) {
11    // 校验别名是否存在
12    if !config.contains(section::PATH, alias)
13        && !config.contains(section::INNER_URL, alias)
14        && !config.contains(section::OUTER_URL, alias)
15    {
16        error!("❌ 别名 {} 不存在", alias);
17        return;
18    }
19
20    // 校验 category 是否合法
21    if !VALID_CATEGORIES.contains(&category) {
22        usage!(
23            "j note <alias> <category> (可选: {})",
24            VALID_CATEGORIES.join(", ")
25        );
26        return;
27    }
28
29    match category {
30        c if c == section::OUTER_URL => {
31            // outer_url 特殊处理:从 inner_url 移到 outer_url
32            if let Some(url) = config.get_property(section::INNER_URL, alias).cloned() {
33                config.set_property(section::OUTER_URL, alias, &url);
34                config.remove_property(section::INNER_URL, alias);
35                info!("✅ 将别名 {} 标记为 OUTER_URL 成功", alias);
36            } else {
37                error!("❌ 别名 {} 不在 INNER_URL 中,无法标记为 OUTER_URL", alias);
38            }
39        }
40        _ => {
41            // 其他分类:将 path 中的值复制到对应分类
42            if let Some(path) = config.get_property(section::PATH, alias).cloned() {
43                config.set_property(category, alias, &path);
44                info!(
45                    "✅ 将别名 {} 标记为 {} 成功",
46                    alias,
47                    category.to_uppercase()
48                );
49            } else {
50                error!("❌ 别名 {} 不在 PATH 中,无法标记", alias);
51            }
52        }
53    }
54}
55
56/// 处理 denote 命令: j denote <alias> <category>
57/// 解除别名的分类标记
58pub fn handle_denote(alias: &str, category: &str, config: &mut YamlConfig) {
59    // 校验 category 是否合法
60    if !VALID_CATEGORIES.contains(&category) {
61        usage!(
62            "j denote <alias> <category> (可选: {})",
63            VALID_CATEGORIES.join(", ")
64        );
65        return;
66    }
67
68    if !config.contains(category, alias) {
69        error!("❌ 别名 {} 不在 {} 分类中", alias, category.to_uppercase());
70        return;
71    }
72
73    config.remove_property(category, alias);
74    info!(
75        "✅ 已将别名 {} 从 {} 中移除",
76        alias,
77        category.to_uppercase()
78    );
79}