Skip to content

Commit

Permalink
refactor: derive OptsBuilder for GetContextOpts on nightly
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Dec 10, 2023
1 parent 5d3d7f9 commit 60b9286
Showing 1 changed file with 23 additions and 22 deletions.
45 changes: 23 additions & 22 deletions crates/oxi-api/src/opts/get_context.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,33 @@
use oxi_types::{self as nvim, Array};
use oxi_types as types;

use crate::types::ContextType;

/// Options passed to [`get_context()`](crate::get_context).
#[cfg(not(feature = "neovim-nightly"))]
#[derive(Clone, Debug, Default)]
#[cfg(feature = "neovim-nightly")]
#[derive(Clone, Debug, Default, oxi_macros::OptsBuilder)]
#[repr(C)]
pub struct GetContextOpts {
types: oxi_types::Object,
#[builder(mask)]
mask: u64,

/// List of [`ContextType`]s to gather, or empty for all.
#[builder(
generics = "T: IntoIterator<Item = ContextType>",
argtype = "T",
inline = "{0}.into_iter().map(types::String::from).collect()"
)]
types: types::Array,
}

/// Options passed to [`get_context()`](crate::get_context).
#[cfg(feature = "neovim-nightly")]
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
#[derive(Clone, Debug, Default)]
#[repr(C)]
pub struct GetContextOpts {
mask: u64,

/// 1st in the mask.
types: Array,
types: oxi_types::Object,
}

#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
impl GetContextOpts {
/// Creates a new [`GetContextOptsBuilder`].
#[inline]
Expand All @@ -29,29 +36,23 @@ impl GetContextOpts {
}
}

#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
#[derive(Clone, Default)]
pub struct GetContextOptsBuilder(GetContextOpts);

#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
impl GetContextOptsBuilder {
/// List of [`ContextType`]s to gather, or empty for all.
#[inline]
pub fn types<T>(&mut self, types: T) -> &mut Self
where
T: IntoIterator<Item = ContextType>,
{
let types =
types.into_iter().map(nvim::String::from).collect::<Array>();

#[cfg(not(feature = "neovim-nightly"))]
{
self.0.types = types.into();
}
#[cfg(feature = "neovim-nightly")]
{
self.0.types = types;
self.0.mask |= 0b11;
}

self.0.types = types
.into_iter()
.map(types::String::from)
.collect::<types::Array>()
.into();
self
}

Expand Down

0 comments on commit 60b9286

Please sign in to comment.