From e41318fb4ae92467a21d343be74e761d3b958854 Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Mon, 27 Feb 2023 22:33:13 +0800 Subject: [PATCH] feat: ShortCode framework of rust codec --- src/cli/src/main.rs | 3 +- src/rust_ffi/src/codec/mod.rs | 3 ++ src/rust_ffi/src/codec/short_code.rs | 69 ++++++++++++++++++++++++++++ src/rust_ffi/src/lib.rs | 2 + 4 files changed, 76 insertions(+), 1 deletion(-) create mode 100644 src/rust_ffi/src/codec/short_code.rs diff --git a/src/cli/src/main.rs b/src/cli/src/main.rs index f53f6eb..d4ac734 100644 --- a/src/cli/src/main.rs +++ b/src/cli/src/main.rs @@ -1,7 +1,7 @@ extern crate klotski_ffi; fn main() { - println!("{:#?}", klotski_ffi::metadata()); + // println!("{:#?}", klotski_ffi::metadata()); // println!("{}", klotski_ffi::is_short_code_available()); // println!("{}", klotski_ffi::is_short_code_available_fast()); @@ -31,4 +31,5 @@ fn main() { // println!("{}", klotski_ffi::common_code_from_string("1A9BF0C").unwrap()); // println!("{}", klotski_ffi::common_code_from_string("1A9BF0C00").unwrap()); + klotski_ffi::demo(); } diff --git a/src/rust_ffi/src/codec/mod.rs b/src/rust_ffi/src/codec/mod.rs index 57ae9b9..3dc33cb 100644 --- a/src/rust_ffi/src/codec/mod.rs +++ b/src/rust_ffi/src/codec/mod.rs @@ -1 +1,4 @@ mod ffi; +mod short_code; + +pub use short_code::demo; diff --git a/src/rust_ffi/src/codec/short_code.rs b/src/rust_ffi/src/codec/short_code.rs new file mode 100644 index 0000000..d5f9e56 --- /dev/null +++ b/src/rust_ffi/src/codec/short_code.rs @@ -0,0 +1,69 @@ +use std::fmt; +use super::ffi as codec_ffi; + +#[derive(Debug)] +pub struct ShortCode { + code: u32 +} + +impl ShortCode { + fn from(short_code: u32) -> Result { + match codec_ffi::short_code_check(short_code) { + true => Ok(ShortCode { + code: short_code + }), + _ => Err("invalid short code"), + } + } + + fn from_str(short_code: &str) -> Result { + Ok(ShortCode { + code: codec_ffi::short_code_from_string(short_code)? + }) + } + + fn to_string(&self) -> String { + codec_ffi::short_code_to_string_unsafe(self.code) + } + + fn unwrap(&self) -> u32 { + self.code + } + + fn warm_up() { + codec_ffi::short_code_enable(); + } + + fn warm_up_fast() { + codec_ffi::short_code_enable_fast(); + } + + // TODO: to CommonCode + // TODO: from CommonCode (u32 / String / CommonCode) + +} + +impl fmt::Display for ShortCode { + fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { + // TODO: using ShortCode string output + write!(f, "{}", self.code) + } +} + +impl PartialEq for ShortCode { + fn eq(&self, other: &Self) -> bool { + self.code == other.code + } +} + +impl Eq for ShortCode {} + +pub fn demo() { + + let s1 = ShortCode::from(12345).unwrap(); + let s2 = ShortCode::from(54321).unwrap(); + + // println!("ok -> {:?}", s); + // println!("ok -> {}", s.to_string()); + +} diff --git a/src/rust_ffi/src/lib.rs b/src/rust_ffi/src/lib.rs index a8ae7f5..aca44e0 100644 --- a/src/rust_ffi/src/lib.rs +++ b/src/rust_ffi/src/lib.rs @@ -3,3 +3,5 @@ mod codec; mod metadata; pub use metadata::load_metadata as metadata; + +pub use codec::demo;