#[cxx::bridge(namespace = "klotski::ffi")]
mod ffi {
#[derive(Debug)]
struct RsLayout {
code: u64
}
#[derive(Debug)]
struct RsShortCode {
code: u32
}
unsafe extern "C++" {
include!("rust_ffi/include/layout.h");
fn layout_check(val: u64) -> bool;
fn layout_from_str(s: &str) -> u64;
fn to_string(self: &RsLayout) -> String;
fn to_shorten_string(self: &RsLayout) -> String;
fn to_short_code(self: &RsLayout) -> RsShortCode;
fn is_vertical_mirror(self: &RsLayout) -> bool;
fn is_horizontal_mirror(self: &RsLayout) -> bool;
fn to_vertical_mirror(self: &RsLayout) -> RsLayout;
fn to_horizontal_mirror(self: &RsLayout) -> RsLayout;
fn n_1x1(self: &RsLayout) -> u8;
fn n_1x2(self: &RsLayout) -> u8;
fn n_2x1(self: &RsLayout) -> u8;
fn n_2x2(self: &RsLayout) -> u8;
fn type_id(self: &RsLayout) -> u8;
fn pattern_id(self: &RsLayout) -> u16;
fn toward_char(self: &RsLayout) -> u8;
fn case_id(self: &RsLayout) -> u32;
fn next_cases(self: &RsLayout) -> Vec<RsLayout>;
}
unsafe extern "C++" {
include!("rust_ffi/include/short_code.h");
fn short_code_check(val: u32) -> bool;
fn short_code_from_str(s: &str) -> u32;
fn to_string(self: &RsShortCode) -> String;
fn to_layout(self: &RsShortCode) -> RsLayout;
fn short_code_speed_up(fast_mode: bool);
}
}
pub use ffi::RsLayout as Layout;
pub use ffi::RsShortCode as ShortCode;
impl Layout {
pub fn check(code: u64) -> bool {
ffi::layout_check(code)
}
pub fn unsafe_create(code: u64) -> Self {
Self { code }
}
pub fn create(code: u64) -> Option<Self> {
if Self::check(code) {
return Some(Self::unsafe_create(code));
}
None
}
pub fn unwrap(self: &Self) -> u64 {
self.code
}
pub fn from_string(s: &str) -> Option<Self> {
let val = ffi::layout_from_str(s);
if val < 0x10_FFFF_FFFF {
return Some(Self::unsafe_create(val));
}
None
}
}
impl ShortCode {
pub fn check(code: u32) -> bool {
ffi::short_code_check(code)
}
pub fn unsafe_create(code: u32) -> Self {
Self { code }
}
pub fn create(code: u32) -> Option<Self> {
if Self::check(code) {
return Some(Self::unsafe_create(code));
}
None
}
pub fn unwrap(self: &Self) -> u32 {
self.code
}
pub fn from_string(s: &str) -> Option<Self> {
let val = ffi::short_code_from_str(s);
if val != 0xFFFF_FFFF {
return Some(Self::unsafe_create(val));
}
None
}
pub fn speed_up(fast_mode: bool) {
ffi::short_code_speed_up(fast_mode)
}
}
impl PartialEq for Layout {
fn eq(&self, other: &Self) -> bool {
self.code == other.code
}
}
impl PartialEq for ShortCode {
fn eq(&self, other: &Self) -> bool {
self.code == other.code
}
}