Skip to content

Commit

Permalink
api: add OneOrMore enum for deserializing either singletons or strings
Browse files Browse the repository at this point in the history
  • Loading branch information
noib3 committed Oct 2, 2023
1 parent 33a8bc1 commit a692b0f
Show file tree
Hide file tree
Showing 4 changed files with 60 additions and 14 deletions.
4 changes: 2 additions & 2 deletions crates/oxi-api/src/types/extmark_infos.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use oxi_types::{
};
use serde::Deserialize;

use super::{ExtmarkHlMode, ExtmarkVirtTextPosition};
use super::{ExtmarkHlMode, ExtmarkVirtTextPosition, OneOrMore};

/// Extmark infos returned by `Buffer::get_extmark_by_id`.
#[non_exhaustive]
Expand Down Expand Up @@ -47,7 +47,7 @@ pub struct ExtmarkInfos {
pub virt_lines_leftcol: Option<bool>,

#[serde(default)]
pub virt_text: Option<Vec<(String, String)>>,
pub virt_text: Option<Vec<(String, OneOrMore<String>)>>,

#[serde(default)]
pub virt_text_hide: Option<bool>,
Expand Down
2 changes: 2 additions & 0 deletions crates/oxi-api/src/types/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mod log_level;
mod mode;
mod mouse_action;
mod mouse_button;
mod one_or_more;
mod option_infos;
mod parsed_viml_expression;
mod paste_phase;
Expand Down Expand Up @@ -73,6 +74,7 @@ pub use log_level::*;
pub use mode::*;
pub use mouse_action::*;
pub use mouse_button::*;
pub use one_or_more::*;
pub use option_infos::*;
pub use parsed_viml_expression::*;
pub use paste_phase::*;
Expand Down
26 changes: 26 additions & 0 deletions crates/oxi-api/src/types/one_or_more.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
use serde::Deserialize;

#[derive(Clone, Debug, Eq, PartialEq, Hash, Deserialize)]
#[serde(untagged)]
pub enum OneOrMore<T> {
One(T),
List(Vec<T>),
}

impl<T> From<T> for OneOrMore<T> {
fn from(one: T) -> Self {
OneOrMore::One(one)
}
}

impl<T> From<Vec<T>> for OneOrMore<T> {
fn from(vec: Vec<T>) -> Self {
OneOrMore::List(vec)
}
}

impl From<&str> for OneOrMore<String> {
fn from(s: &str) -> Self {
OneOrMore::One(s.to_owned())
}
}
42 changes: 30 additions & 12 deletions tests/src/api/extmark.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ fn get_extmarks() {
let (id, row, col, infos) = extmarks.into_iter().next().unwrap();

assert!(infos.is_some(), "no informations were returned");

let infos = infos.unwrap();

assert_eq!(extmark_id, id);
Expand All @@ -60,10 +61,19 @@ fn get_extmarks() {
assert_eq!(Some(0), infos.end_row);
assert_eq!(Some(String::from("Bar")), infos.hl_group);
assert_eq!(Some(ExtmarkHlMode::Combine), infos.hl_mode);
assert_eq!(
Some(vec![("".into(), "Foo".into()), ("foo".into(), "Bar".into())]),
infos.virt_text
);

#[cfg(feature = "neovim-nightly")]
let virt_text = vec![(
"foo".to_owned(),
vec!["Foo".to_owned(), "Bar".to_owned()].into(),
)];

#[cfg(not(feature = "neovim-nightly"))]
let virt_text =
vec![("".into(), "Foo".into()), ("foo".into(), "Bar".into())];

assert_eq!(Some(virt_text), infos.virt_text);

assert_eq!(Some(ExtmarkVirtTextPosition::Overlay), infos.virt_text_pos);
}

Expand Down Expand Up @@ -154,14 +164,22 @@ fn set_get_del_extmark() {
assert_eq!(Some(0), infos.end_row);
assert_eq!(Some(String::from("Bar")), infos.hl_group);
assert_eq!(Some(ExtmarkHlMode::Combine), infos.hl_mode);
assert_eq!(
Some(vec![
("foo".into(), "Foo".into()),
("".into(), "Bar".into()),
("bar".into(), "Baz".into())
]),
infos.virt_text
);

#[cfg(feature = "neovim-nightly")]
let virt_text = vec![
("foo".to_owned(), "Foo".into()),
("bar".to_owned(), vec!["Bar".to_owned(), "Baz".to_owned()].into()),
];

#[cfg(not(feature = "neovim-nightly"))]
let virt_text = vec![
("foo".into(), "Foo".into()),
("".into(), "Bar".into()),
("bar".into(), "Baz".into()),
];

assert_eq!(Some(virt_text), infos.virt_text);

assert_eq!(Some(ExtmarkVirtTextPosition::Overlay), infos.virt_text_pos);

let res = buf.del_extmark(ns_id, extmark_id);
Expand Down

0 comments on commit a692b0f

Please sign in to comment.