Skip to main content

memo_cli/commands/
mod.rs

1mod add;
2mod apply;
3mod delete;
4mod fetch;
5mod list;
6mod report;
7mod search;
8mod update;
9
10use crate::cli::{Cli, ItemState, MemoCommand, OutputMode, SearchMatch};
11use crate::errors::AppError;
12use crate::storage::Storage;
13use crate::storage::repository::QueryState;
14
15pub fn run(cli: &Cli, output_mode: OutputMode) -> Result<(), AppError> {
16    let storage = Storage::new(cli.db.clone());
17    storage.init()?;
18
19    match &cli.command {
20        MemoCommand::Add(args) => add::run(&storage, args, output_mode),
21        MemoCommand::Update(args) => update::run(&storage, args, output_mode),
22        MemoCommand::Delete(args) => delete::run(&storage, args, output_mode),
23        MemoCommand::List(args) => list::run(
24            &storage,
25            output_mode,
26            to_query_state(args.state),
27            args.limit,
28            args.offset,
29        ),
30        MemoCommand::Search(args) => search::run(
31            &storage,
32            output_mode,
33            to_query_state(args.state),
34            &args.query,
35            &args.fields,
36            to_search_match_mode(args.match_mode),
37            args.limit,
38        ),
39        MemoCommand::Report(args) => report::run(&storage, output_mode, args),
40        MemoCommand::Fetch(args) => {
41            fetch::run(&storage, output_mode, args.limit, args.cursor.as_deref())
42        }
43        MemoCommand::Apply(args) => apply::run(&storage, output_mode, args),
44        MemoCommand::Completion(_) => Ok(()),
45    }
46}
47
48fn to_query_state(state: ItemState) -> QueryState {
49    match state {
50        ItemState::All => QueryState::All,
51        ItemState::Pending => QueryState::Pending,
52        ItemState::Enriched => QueryState::Enriched,
53    }
54}
55
56fn to_search_match_mode(mode: SearchMatch) -> crate::storage::search::SearchMatchMode {
57    match mode {
58        SearchMatch::Fts => crate::storage::search::SearchMatchMode::Fts,
59        SearchMatch::Prefix => crate::storage::search::SearchMatchMode::Prefix,
60        SearchMatch::Contains => crate::storage::search::SearchMatchMode::Contains,
61    }
62}