nvim_oxi_api/opts/
option.rs

1use serde::Serialize;
2use types::conversion::FromObject;
3
4use crate::{Buffer, Window};
5
6/// Options passed to
7/// [`set_option_value()`](crate::set_option_value).
8#[cfg(feature = "neovim-0-10")] // On 0.10 and nightly.
9#[derive(Clone, Debug, Default, macros::OptsBuilder)]
10#[repr(C)]
11pub struct OptionOpts {
12    #[builder(mask)]
13    mask: u64,
14
15    #[builder(argtype = "OptionScope", inline = "{0}.into()")]
16    scope: types::String,
17
18    #[builder(argtype = "Window", inline = "{0}.0")]
19    win: types::WinHandle,
20
21    #[builder(method = "buffer", argtype = "Buffer", inline = "{0}.0")]
22    buf: types::BufHandle,
23
24    #[builder(
25        generics = "F: Into<types::String>",
26        argtype = "F",
27        inline = "{0}.into()"
28    )]
29    filetype: types::String,
30}
31
32/// Options passed to
33/// [`set_option_value()`](crate::set_option_value).
34#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
35#[derive(Clone, Debug, Default)]
36#[repr(C)]
37pub struct OptionOpts {
38    buf: types::Object,
39    win: types::Object,
40    scope: types::Object,
41    filetype: types::Object,
42}
43
44#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
45impl OptionOpts {
46    #[inline(always)]
47    pub fn builder() -> OptionOptsBuilder {
48        OptionOptsBuilder::default()
49    }
50}
51
52#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
53#[derive(Clone, Default)]
54pub struct OptionOptsBuilder(OptionOpts);
55
56#[cfg(not(feature = "neovim-0-10"))] // 0nly on 0.9.
57impl OptionOptsBuilder {
58    #[inline]
59    pub fn buffer(&mut self, buffer: Buffer) -> &mut Self {
60        self.0.buf = buffer.into();
61        self
62    }
63
64    #[inline]
65    pub fn filetype(&mut self, filetype: &str) -> &mut Self {
66        let filetype = types::String::from(filetype);
67        self.0.filetype = filetype.into();
68        self
69    }
70
71    #[inline]
72    pub fn scope(&mut self, scope: OptionScope) -> &mut Self {
73        let scope = types::String::from(scope);
74        self.0.scope = scope.into();
75        self
76    }
77
78    #[inline]
79    pub fn window(&mut self, window: Window) -> &mut Self {
80        self.0.win = window.into();
81        self
82    }
83
84    #[inline]
85    pub fn build(&mut self) -> OptionOpts {
86        std::mem::take(&mut self.0)
87    }
88}
89
90#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize)]
91#[serde(rename_all = "lowercase")]
92pub enum OptionScope {
93    Global,
94    Local,
95}
96
97impl From<OptionScope> for types::String {
98    #[inline]
99    fn from(ctx: OptionScope) -> Self {
100        types::String::from_object(
101            ctx.serialize(types::serde::Serializer::new())
102                .expect("`OptionScope` is serializable"),
103        )
104        .expect("`OptionScope` is serialized into a string")
105    }
106}