rightkit_context/
dispatch.rs1use crate::cli::{
4 self, AdaptCommand, Command, ContextCommand, MemoryCommand, ReceiptsCommand, TranscriptCommand,
5};
6use crate::modes::{self, DispatchOutcome};
7use crate::{EXIT_INTERNAL_ERROR, EXIT_NOT_AVAILABLE, EXIT_OK, EXIT_USER_ERROR};
8use std::ffi::OsString;
9
10pub fn run<I, T>(args: I) -> i32
16where
17 I: IntoIterator<Item = T>,
18 T: Into<OsString> + Clone,
19{
20 let args: Vec<OsString> = args.into_iter().map(Into::into).collect();
21 let word = |index: usize| args.get(index).and_then(|arg| arg.to_str());
22 let always_ok = if word(1) == Some("hook") {
23 Some("hook")
24 } else if word(1) == Some("receipts") && word(2) == Some("confirm") {
25 Some("receipts confirm")
26 } else {
27 None
28 };
29 let parsed = match cli::parse(args) {
30 Ok(parsed) => parsed,
31 Err(error) if always_ok.is_some() => {
32 let line = error.to_string();
33 eprintln!(
34 "membrane: {}: {}",
35 always_ok.unwrap_or("hook"),
36 line.lines().next().unwrap_or("invalid arguments")
37 );
38 return EXIT_OK;
39 }
40 Err(error) => {
41 let _ = error.print();
43 return if error.use_stderr() {
44 EXIT_USER_ERROR
45 } else {
46 EXIT_OK
47 };
48 }
49 };
50 match execute(parsed.command) {
51 DispatchOutcome::Ok => EXIT_OK,
52 DispatchOutcome::UserError(error) => {
53 eprintln!("membrane: {error}");
54 EXIT_USER_ERROR
55 }
56 DispatchOutcome::NotAvailable(error) => {
57 eprintln!("membrane: {error}");
58 EXIT_NOT_AVAILABLE
59 }
60 DispatchOutcome::InternalError(error) => {
61 eprintln!("membrane: internal: {error}");
62 EXIT_INTERNAL_ERROR
63 }
64 DispatchOutcome::Exit(code) => code,
65 }
66}
67
68fn execute(command: Command) -> DispatchOutcome {
69 match command {
70 Command::Context {
71 command: ContextCommand::Pull(args),
72 } => modes::context_pull(&args),
73 Command::Memory {
74 command: MemoryCommand::Push(args),
75 } => modes::memory_push(&args),
76 Command::Memory {
77 command: MemoryCommand::Search(args),
78 } => modes::memory_search(&args),
79 Command::Memory {
80 command: MemoryCommand::Delete(args),
81 } => modes::memory_delete(&args),
82 Command::Memory {
83 command: MemoryCommand::Durable(rest),
84 } => modes::memory_durable(&rest),
85 Command::Adapt {
86 command: AdaptCommand::Observe(args),
87 } => modes::adapt_observe(&args),
88 Command::Adapt {
89 command: AdaptCommand::Recall(args),
90 } => modes::adapt_recall(&args),
91 Command::Adapt {
92 command: AdaptCommand::Apply(args),
93 } => modes::adapt_apply(&args),
94 Command::Adapt {
95 command: AdaptCommand::TasteReview(args),
96 } => modes::adapt_taste_review(&args),
97 Command::Adapt {
98 command: AdaptCommand::Status(args),
99 } => modes::adapt_status(&args),
100 Command::Adapt {
101 command: AdaptCommand::AdjudicateTaste(args),
102 } => modes::adapt_adjudicate_taste(&args),
103 Command::Adapt {
104 command: AdaptCommand::MineSession(args),
105 } => modes::adapt_mine_session(&args),
106 Command::Transcript {
107 command: TranscriptCommand::Append(args),
108 } => modes::transcript_append(&args),
109 Command::Transcript {
110 command: TranscriptCommand::Tip(args),
111 } => modes::transcript_tip(&args),
112 Command::History(args) => modes::history(&args.words),
113 Command::Blueprint(args) => modes::blueprint(&args),
114 Command::Status(args) => modes::status(&args),
115 Command::Where(args) => modes::where_binary(&args),
116 Command::Hook(args) => modes::hook_event(&args),
117 Command::Receipts {
118 command: ReceiptsCommand::Confirm(args),
119 } => modes::receipts_confirm(&args),
120 Command::Receipts {
121 command: ReceiptsCommand::List(args),
122 } => modes::receipts_list(&args),
123 Command::Receipts {
124 command: ReceiptsCommand::Mark(args),
125 } => modes::receipts_mark(&args),
126 Command::Serve => modes::serve(),
127 Command::Init(args) => modes::init(&args),
128 }
129}