nvim_api/types/
command_infos.rs

1use nvim_types::{
2    conversion::{self, FromObject},
3    serde::Deserializer,
4    Function,
5    Object,
6};
7use serde::{
8    de::{self, Error},
9    Deserialize,
10};
11
12use super::{CommandAddr, CommandArgs, CommandNArgs, CommandRange};
13
14#[non_exhaustive]
15#[derive(Clone, Debug, Eq, PartialEq, Hash, Deserialize)]
16pub struct CommandInfos {
17    /// TODO: docs
18    pub addr: Option<CommandAddr>,
19
20    /// Whether the command can take a `!` modifier.
21    pub bang: bool,
22
23    /// Whether the command can be followed by a `|` and another command.
24    pub bar: bool,
25
26    /// Callback triggered by the command.
27    pub callback: Option<Function<CommandArgs, ()>>,
28
29    /// Command complletion strategy.
30    pub complete: Option<String>,
31
32    /// TODO: docs
33    pub complete_arg: Option<String>,
34
35    /// TODO: docs
36    #[serde(deserialize_with = "parse_count")]
37    pub count: Option<u32>,
38
39    /// TODO: docs
40    pub definition: Option<String>,
41
42    /// Whether to use the invocation location as opposed to the definition
43    /// location in verbose messages.
44    pub keepscript: bool,
45
46    /// The command name.
47    pub name: String,
48
49    /// The number of arguments the command can take.
50    #[serde(default)]
51    pub nargs: CommandNArgs,
52
53    /// TODO: docs
54    pub range: Option<CommandRange>,
55
56    /// Whether the firrst argument to the command can be an optional register
57    /// name (like `:del`, `:put` or `:yank`).
58    pub register: bool,
59
60    /// TODO: docs
61    pub script_id: i32,
62}
63
64fn parse_count<'de, D>(deserializer: D) -> Result<Option<u32>, D::Error>
65where
66    D: de::Deserializer<'de>,
67{
68    Option::<String>::deserialize(deserializer)?
69        .map(|count| {
70            count.parse().map_err(|err: std::num::ParseIntError| {
71                D::Error::custom(err.to_string())
72            })
73        })
74        .transpose()
75}
76
77impl FromObject for CommandInfos {
78    fn from_object(obj: Object) -> Result<Self, conversion::Error> {
79        Self::deserialize(Deserializer::new(obj)).map_err(Into::into)
80    }
81}