From 7df1dea0f23fa1a5ffa0819f3c329a47f984197b Mon Sep 17 00:00:00 2001 From: Dnomd343 Date: Tue, 28 Feb 2023 15:26:22 +0800 Subject: [PATCH] feat: more conversion interfaces for rust codec --- src/rust_ffi/src/codec/common_code.rs | 71 +++++++++++++++++++++- src/rust_ffi/src/codec/raw_code.rs | 87 ++++++++++++++++++++++++++- src/rust_ffi/src/codec/short_code.rs | 71 +++++++++++++++++++++- 3 files changed, 222 insertions(+), 7 deletions(-) diff --git a/src/rust_ffi/src/codec/common_code.rs b/src/rust_ffi/src/codec/common_code.rs index 52e1579..9d411d7 100644 --- a/src/rust_ffi/src/codec/common_code.rs +++ b/src/rust_ffi/src/codec/common_code.rs @@ -82,8 +82,75 @@ impl CommonCode { } impl CommonCode { - // TODO: from RawCode (u64 / RawCode) - // TODO: from ShortCode (u32 / String / CommonCode) + /// Build CommonCode from RawCode. + /// # Example + /// ``` + /// use klotski_ffi::{RawCode, CommonCode}; + /// + /// let c = CommonCode::from_raw_code(&RawCode::from(0x0_603_EDF_5CA_FFF_5E2).unwrap()); + /// + /// println!("result: {}", c); + /// ``` + #[inline] + pub fn from_raw_code(raw_code: &RawCode) -> CommonCode { + raw_code.to_common_code() + } + + /// Build CommonCode from raw `u64` type raw code. + /// # Example + /// ``` + /// use klotski_ffi::CommonCode; + /// + /// let c = CommonCode::from_raw_code_val(0x0_603_EDF_5CA_FFF_5E2).unwrap(); + /// + /// println!("result: {}", c); + /// ``` + pub fn from_raw_code_val(raw_code: u64) -> Result { + let raw_code = RawCode::from(raw_code)?; + Ok(raw_code.to_common_code()) + } + + /// Build CommonCode from ShortCode. + /// # Example + /// ``` + /// use klotski_ffi::{ShortCode, CommonCode}; + /// + /// let c = CommonCode::from_short_code(&ShortCode::from(4091296).unwrap()); + /// + /// println!("result: {}", c); + /// ``` + #[inline] + pub fn from_short_code(short_code: &ShortCode) -> CommonCode { + short_code.to_common_code() + } + + /// Build CommonCode from raw `u32` type short code. + /// # Example + /// ``` + /// use klotski_ffi::CommonCode; + /// + /// let c = CommonCode::from_short_code_val(4091296).unwrap(); + /// + /// println!("result: {}", c); + /// ``` + pub fn from_short_code_val(short_code: u32) -> Result { + let short_code = ShortCode::from(short_code)?; + Ok(short_code.to_common_code()) + } + + /// Build CommonCode from raw `&str` type short code. + /// # Example + /// ``` + /// use klotski_ffi::CommonCode; + /// + /// let c = CommonCode::from_short_code_str("4WVE1").unwrap(); + /// + /// println!("result: {}", c); + /// ``` + pub fn from_short_code_str(short_code: &str) -> Result { + let short_code = ShortCode::from_str(short_code)?; + Ok(short_code.to_common_code()) + } } impl CommonCode { diff --git a/src/rust_ffi/src/codec/raw_code.rs b/src/rust_ffi/src/codec/raw_code.rs index 05c3890..819fc14 100644 --- a/src/rust_ffi/src/codec/raw_code.rs +++ b/src/rust_ffi/src/codec/raw_code.rs @@ -45,7 +45,7 @@ impl RawCode { /// ``` /// use klotski_ffi::RawCode; /// - /// match RawCode::from(0x0603_EDF5_CAFF_F5E2) { + /// match RawCode::from(0x0_603_EDF_5CA_FFF_5E2) { /// Ok(code) => println!("result: {}", code), /// Err(err) => println!("error: {}", err), /// } @@ -61,8 +61,89 @@ impl RawCode { } impl RawCode { - // TODO: from ShortCode (u32 / String / CommonCode) - // TODO: from CommonCode (u64 / String / CommonCode) + /// Build RawCode from ShortCode. + /// # Example + /// ``` + /// use klotski_ffi::{RawCode, ShortCode}; + /// + /// let r = RawCode::from_short_code(&ShortCode::from(4091296).unwrap()); + /// + /// println!("result: {}", r); + /// ``` + #[inline] + pub fn from_short_code(short_code: &ShortCode) -> RawCode { + short_code.to_raw_code() + } + + /// Build RawCode from raw `u32` type short code. + /// # Example + /// ``` + /// use klotski_ffi::RawCode; + /// + /// let r = RawCode::from_short_code_val(4091296).unwrap(); + /// + /// println!("result: {}", r); + /// ``` + pub fn from_short_code_val(short_code: u32) -> Result { + let short_code = ShortCode::from(short_code)?; + Ok(short_code.to_raw_code()) + } + + /// Build RawCode from raw `&str` type short code. + /// # Example + /// ``` + /// use klotski_ffi::RawCode; + /// + /// let r = RawCode::from_short_code_str("4WVE1").unwrap(); + /// + /// println!("result: {}", r); + /// ``` + pub fn from_short_code_str(short_code: &str) -> Result { + let short_code = ShortCode::from_str(short_code)?; + Ok(short_code.to_raw_code()) + } + + /// Build RawCode from CommonCode. + /// # Example + /// ``` + /// use klotski_ffi::{RawCode, CommonCode}; + /// + /// let r = RawCode::from_common_code(&CommonCode::from(0x1_A9BF_0C00).unwrap()); + /// + /// println!("result: {}", r); + /// ``` + #[inline] + pub fn from_common_code(common_code: &CommonCode) -> RawCode { + common_code.to_raw_code() + } + + /// Build RawCode from raw `u64` type common code. + /// # Example + /// ``` + /// use klotski_ffi::RawCode; + /// + /// let r = RawCode::from_common_code_val(0x1_A9BF_0C00).unwrap(); + /// + /// println!("result: {}", r); + /// ``` + pub fn from_common_code_val(common_code: u64) -> Result { + let common_code = CommonCode::from(common_code)?; + Ok(common_code.to_raw_code()) + } + + /// Build RawCode from raw `&str` type common code. + /// # Example + /// ``` + /// use klotski_ffi::RawCode; + /// + /// let r = RawCode::from_common_code_str("1A9BF0C00").unwrap(); + /// + /// println!("result: {}", r); + /// ``` + pub fn from_common_code_str(common_code: &str) -> Result { + let common_code = CommonCode::from_str(common_code)?; + Ok(common_code.to_raw_code()) + } } impl RawCode { diff --git a/src/rust_ffi/src/codec/short_code.rs b/src/rust_ffi/src/codec/short_code.rs index 08f021f..203b309 100644 --- a/src/rust_ffi/src/codec/short_code.rs +++ b/src/rust_ffi/src/codec/short_code.rs @@ -77,8 +77,75 @@ impl ShortCode { } impl ShortCode { - // TODO: from RawCode (u64 / RawCode) - // TODO: from CommonCode (u64 / String / CommonCode) + /// Build ShortCode from RawCode. + /// # Example + /// ``` + /// use klotski_ffi::{RawCode, ShortCode}; + /// + /// let s = ShortCode::from_raw_code(&RawCode::from(0x0_603_EDF_5CA_FFF_5E2).unwrap()); + /// + /// println!("result: {}", s); + /// ``` + #[inline] + pub fn from_raw_code(raw_code: &RawCode) -> ShortCode { + raw_code.to_short_code() + } + + /// Build ShortCode from raw `u64` type raw code. + /// # Example + /// ``` + /// use klotski_ffi::ShortCode; + /// + /// let s = ShortCode::from_raw_code_val(0x0_603_EDF_5CA_FFF_5E2).unwrap(); + /// + /// println!("result: {}", s); + /// ``` + pub fn from_raw_code_val(raw_code: u64) -> Result { + let raw_code = RawCode::from(raw_code)?; + Ok(raw_code.to_short_code()) + } + + /// Build ShortCode from CommonCode. + /// # Example + /// ``` + /// use klotski_ffi::{ShortCode, CommonCode}; + /// + /// let s = ShortCode::from_common_code(&CommonCode::from(0x1_A9BF_0C00).unwrap()); + /// + /// println!("result: {}", s); + /// ``` + #[inline] + pub fn from_common_code(common_code: &CommonCode) -> ShortCode { + common_code.to_short_code() + } + + /// Build ShortCode from raw `u64` type common code. + /// # Example + /// ``` + /// use klotski_ffi::ShortCode; + /// + /// let s = ShortCode::from_common_code_val(0x1_A9BF_0C00).unwrap(); + /// + /// println!("result: {}", s); + /// ``` + pub fn from_common_code_val(common_code: u64) -> Result { + let common_code = CommonCode::from(common_code)?; + Ok(common_code.to_short_code()) + } + + /// Build ShortCode from raw `&str` type common code. + /// # Example + /// ``` + /// use klotski_ffi::ShortCode; + /// + /// let s = ShortCode::from_common_code_str("1A9BF0C00").unwrap(); + /// + /// println!("result: {}", s); + /// ``` + pub fn from_common_code_str(common_code: &str) -> Result { + let common_code = CommonCode::from_str(common_code)?; + Ok(common_code.to_short_code()) + } } impl ShortCode {