xref: /DADK/dadk/src/console/user.rs (revision cfb7b78ff5dff2a09cba93336ba222be89cbd3e1)
1 use clap::{Parser, Subcommand, ValueEnum};
2 
3 #[derive(Debug, Subcommand, Clone, PartialEq, Eq)]
4 pub enum UserCommand {
5     Build,
6     Clean(UserCleanCommand),
7     Install,
8 }
9 
10 #[derive(Debug, Parser, Clone, PartialEq, Eq)]
11 pub struct UserCleanCommand {
12     /// 清理级别
13     #[clap(long, default_value = "all")]
14     pub level: UserCleanLevel,
15     /// 要清理的task
16     #[clap(long)]
17     pub task: Option<String>,
18 }
19 
20 #[derive(Debug, Clone, Copy, PartialEq, Eq, ValueEnum)]
21 pub enum UserCleanLevel {
22     /// 清理所有用户程序构建缓存
23     All,
24     /// 只在用户程序源码目录下清理
25     InSrc,
26     /// 只清理用户程序输出目录
27     Output,
28 }
29 
30 impl Into<dadk_config::user::UserCleanLevel> for UserCleanLevel {
31     fn into(self) -> dadk_config::user::UserCleanLevel {
32         match self {
33             UserCleanLevel::All => dadk_config::user::UserCleanLevel::All,
34             UserCleanLevel::InSrc => dadk_config::user::UserCleanLevel::InSrc,
35             UserCleanLevel::Output => dadk_config::user::UserCleanLevel::Output,
36         }
37     }
38 }
39 
40 impl Into<dadk_user::context::Action> for UserCommand {
41     fn into(self) -> dadk_user::context::Action {
42         match self {
43             UserCommand::Build => dadk_user::context::Action::Build,
44             UserCommand::Install => dadk_user::context::Action::Install,
45             UserCommand::Clean(args) => dadk_user::context::Action::Clean(args.level.into()),
46         }
47     }
48 }
49 
50 #[cfg(test)]
51 mod tests {
52 
53     use super::*;
54 
55     #[test]
56     fn test_user_clean_level_from_str() {
57         // Test valid cases
58         assert_eq!(
59             UserCleanLevel::from_str("all", true).unwrap(),
60             UserCleanLevel::All
61         );
62         assert_eq!(
63             UserCleanLevel::from_str("in-src", true).unwrap(),
64             UserCleanLevel::InSrc
65         );
66         assert_eq!(
67             UserCleanLevel::from_str("output", true).unwrap(),
68             UserCleanLevel::Output
69         );
70 
71         // Test invalid case
72         assert!(UserCleanLevel::from_str("invalid", true).is_err());
73     }
74 }
75