1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
use std::env;
use std::error::Error;

use super::utils::print_help;

#[derive(Debug)]
pub enum Command {
    Help,
    Version,
    Message(String),
    REPL,
    ShowAllSessions,
    ShowSession(String),
    DeleteSession(String),
    ClearAllSessions,
    NewSession,
    GlobalSystemPrompt(String),
    SessionSystemPrompt(String, String),
    ShowGlobalSystemPrompt,
    ClearGlobalSystemPrompt,
    DeleteSessionSystemPrompt(String),
    Imagine(String),
    ViewConfig,
    SetConfig,
}

#[derive(Debug)]
pub struct Config {
    pub command: Command,
}

impl Config {
    pub fn new() -> Result<Config, Box<dyn Error>> {
        let args: Vec<String> = env::args().collect();

        if args.len() < 2 {
            print_help()?;
            return Err("No command provided".into());
        }

        let command = match args[1].as_str() {
            "-h" | "--help" => Command::Help,
            "-v" | "--version" => Command::Version,
            "-m" | "--message" => {
                if args.len() < 3 {
                    return Err("No message provided".into());
                }
                Command::Message(args[2].clone())
            }
            "-r" | "--repl" => {
                if args.len() > 2 {
                    return Err("No prompt should be provided".into());
                }
                Command::REPL
            }
            "-i" | "--imagine" => {
                if args.len() < 3 {
                    return Err("No prompt provided".into());
                }
                Command::Imagine(args[2].clone())
            }
            "-s" | "--sessions" => {
                if args.len() < 3 {
                    Command::ShowAllSessions
                } else {
                    Command::ShowSession(args[2].clone())
                }
            }
            "-d" | "--delete" => {
                if args.len() < 3 {
                    return Err("No session id provided".into());
                }
                Command::DeleteSession(args[2].clone())
            }
            "-c" | "--clear" => Command::ClearAllSessions,
            "-n" | "--new" => Command::NewSession,
            "-p" | "--prompt" => {
                if args.len() < 3 {
                    return Err("No prompt provided".into());
                }
                if args.len() < 4 {
                    Command::GlobalSystemPrompt(args[2].clone())
                } else {
                    Command::SessionSystemPrompt(args[2].clone(), args[3].clone())
                }
            }
            "-ps" | "--prompt-show" => Command::ShowGlobalSystemPrompt,
            "-pc" | "--prompt-clear" => Command::ClearGlobalSystemPrompt,
            "-pd" | "--prompt-delete" => {
                if args.len() < 3 {
                    return Err("No session id provided".into());
                }
                Command::DeleteSessionSystemPrompt(args[2].clone())
            }
            "-vc" | "--view-config" => Command::ViewConfig,
            "-sc" | "--set-config" => Command::SetConfig,
            _ => Command::Help,
        };

        Ok(Config { command })
    }
}