Skip to content

Commit

Permalink
refactor: derive OptsBuilder for OptionValueOpts on nightly
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Dec 10, 2023
1 parent 60b9286 commit 147aaaf
Showing 1 changed file with 39 additions and 71 deletions.
110 changes: 39 additions & 71 deletions crates/oxi-api/src/opts/get_option_value.rs
Original file line number Diff line number Diff line change
@@ -1,116 +1,84 @@
#[cfg(not(feature = "neovim-nightly"))]
use oxi_types::Object;
use oxi_types::{self as nvim, conversion::FromObject};
#[cfg(feature = "neovim-nightly")]
use oxi_types::{BufHandle, String as NvimString, WinHandle};
use oxi_types as types;
use serde::Serialize;
use types::conversion::FromObject;

use crate::{Buffer, Window};

/// Options passed to
/// [`set_option_value()`](crate::set_option_value).
#[cfg(not(feature = "neovim-nightly"))]
#[derive(Clone, Debug, Default)]
#[cfg(feature = "neovim-nightly")]
#[derive(Clone, Debug, Default, oxi_macros::OptsBuilder)]
#[repr(C)]
pub struct OptionValueOpts {
buf: Object,
win: Object,
scope: Object,
filetype: Object,
#[builder(mask)]
mask: u64,

#[builder(argtype = "OptionScope", inline = "{0}.into()")]
scope: types::String,

#[builder(argtype = "Window", inline = "{0}.0")]
win: types::WinHandle,

#[builder(method = "buffer", argtype = "Buffer", inline = "{0}.0")]
buf: types::BufHandle,

#[builder(
generics = "F: Into<types::String>",
argtype = "F",
inline = "{0}.into()"
)]
filetype: types::String,
}

/// Options passed to
/// [`set_option_value()`](crate::set_option_value).
#[cfg(feature = "neovim-nightly")]
#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
#[derive(Clone, Debug, Default)]
#[repr(C)]
pub struct OptionValueOpts {
/// <filetype><scope><win><buf>1
mask: u64,

/// 3rd in the mask.
scope: NvimString,

/// 2nd in the mask.
win: WinHandle,

/// 1st in the mask.
buf: BufHandle,

/// 4th in the mask.
filetype: NvimString,
buf: types::Object,
win: types::Object,
scope: types::Object,
filetype: types::Object,
}

#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
impl OptionValueOpts {
#[inline(always)]
pub fn builder() -> OptionValueOptsBuilder {
OptionValueOptsBuilder::default()
}
}

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

#[cfg(any(feature = "neovim-0-8", feature = "neovim-0-9"))]
impl OptionValueOptsBuilder {
#[inline]
pub fn buffer(&mut self, buffer: Buffer) -> &mut Self {
#[cfg(not(feature = "neovim-nightly"))]
{
self.0.buf = buffer.into();
}
#[cfg(feature = "neovim-nightly")]
{
self.0.buf = buffer.0;
self.0.mask |= 0b11;
}
self.0.buf = buffer.into();
self
}

#[inline]
pub fn filetype(&mut self, filetype: &str) -> &mut Self {
let filetype = nvim::String::from(filetype);

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

let filetype = types::String::from(filetype);
self.0.filetype = filetype.into();
self
}

#[inline]
pub fn scope(&mut self, scope: OptionScope) -> &mut Self {
let scope = nvim::String::from(scope);

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

let scope = types::String::from(scope);
self.0.scope = scope.into();
self
}

#[inline]
pub fn window(&mut self, window: Window) -> &mut Self {
#[cfg(not(feature = "neovim-nightly"))]
{
self.0.win = window.into();
}
#[cfg(feature = "neovim-nightly")]
{
self.0.win = window.0;
self.0.mask |= 0b101;
}
self.0.win = window.into();
self
}

Expand All @@ -127,11 +95,11 @@ pub enum OptionScope {
Local,
}

impl From<OptionScope> for nvim::String {
impl From<OptionScope> for types::String {
#[inline]
fn from(ctx: OptionScope) -> Self {
nvim::String::from_object(
ctx.serialize(nvim::serde::Serializer::new())
types::String::from_object(
ctx.serialize(types::serde::Serializer::new())
.expect("`OptionScope` is serializable"),
)
.expect("`OptionScope` is serialized into a string")
Expand Down

0 comments on commit 147aaaf

Please sign in to comment.