Add from_ffi() to rgb_color_t

This allows converting a C++ rgb_color_t to a Rust RgbColor.
This commit is contained in:
ridiculousfish 2023-05-28 16:49:20 -07:00 committed by Peter Ammon
parent 8604be9a4f
commit 8f38e175ce
2 changed files with 56 additions and 0 deletions

View file

@ -453,6 +453,38 @@ fn term256_color_for_rgb(color: Color24) -> u8 {
(16 + convert_color(color, COLORS)).try_into().unwrap() (16 + convert_color(color, COLORS)).try_into().unwrap()
} }
/// FFI junk.
use crate::ffi::rgb_color_t;
impl rgb_color_t {
/// Convert from a C++ rgb_color_t to a Rust RgbColor.
#[allow(clippy::wrong_self_convention)]
pub fn from_ffi(&self) -> RgbColor {
let typ = if self.is_normal() {
Type::Normal
} else if self.is_reset() {
Type::Reset
} else if self.is_none() {
Type::None
} else if self.is_named() {
let idx = self.to_name_index();
Type::Named { idx }
} else if self.is_rgb() {
let [r, g, b] = self.to_color24().rgb;
Type::Rgb(Color24 { r, g, b })
} else {
unreachable!("Unknown color type")
};
let flags = Flags {
bold: self.is_bold(),
underline: self.is_underline(),
italics: self.is_italics(),
dim: self.is_dim(),
reverse: self.is_reverse(),
};
RgbColor { typ, flags }
}
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use crate::{ use crate::{
@ -497,3 +529,24 @@ mod tests {
} }
} }
} }
crate::ffi_tests::add_test!("test_colors_ffi", || {
use autocxx::WithinUniquePtr;
use moveit::moveit;
assert_eq!(RgbColor::WHITE, moveit!(rgb_color_t::white()).from_ffi());
assert_eq!(RgbColor::BLACK, moveit!(rgb_color_t::black()).from_ffi());
assert_eq!(RgbColor::RESET, moveit!(rgb_color_t::reset()).from_ffi());
assert_eq!(RgbColor::NORMAL, moveit!(rgb_color_t::normal()).from_ffi());
assert_eq!(RgbColor::NONE, moveit!(rgb_color_t::none()).from_ffi());
let mut cxx_color = rgb_color_t::black().within_unique_ptr();
cxx_color.as_mut().unwrap().set_bold(true);
cxx_color.as_mut().unwrap().set_dim(true);
let mut rust_color = RgbColor::BLACK;
assert_ne!(rust_color, cxx_color.as_ref().unwrap().from_ffi());
rust_color.set_bold(true);
assert_ne!(rust_color, cxx_color.as_ref().unwrap().from_ffi());
rust_color.set_dim(true);
assert_eq!(rust_color, cxx_color.as_ref().unwrap().from_ffi());
});

View file

@ -19,6 +19,7 @@ pub type wchar_t = u32;
include_cpp! { include_cpp! {
#include "builtin.h" #include "builtin.h"
#include "color.h"
#include "common.h" #include "common.h"
#include "complete.h" #include "complete.h"
#include "env.h" #include "env.h"
@ -130,6 +131,8 @@ include_cpp! {
generate!("function_exists") generate!("function_exists")
generate!("path_get_paths_ffi") generate!("path_get_paths_ffi")
generate!("rgb_color_t")
generate_pod!("color24_t")
generate!("colorize_shell") generate!("colorize_shell")
generate!("reader_status_count") generate!("reader_status_count")