neko_cli/
lib.rs

1use std::error::Error;
2pub mod config;
3pub mod create_app;
4
5#[derive(Debug)]
6/// cli功能
7pub enum Command {
8    /// 创建应用
9    Create,
10    /// 运行应用
11    Start,
12    /// 打包应用
13    Build,
14    /// 测试应用
15    Test,
16    /// cli帮助
17    Help,
18}
19/// 应用类型
20pub enum AppType {
21    /// 移动端
22    Mobile,
23    /// 静态网页
24    Site,
25    /// 中台管理
26    BackStage,
27    /// 微前端
28    SingleSpa,
29    /// 库
30    Library,
31}
32pub use config::Config;
33
34pub use self::create_app::CreateApp;
35
36pub fn run(app: Config) -> Result<(), Box<dyn Error>> {
37    println!("{:?}", app);
38    match app.command {
39        Command::Create => {
40            CreateApp::new(app)?;
41        }
42        Command::Start => todo!(),
43        Command::Build => todo!(),
44        Command::Test => todo!(),
45        Command::Help => todo!(),
46    };
47    Ok(())
48}
49
50/// 从内容中搜索出文字,区分大小写
51pub fn search<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
52    contents
53        .lines()
54        .filter(|line| line.contains(query))
55        .collect()
56}
57/// 从内容中搜索出文字,不区分大小写
58pub fn search_case_insensitive<'a>(query: &str, contents: &'a str) -> Vec<&'a str> {
59    let query = query.to_lowercase();
60
61    contents
62        .lines()
63        .filter(|line| line.to_lowercase().contains(&query))
64        .collect()
65}
66#[cfg(test)]
67mod tests {
68    use super::*;
69    #[test]
70    fn case_sensitive() {
71        let query = "duct";
72        let contents = "\
73Rust:
74safe, fast, productive.
75Pick three.
76Duct tape.";
77        assert_eq!(vec!["safe, fast, productive."], search(query, contents))
78    }
79    #[test]
80    fn case_insensitive() {
81        let query = "rUsT";
82        let contents = "\
83Rust:
84safe, fast, productive.
85Pick three.
86Trust me.";
87        assert_eq!(
88            vec!["Rust:", "Trust me."],
89            search_case_insensitive(query, contents)
90        )
91    }
92}