1#![allow(clippy::arc_with_non_send_sync)]
2#![deny(clippy::unwrap_used, unused_variables)]
3
4use std::sync::{Arc, Mutex};
5
6use nvim_oxi::{
7 api::{create_user_command, err_writeln, opts::CreateCommandOpts, types::*},
8 Dictionary, Error as OxiError, Function, Result as OxiResult,
9};
10
11use config::Config;
12use error::PluginError;
13
14use self::{
15 command::{completion, Command},
16 core::App,
17};
18
19mod command;
20mod config;
21mod core;
22mod crypt;
23mod error;
24#[cfg(feature = "mail")]
25mod mail;
26
27#[nvim_oxi::plugin]
28fn just() -> OxiResult<Dictionary> {
29 let config = Config::default();
30
31 let app = Arc::new(Mutex::new(App::new(config)));
32
33 let just_cmd = {
34 let app_handle_cmd = Arc::clone(&app);
35
36 move |args: CommandArgs| -> OxiResult<()> {
37 let binding = match args.args {
38 Some(a) => a,
39 None => "".to_string(),
40 };
41
42 let mut split_args = binding.split_whitespace();
43 let action = split_args.next().unwrap_or("").to_string();
44 let argument = split_args.next().map(|s| s.to_string());
45
46 let command = Command::from_str(&action, argument.as_deref());
47
48 match command {
49 Some(command) => {
50 if let Ok(mut app_lock) = app_handle_cmd.lock() {
51 app_lock.handle_command(command)?;
52 } else {
53 err_writeln("Failed to acquire lock on app");
54 }
55 }
56 None => err_writeln(&format!("Unknown command: {}", action)),
57 };
58 Ok(())
59 }
60 };
61
62 let opts = CreateCommandOpts::builder()
63 .desc("Just command")
64 .complete(CommandComplete::CustomList(completion()))
65 .nargs(CommandNArgs::Any)
66 .build();
67
68 create_user_command("Just", just_cmd, &opts)?;
69
70 let app_setup = Arc::clone(&app);
71 let exports: Dictionary =
72 Dictionary::from_iter::<[(&str, Function<Dictionary, Result<(), OxiError>>); 1]>([(
73 "setup",
74 Function::from_fn(move |dict: Dictionary| -> OxiResult<()> {
75 match app_setup.lock() {
76 Ok(mut app) => app.setup(dict),
77 Err(e) => {
78 err_writeln(&format!(
79 "Failed to acquire lock on app during setup: {}",
80 e
81 ));
82 Err(PluginError::Custom("Lock error during setup".into()).into())
83 }
84 }
85 }),
86 )]);
87
88 Ok(exports)
89}