nvim_api/opts/
get_option_value.rs

1use derive_builder::Builder;
2use nvim_types::{self as nvim, conversion::FromObject, NonOwning, Object};
3use serde::Serialize;
4
5/// Options passed to
6/// [`nvim_oxi::api::set_option_value`](crate::set_option_value).
7#[derive(Clone, Debug, Default, Builder)]
8#[builder(default, build_fn(private, name = "fallible_build"))]
9pub struct OptionValueOpts {
10    #[cfg(any(feature = "neovim-0-8", feature = "neovim-nightly"))]
11    #[cfg_attr(
12        docsrs,
13        doc(cfg(any(feature = "neovim-0-8", feature = "neovim-nightly")))
14    )]
15    #[builder(setter(strip_option))]
16    buffer: Option<crate::Buffer>,
17
18    #[builder(setter(custom))]
19    scope: Object,
20
21    #[cfg(any(feature = "neovim-0-8", feature = "neovim-nightly"))]
22    #[cfg_attr(
23        docsrs,
24        doc(cfg(any(feature = "neovim-0-8", feature = "neovim-nightly")))
25    )]
26    #[builder(setter(into, strip_option))]
27    window: Option<crate::Window>,
28}
29
30impl OptionValueOpts {
31    #[inline(always)]
32    pub fn builder() -> OptionValueOptsBuilder {
33        OptionValueOptsBuilder::default()
34    }
35}
36
37impl OptionValueOptsBuilder {
38    pub fn scope(&mut self, scope: OptionScope) -> &mut Self {
39        self.scope = Some(nvim::String::from(scope).into());
40        self
41    }
42
43    pub fn build(&mut self) -> OptionValueOpts {
44        self.fallible_build().expect("never fails, all fields have defaults")
45    }
46}
47
48#[derive(Copy, Clone, Debug, Eq, PartialEq, Hash, Serialize)]
49#[serde(rename_all = "lowercase")]
50pub enum OptionScope {
51    Global,
52    Local,
53}
54
55impl From<OptionScope> for nvim::String {
56    fn from(ctx: OptionScope) -> Self {
57        nvim::String::from_object(
58            ctx.serialize(nvim::serde::Serializer::new())
59                .expect("`OptionScope` is serializable"),
60        )
61        .expect("`OptionScope` is serialized into a string")
62    }
63}
64
65#[derive(Default)]
66#[allow(non_camel_case_types)]
67#[repr(C)]
68pub(crate) struct KeyDict_option<'a> {
69    #[cfg(any(feature = "neovim-0-8", feature = "neovim-nightly"))]
70    buf: Object,
71    #[cfg(any(feature = "neovim-0-8", feature = "neovim-nightly"))]
72    win: Object,
73    scope: NonOwning<'a, Object>,
74}
75
76impl<'a> From<&'a OptionValueOpts> for KeyDict_option<'a> {
77    fn from(opts: &'a OptionValueOpts) -> Self {
78        Self {
79            #[cfg(any(feature = "neovim-0-8", feature = "neovim-nightly"))]
80            buf: opts.buffer.as_ref().into(),
81            #[cfg(any(feature = "neovim-0-8", feature = "neovim-nightly"))]
82            win: opts.window.as_ref().into(),
83            scope: opts.scope.non_owning(),
84        }
85    }
86}