Browse Source

test: update rust ffi doc-tests

legacy
Dnomd343 2 years ago
parent
commit
2034d9dab1
  1. 51
      src/rust_ffi/src/all_cases.rs
  2. 92
      src/rust_ffi/src/codec/common_code.rs
  3. 113
      src/rust_ffi/src/codec/raw_code.rs
  4. 81
      src/rust_ffi/src/codec/short_code.rs
  5. 7
      src/rust_ffi/src/metadata.rs

51
src/rust_ffi/src/all_cases.rs

@ -45,15 +45,22 @@ impl AllCases {
} }
/// Get all possible layouts and return an vector loaded with RawCode. /// Get all possible layouts and return an vector loaded with RawCode.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::AllCases; /// use klotski_ffi::{AllCases, RawCode};
/// ///
/// let all_raw_codes = AllCases::raw_codes(); /// let all_raw_codes = AllCases::raw_codes();
/// ///
/// for raw_code in &all_raw_codes[..16] { /// assert_eq!(all_raw_codes[..8], vec![
/// println!("{}", raw_code); /// RawCode::from(0x0_000_000_000_03F_03C).unwrap(),
/// } /// RawCode::from(0x0_600_000_000_03F_03C).unwrap(),
/// RawCode::from(0x0_E40_000_000_03F_03C).unwrap(),
/// RawCode::from(0x0_0C0_000_000_03F_03C).unwrap(),
/// RawCode::from(0x0_6C0_000_000_03F_03C).unwrap(),
/// RawCode::from(0x0_1C8_000_000_03F_03C).unwrap(),
/// RawCode::from(0x0_7C8_000_000_03F_03C).unwrap(),
/// RawCode::from(0x0_018_000_000_03F_03C).unwrap(),
/// ]);
/// ``` /// ```
#[inline] #[inline]
pub fn raw_codes() -> &'static Vec<RawCode> { pub fn raw_codes() -> &'static Vec<RawCode> {
@ -61,15 +68,22 @@ impl AllCases {
} }
/// Get all possible layouts and return an vector loaded with ShortCode. /// Get all possible layouts and return an vector loaded with ShortCode.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::AllCases; /// use klotski_ffi::{AllCases, ShortCode};
/// ///
/// let all_short_codes = AllCases::short_codes(); /// let all_short_codes = AllCases::short_codes();
/// ///
/// for short_code in &all_short_codes[..16] { /// assert_eq!(all_short_codes[..8], vec![
/// println!("{}", short_code); /// ShortCode::from(0).unwrap(),
/// } /// ShortCode::from(1).unwrap(),
/// ShortCode::from(2).unwrap(),
/// ShortCode::from(3).unwrap(),
/// ShortCode::from(4).unwrap(),
/// ShortCode::from(5).unwrap(),
/// ShortCode::from(6).unwrap(),
/// ShortCode::from(7).unwrap(),
/// ]);
/// ``` /// ```
#[inline] #[inline]
pub fn short_codes() -> &'static Vec<ShortCode> { pub fn short_codes() -> &'static Vec<ShortCode> {
@ -77,15 +91,22 @@ impl AllCases {
} }
/// Get all possible layouts and return an vector loaded with CommonCode. /// Get all possible layouts and return an vector loaded with CommonCode.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::AllCases; /// use klotski_ffi::{AllCases, CommonCode};
/// ///
/// let all_common_codes = AllCases::common_codes(); /// let all_common_codes = AllCases::common_codes();
/// ///
/// for common_code in &all_common_codes[..16] { /// assert_eq!(all_common_codes[..8], vec![
/// println!("{}", common_code); /// CommonCode::from(0x0_0000_0000).unwrap(),
/// } /// CommonCode::from(0x0_0000_0003).unwrap(),
/// CommonCode::from(0x0_0000_0004).unwrap(),
/// CommonCode::from(0x0_0000_000C).unwrap(),
/// CommonCode::from(0x0_0000_000F).unwrap(),
/// CommonCode::from(0x0_0000_0010).unwrap(),
/// CommonCode::from(0x0_0000_001C).unwrap(),
/// CommonCode::from(0x0_0000_0030).unwrap(),
/// ]);
/// ``` /// ```
#[inline] #[inline]
pub fn common_codes() -> &'static Vec<CommonCode> { pub fn common_codes() -> &'static Vec<CommonCode> {

92
src/rust_ffi/src/codec/common_code.rs

@ -41,14 +41,14 @@ impl CommonCode {
} }
/// Create common code from raw `u64`, and will be checked for validity. /// Create common code from raw `u64`, and will be checked for validity.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::CommonCode; /// use klotski_ffi::CommonCode;
/// ///
/// match CommonCode::from(0x1_A9BF_0C00) { /// let common_code = CommonCode::from(0x1_A9BF_0C00);
/// Ok(code) => println!("result: {}", code), ///
/// Err(err) => println!("error: {}", err), /// assert!(common_code.is_ok());
/// } /// assert_eq!(common_code.unwrap().unwrap(), 0x1_A9BF_0C00);
/// ``` /// ```
pub fn from(common_code: u64) -> Result<CommonCode, &'static str> { pub fn from(common_code: u64) -> Result<CommonCode, &'static str> {
match CommonCode::check(common_code) { match CommonCode::check(common_code) {
@ -60,19 +60,19 @@ impl CommonCode {
} }
/// Create common code from raw `&str`, and will be checked for validity. /// Create common code from raw `&str`, and will be checked for validity.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::CommonCode; /// use klotski_ffi::CommonCode;
/// ///
/// match CommonCode::from_str("1A9BF0C00") { /// let common_code = CommonCode::from_str("1A9BF0C00");
/// Ok(code) => println!("result: {}", code), ///
/// Err(err) => println!("error: {}", err), /// assert!(common_code.is_ok());
/// } /// assert_eq!(common_code.unwrap().unwrap(), 0x1_A9BF_0C00);
/// ///
/// match CommonCode::from_str("1a9bf0c") { /// let common_code = CommonCode::from_str("1a9bf0c");
/// Ok(code) => println!("result: {}", code), ///
/// Err(err) => println!("error: {}", err), /// assert!(common_code.is_ok());
/// } /// assert_eq!(common_code.unwrap().unwrap(), 0x1_A9BF_0C00);
/// ``` /// ```
pub fn from_str(common_code: &str) -> Result<CommonCode, &'static str> { pub fn from_str(common_code: &str) -> Result<CommonCode, &'static str> {
Ok(CommonCode { Ok(CommonCode {
@ -83,13 +83,14 @@ impl CommonCode {
impl CommonCode { impl CommonCode {
/// Build CommonCode from RawCode. /// Build CommonCode from RawCode.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::{RawCode, CommonCode}; /// use klotski_ffi::{RawCode, CommonCode};
/// ///
/// let c = CommonCode::from_raw_code(&RawCode::from(0x0_603_EDF_5CA_FFF_5E2).unwrap()); /// let raw_code = RawCode::from(0x0_603_EDF_5CA_FFF_5E2).unwrap();
/// let common_code = CommonCode::from_raw_code(&raw_code);
/// ///
/// println!("result: {}", c); /// assert_eq!(common_code.unwrap(), 0x1_A9BF_0C00);
/// ``` /// ```
#[inline] #[inline]
pub fn from_raw_code(raw_code: &RawCode) -> CommonCode { pub fn from_raw_code(raw_code: &RawCode) -> CommonCode {
@ -97,13 +98,13 @@ impl CommonCode {
} }
/// Build CommonCode from raw `u64` type raw code. /// Build CommonCode from raw `u64` type raw code.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::CommonCode; /// use klotski_ffi::CommonCode;
/// ///
/// let c = CommonCode::from_raw_code_val(0x0_603_EDF_5CA_FFF_5E2).unwrap(); /// let common_code = CommonCode::from_raw_code_val(0x0_603_EDF_5CA_FFF_5E2).unwrap();
/// ///
/// println!("result: {}", c); /// assert_eq!(common_code.unwrap(), 0x1_A9BF_0C00);
/// ``` /// ```
pub fn from_raw_code_val(raw_code: u64) -> Result<CommonCode, &'static str> { pub fn from_raw_code_val(raw_code: u64) -> Result<CommonCode, &'static str> {
let raw_code = RawCode::from(raw_code)?; let raw_code = RawCode::from(raw_code)?;
@ -111,13 +112,13 @@ impl CommonCode {
} }
/// Build CommonCode from ShortCode. /// Build CommonCode from ShortCode.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::{ShortCode, CommonCode}; /// use klotski_ffi::{ShortCode, CommonCode};
/// ///
/// let c = CommonCode::from_short_code(&ShortCode::from(4091296).unwrap()); /// let common_code = CommonCode::from_short_code(&ShortCode::from(4091296).unwrap());
/// ///
/// println!("result: {}", c); /// assert_eq!(common_code.unwrap(), 0x1_A9BF_0C00);
/// ``` /// ```
#[inline] #[inline]
pub fn from_short_code(short_code: &ShortCode) -> CommonCode { pub fn from_short_code(short_code: &ShortCode) -> CommonCode {
@ -125,13 +126,13 @@ impl CommonCode {
} }
/// Build CommonCode from raw `u32` type short code. /// Build CommonCode from raw `u32` type short code.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::CommonCode; /// use klotski_ffi::CommonCode;
/// ///
/// let c = CommonCode::from_short_code_val(4091296).unwrap(); /// let common_code = CommonCode::from_short_code_val(4091296).unwrap();
/// ///
/// println!("result: {}", c); /// assert_eq!(common_code.unwrap(), 0x1_A9BF_0C00);
/// ``` /// ```
pub fn from_short_code_val(short_code: u32) -> Result<CommonCode, &'static str> { pub fn from_short_code_val(short_code: u32) -> Result<CommonCode, &'static str> {
let short_code = ShortCode::from(short_code)?; let short_code = ShortCode::from(short_code)?;
@ -139,13 +140,13 @@ impl CommonCode {
} }
/// Build CommonCode from raw `&str` type short code. /// Build CommonCode from raw `&str` type short code.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::CommonCode; /// use klotski_ffi::CommonCode;
/// ///
/// let c = CommonCode::from_short_code_str("4WVE1").unwrap(); /// let common_code = CommonCode::from_short_code_str("4WVE1").unwrap();
/// ///
/// println!("result: {}", c); /// assert_eq!(common_code.unwrap(), 0x1_A9BF_0C00);
/// ``` /// ```
pub fn from_short_code_str(short_code: &str) -> Result<CommonCode, &'static str> { pub fn from_short_code_str(short_code: &str) -> Result<CommonCode, &'static str> {
let short_code = ShortCode::from_str(short_code)?; let short_code = ShortCode::from_str(short_code)?;
@ -155,12 +156,13 @@ impl CommonCode {
impl CommonCode { impl CommonCode {
/// Return the original `u64` type common code value. /// Return the original `u64` type common code value.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::CommonCode; /// use klotski_ffi::CommonCode;
/// ///
/// let code = CommonCode::from_str("1A9BF0C00").expect("invalid common code"); /// let common_code = CommonCode::from_str("1A9BF0C00").expect("invalid common code");
/// println!("original: {}", code.unwrap()); ///
/// assert_eq!(common_code.unwrap(), 0x1_A9BF_0C00);
/// ``` /// ```
#[inline] #[inline]
pub fn unwrap(&self) -> u64 { pub fn unwrap(&self) -> u64 {
@ -168,12 +170,13 @@ impl CommonCode {
} }
/// Returns the common code encoded as a string. /// Returns the common code encoded as a string.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::CommonCode; /// use klotski_ffi::CommonCode;
/// ///
/// let code = CommonCode::from(0x1_A9BF_0C00).expect("invalid common code"); /// let common_code = CommonCode::from(0x1_A9BF_0C00).expect("invalid common code");
/// println!("common code string: {}", code.to_string()); ///
/// assert_eq!(common_code.to_string(), "1A9BF0C00");
/// ``` /// ```
#[inline] #[inline]
pub fn to_string(&self) -> String { pub fn to_string(&self) -> String {
@ -183,12 +186,13 @@ impl CommonCode {
/// Returns the common code encoded as a shorten string, based on the /// Returns the common code encoded as a shorten string, based on the
/// provisions of the common code, it is allowed to omit the last `0` /// provisions of the common code, it is allowed to omit the last `0`
/// of the string (but at least one byte length must be reserved). /// of the string (but at least one byte length must be reserved).
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::CommonCode; /// use klotski_ffi::CommonCode;
/// ///
/// let code = CommonCode::from(0x1_A9BF_0C00).expect("invalid common code"); /// let common_code = CommonCode::from(0x1_A9BF_0C00).expect("invalid common code");
/// println!("common code shorten string: {}", code.to_string_shorten()); ///
/// assert_eq!(common_code.to_string_shorten(), "1A9BF0C");
/// ``` /// ```
#[inline] #[inline]
pub fn to_string_shorten(&self) -> String { pub fn to_string_shorten(&self) -> String {
@ -196,13 +200,13 @@ impl CommonCode {
} }
/// Convert CommonCode to RawCode type. /// Convert CommonCode to RawCode type.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::CommonCode; /// use klotski_ffi::CommonCode;
/// ///
/// let common_code = CommonCode::from_str("1A9BF0C00").unwrap(); /// let common_code = CommonCode::from_str("1A9BF0C00").unwrap();
/// let raw_code = common_code.to_raw_code(); ///
/// println!("{} => {}", common_code, raw_code); /// assert_eq!(common_code.to_raw_code().unwrap(), 0x0_603_EDF_5CA_FFF_5E2);
/// ``` /// ```
#[inline] #[inline]
pub fn to_raw_code(&self) -> RawCode { pub fn to_raw_code(&self) -> RawCode {
@ -213,13 +217,13 @@ impl CommonCode {
/// Convert CommonCode to ShortCode type, note that it will take a long time if there /// Convert CommonCode to ShortCode type, note that it will take a long time if there
/// is no warm-up index. /// is no warm-up index.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::CommonCode; /// use klotski_ffi::CommonCode;
/// ///
/// let common_code = CommonCode::from_str("1A9BF0C00").unwrap(); /// let common_code = CommonCode::from_str("1A9BF0C00").unwrap();
/// let short_code = common_code.to_short_code(); ///
/// println!("{} => {}", common_code, short_code); /// assert_eq!(common_code.to_short_code().unwrap(), 4091296);
/// ``` /// ```
#[inline] #[inline]
pub fn to_short_code(&self) -> ShortCode { pub fn to_short_code(&self) -> ShortCode {

113
src/rust_ffi/src/codec/raw_code.rs

@ -41,14 +41,14 @@ impl RawCode {
} }
/// Create raw code from raw `u64`, and will be checked for validity. /// Create raw code from raw `u64`, and will be checked for validity.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::RawCode; /// use klotski_ffi::RawCode;
/// ///
/// match RawCode::from(0x0_603_EDF_5CA_FFF_5E2) { /// let raw_code = RawCode::from(0x0_603_EDF_5CA_FFF_5E2);
/// Ok(code) => println!("result: {}", code), ///
/// Err(err) => println!("error: {}", err), /// assert!(raw_code.is_ok());
/// } /// assert_eq!(raw_code.unwrap().unwrap(), 0x0_603_EDF_5CA_FFF_5E2);
/// ``` /// ```
pub fn from(raw_code: u64) -> Result<RawCode, &'static str> { pub fn from(raw_code: u64) -> Result<RawCode, &'static str> {
match RawCode::check(raw_code) { match RawCode::check(raw_code) {
@ -62,13 +62,14 @@ impl RawCode {
impl RawCode { impl RawCode {
/// Build RawCode from ShortCode. /// Build RawCode from ShortCode.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::{RawCode, ShortCode}; /// use klotski_ffi::{RawCode, ShortCode};
/// ///
/// let r = RawCode::from_short_code(&ShortCode::from(4091296).unwrap()); /// let short_code = ShortCode::from(4091296).unwrap();
/// let raw_code = RawCode::from_short_code(&short_code);
/// ///
/// println!("result: {}", r); /// assert_eq!(raw_code.unwrap(), 0x0_603_EDF_5CA_FFF_5E2);
/// ``` /// ```
#[inline] #[inline]
pub fn from_short_code(short_code: &ShortCode) -> RawCode { pub fn from_short_code(short_code: &ShortCode) -> RawCode {
@ -76,13 +77,13 @@ impl RawCode {
} }
/// Build RawCode from raw `u32` type short code. /// Build RawCode from raw `u32` type short code.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::RawCode; /// use klotski_ffi::RawCode;
/// ///
/// let r = RawCode::from_short_code_val(4091296).unwrap(); /// let raw_code = RawCode::from_short_code_val(4091296).unwrap();
/// ///
/// println!("result: {}", r); /// assert_eq!(raw_code.unwrap(), 0x0_603_EDF_5CA_FFF_5E2);
/// ``` /// ```
pub fn from_short_code_val(short_code: u32) -> Result<RawCode, &'static str> { pub fn from_short_code_val(short_code: u32) -> Result<RawCode, &'static str> {
let short_code = ShortCode::from(short_code)?; let short_code = ShortCode::from(short_code)?;
@ -90,13 +91,13 @@ impl RawCode {
} }
/// Build RawCode from raw `&str` type short code. /// Build RawCode from raw `&str` type short code.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::RawCode; /// use klotski_ffi::RawCode;
/// ///
/// let r = RawCode::from_short_code_str("4WVE1").unwrap(); /// let raw_code = RawCode::from_short_code_str("4WVE1").unwrap();
/// ///
/// println!("result: {}", r); /// assert_eq!(raw_code.unwrap(), 0x0_603_EDF_5CA_FFF_5E2);
/// ``` /// ```
pub fn from_short_code_str(short_code: &str) -> Result<RawCode, &'static str> { pub fn from_short_code_str(short_code: &str) -> Result<RawCode, &'static str> {
let short_code = ShortCode::from_str(short_code)?; let short_code = ShortCode::from_str(short_code)?;
@ -104,13 +105,14 @@ impl RawCode {
} }
/// Build RawCode from CommonCode. /// Build RawCode from CommonCode.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::{RawCode, CommonCode}; /// use klotski_ffi::{RawCode, CommonCode};
/// ///
/// let r = RawCode::from_common_code(&CommonCode::from(0x1_A9BF_0C00).unwrap()); /// let common_code = CommonCode::from(0x1_A9BF_0C00).unwrap();
/// let raw_code = RawCode::from_common_code(&common_code);
/// ///
/// println!("result: {}", r); /// assert_eq!(raw_code.unwrap(), 0x0_603_EDF_5CA_FFF_5E2);
/// ``` /// ```
#[inline] #[inline]
pub fn from_common_code(common_code: &CommonCode) -> RawCode { pub fn from_common_code(common_code: &CommonCode) -> RawCode {
@ -118,13 +120,13 @@ impl RawCode {
} }
/// Build RawCode from raw `u64` type common code. /// Build RawCode from raw `u64` type common code.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::RawCode; /// use klotski_ffi::RawCode;
/// ///
/// let r = RawCode::from_common_code_val(0x1_A9BF_0C00).unwrap(); /// let raw_code = RawCode::from_common_code_val(0x1_A9BF_0C00).unwrap();
/// ///
/// println!("result: {}", r); /// assert_eq!(raw_code.unwrap(), 0x0_603_EDF_5CA_FFF_5E2);
/// ``` /// ```
pub fn from_common_code_val(common_code: u64) -> Result<RawCode, &'static str> { pub fn from_common_code_val(common_code: u64) -> Result<RawCode, &'static str> {
let common_code = CommonCode::from(common_code)?; let common_code = CommonCode::from(common_code)?;
@ -132,13 +134,13 @@ impl RawCode {
} }
/// Build RawCode from raw `&str` type common code. /// Build RawCode from raw `&str` type common code.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::RawCode; /// use klotski_ffi::RawCode;
/// ///
/// let r = RawCode::from_common_code_str("1A9BF0C00").unwrap(); /// let raw_code = RawCode::from_common_code_str("1A9BF0C00").unwrap();
/// ///
/// println!("result: {}", r); /// assert_eq!(raw_code.unwrap(), 0x0_603_EDF_5CA_FFF_5E2);
/// ``` /// ```
pub fn from_common_code_str(common_code: &str) -> Result<RawCode, &'static str> { pub fn from_common_code_str(common_code: &str) -> Result<RawCode, &'static str> {
let common_code = CommonCode::from_str(common_code)?; let common_code = CommonCode::from_str(common_code)?;
@ -148,12 +150,13 @@ impl RawCode {
impl RawCode { impl RawCode {
/// Return the original `u64` type raw code value. /// Return the original `u64` type raw code value.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::RawCode; /// use klotski_ffi::RawCode;
/// ///
/// let code = RawCode::from(0x0603_EDF5_CAFF_F5E2).expect("invalid raw code"); /// let raw_code = RawCode::from(0x0_603_EDF_5CA_FFF_5E2).expect("invalid raw code");
/// println!("original: {}", code.unwrap()); ///
/// assert_eq!(raw_code.unwrap(), 0x0_603_EDF_5CA_FFF_5E2);
/// ``` /// ```
#[inline] #[inline]
pub fn unwrap(&self) -> u64 { pub fn unwrap(&self) -> u64 {
@ -162,13 +165,14 @@ impl RawCode {
/// Convert RawCode to ShortCode type, note that it will take a long time if there /// Convert RawCode to ShortCode type, note that it will take a long time if there
/// is no warm-up index. /// is no warm-up index.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::RawCode; /// use klotski_ffi::RawCode;
/// ///
/// let raw_code = RawCode::from(0x0603_EDF5_CAFF_F5E2).unwrap(); /// let raw_code = RawCode::from(0x0_603_EDF_5CA_FFF_5E2).unwrap();
/// let short_code = raw_code.to_short_code(); /// let short_code = raw_code.to_short_code();
/// println!("{} => {}", raw_code, short_code); ///
/// assert_eq!(short_code.unwrap(), 4091296);
/// ``` /// ```
#[inline] #[inline]
pub fn to_short_code(&self) -> ShortCode { pub fn to_short_code(&self) -> ShortCode {
@ -178,13 +182,14 @@ impl RawCode {
} }
/// Convert RawCode to CommonCode type. /// Convert RawCode to CommonCode type.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::RawCode; /// use klotski_ffi::RawCode;
/// ///
/// let raw_code = RawCode::from(0x0603_EDF5_CAFF_F5E2).unwrap(); /// let raw_code = RawCode::from(0x0_603_EDF_5CA_FFF_5E2).unwrap();
/// let common_code = raw_code.to_common_code(); /// let common_code = raw_code.to_common_code();
/// println!("{} => {}", raw_code, common_code); ///
/// assert_eq!(common_code.unwrap(), 0x1_A9BF_0C00);
/// ``` /// ```
#[inline] #[inline]
pub fn to_common_code(&self) -> CommonCode { pub fn to_common_code(&self) -> CommonCode {
@ -196,14 +201,14 @@ impl RawCode {
impl RawCode { impl RawCode {
/// Determine whether two layouts are mirrored vertically. /// Determine whether two layouts are mirrored vertically.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::RawCode; /// use klotski_ffi::RawCode;
/// ///
/// let r1 = RawCode::from(0x0_E58_FC8_5FF_EBC_4DB).unwrap(); // 4FEA13400 /// let r1 = RawCode::from(0x0_E58_FC8_5FF_EBC_4DB).unwrap(); // 4FEA13400
/// let r2 = RawCode::from(0x0_EDB_5FF_EBC_5C8_E58).unwrap(); // 8346AFC00 /// let r2 = RawCode::from(0x0_EDB_5FF_EBC_5C8_E58).unwrap(); // 8346AFC00
/// ///
/// println!("is vertical mirror: {}", RawCode::is_vertical_mirror(&r1, &r2)); /// assert!(RawCode::is_vertical_mirror(&r1, &r2));
/// ``` /// ```
#[inline] #[inline]
pub fn is_vertical_mirror(r1: &RawCode, r2: &RawCode) -> bool { pub fn is_vertical_mirror(r1: &RawCode, r2: &RawCode) -> bool {
@ -211,14 +216,14 @@ impl RawCode {
} }
/// Determine whether two layouts are mirrored horizontally. /// Determine whether two layouts are mirrored horizontally.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::RawCode; /// use klotski_ffi::RawCode;
/// ///
/// let r1 = RawCode::from(0x0_E58_FC8_5FF_EBC_4DB).unwrap(); // 4FEA13400 /// let r1 = RawCode::from(0x0_E58_FC8_5FF_EBC_4DB).unwrap(); // 4FEA13400
/// let r2 = RawCode::from(0x0_0F9_1CF_FFA_F17_6DA).unwrap(); // 6BFA47000 /// let r2 = RawCode::from(0x0_0F9_1CF_FFA_F17_6DA).unwrap(); // 6BFA47000
/// ///
/// println!("is horizontal mirror: {}", RawCode::is_horizontal_mirror(&r1, &r2)); /// assert!(RawCode::is_horizontal_mirror(&r1, &r2));
/// ``` /// ```
#[inline] #[inline]
pub fn is_horizontal_mirror(r1: &RawCode, r2: &RawCode) -> bool { pub fn is_horizontal_mirror(r1: &RawCode, r2: &RawCode) -> bool {
@ -226,14 +231,14 @@ impl RawCode {
} }
/// Determine whether the current layout is vertically mirrored with another layout. /// Determine whether the current layout is vertically mirrored with another layout.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::RawCode; /// use klotski_ffi::RawCode;
/// ///
/// let r1 = RawCode::from(0x0_E58_FC8_5FF_EBC_4DB).unwrap(); // 4FEA13400 /// let r1 = RawCode::from(0x0_E58_FC8_5FF_EBC_4DB).unwrap(); // 4FEA13400
/// let r2 = RawCode::from(0x0_EDB_5FF_EBC_5C8_E58).unwrap(); // 8346AFC00 /// let r2 = RawCode::from(0x0_EDB_5FF_EBC_5C8_E58).unwrap(); // 8346AFC00
/// ///
/// println!("is vertical mirror: {}", r1.is_vertical_mirror_with(&r2)); /// assert!(r1.is_vertical_mirror_with(&r2));
/// ``` /// ```
#[inline] #[inline]
pub fn is_vertical_mirror_with(&self, raw_code: &RawCode) -> bool { pub fn is_vertical_mirror_with(&self, raw_code: &RawCode) -> bool {
@ -241,14 +246,14 @@ impl RawCode {
} }
/// Determine whether the current layout is horizontally mirrored with another layout. /// Determine whether the current layout is horizontally mirrored with another layout.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::RawCode; /// use klotski_ffi::RawCode;
/// ///
/// let r1 = RawCode::from(0x0_E58_FC8_5FF_EBC_4DB).unwrap(); // 4FEA13400 /// let r1 = RawCode::from(0x0_E58_FC8_5FF_EBC_4DB).unwrap(); // 4FEA13400
/// let r2 = RawCode::from(0x0_0F9_1CF_FFA_F17_6DA).unwrap(); // 6BFA47000 /// let r2 = RawCode::from(0x0_0F9_1CF_FFA_F17_6DA).unwrap(); // 6BFA47000
/// ///
/// println!("is horizontal mirror: {}", r1.is_horizontal_mirror_with(&r2)); /// assert!(r1.is_horizontal_mirror_with(&r2));
/// ``` /// ```
#[inline] #[inline]
pub fn is_horizontal_mirror_with(&self, raw_code: &RawCode) -> bool { pub fn is_horizontal_mirror_with(&self, raw_code: &RawCode) -> bool {
@ -256,13 +261,13 @@ impl RawCode {
} }
/// Determine whether the layout is vertically symmetrical. /// Determine whether the layout is vertically symmetrical.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::RawCode; /// use klotski_ffi::RawCode;
/// ///
/// let r = RawCode::from(0x0_603_EDF_5CA_FFF_5E2).unwrap(); // 1A9BF0C00 /// let r = RawCode::from(0x0_603_EDF_5CA_FFF_5E2).unwrap(); // 1A9BF0C00
/// ///
/// println!("is vertically symmetrical: {}", r.is_self_vertical_mirror()); /// assert!(!r.is_self_vertical_mirror());
/// ``` /// ```
#[inline] #[inline]
pub fn is_self_vertical_mirror(&self) -> bool { pub fn is_self_vertical_mirror(&self) -> bool {
@ -270,13 +275,13 @@ impl RawCode {
} }
/// Determine whether the layout is horizontally symmetrical. /// Determine whether the layout is horizontally symmetrical.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::RawCode; /// use klotski_ffi::RawCode;
/// ///
/// let r = RawCode::from(0x0_603_EDF_5CA_FFF_5E2).unwrap(); // 1A9BF0C00 /// let r = RawCode::from(0x0_603_EDF_5CA_FFF_5E2).unwrap(); // 1A9BF0C00
/// ///
/// println!("is horizontally symmetrical: {}", r.is_self_horizontal_mirror()); /// assert!(r.is_self_horizontal_mirror());
/// ``` /// ```
#[inline] #[inline]
pub fn is_self_horizontal_mirror(&self) -> bool { pub fn is_self_horizontal_mirror(&self) -> bool {
@ -284,12 +289,13 @@ impl RawCode {
} }
/// Get vertical mirroring of the layout. /// Get vertical mirroring of the layout.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::RawCode; /// use klotski_ffi::RawCode;
/// ///
/// let r = RawCode::from(0x0_E58_FC8_5FF_EBC_4DB).unwrap(); // 4FEA13400 /// let r = RawCode::from(0x0_E58_FC8_5FF_EBC_4DB).unwrap(); // 4FEA13400
/// println!("vertical mirror: {}", r.to_vertical_mirror()); ///
/// assert_eq!(r.to_vertical_mirror(), RawCode::from(0x0_EDB_5FF_EBC_5C8_E58).unwrap());
/// ``` /// ```
#[inline] #[inline]
pub fn to_vertical_mirror(&self) -> RawCode { pub fn to_vertical_mirror(&self) -> RawCode {
@ -299,12 +305,13 @@ impl RawCode {
} }
/// Get horizontally mirroring of the layout. /// Get horizontally mirroring of the layout.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::RawCode; /// use klotski_ffi::RawCode;
/// ///
/// let r = RawCode::from(0x0_E58_FC8_5FF_EBC_4DB).unwrap(); // 4FEA13400 /// let r = RawCode::from(0x0_E58_FC8_5FF_EBC_4DB).unwrap(); // 4FEA13400
/// println!("horizontal mirror: {}", r.to_horizontal_mirror()); ///
/// assert_eq!(r.to_horizontal_mirror(), RawCode::from(0x0_0F9_1CF_FFA_F17_6DA).unwrap());
/// ``` /// ```
#[inline] #[inline]
pub fn to_horizontal_mirror(&self) -> RawCode { pub fn to_horizontal_mirror(&self) -> RawCode {
@ -324,12 +331,12 @@ pub(crate) mod tests {
pub(crate) const TEST_CODE_ERR: u64 = 0x0_A34_182_B38_102_D21; pub(crate) const TEST_CODE_ERR: u64 = 0x0_A34_182_B38_102_D21;
const TEST_MIRROR_V: u64 = 0x0_FC0_480_6DB_FC0_480; // only for test const TEST_MIRROR_V: u64 = 0x0_FC0_480_6DB_FC0_480; // only for test
const TEST_MIRROR_V1: u64 = 0x0_E58_FC8_5FF_EBC_4DB; const TEST_MIRROR_V1: u64 = 0x0_E58_FC8_5FF_EBC_4DB; // 4FEA13400
const TEST_MIRROR_V2: u64 = 0x0_EDB_5FF_EBC_5C8_E58; const TEST_MIRROR_V2: u64 = 0x0_EDB_5FF_EBC_5C8_E58; // 8346AFC00
const TEST_MIRROR_H: u64 = 0x0_603_EDF_5CA_FFF_5E2; const TEST_MIRROR_H: u64 = 0x0_603_EDF_5CA_FFF_5E2; // 1A9BF0C00
const TEST_MIRROR_H1: u64 = 0x0_E58_FC8_5FF_EBC_4DB; const TEST_MIRROR_H1: u64 = 0x0_E58_FC8_5FF_EBC_4DB; // 4FEA13400
const TEST_MIRROR_H2: u64 = 0x0_0F9_1CF_FFA_F17_6DA; const TEST_MIRROR_H2: u64 = 0x0_0F9_1CF_FFA_F17_6DA; // 6BFA47000
#[test] #[test]
fn construct() { fn construct() {

81
src/rust_ffi/src/codec/short_code.rs

@ -41,14 +41,14 @@ impl ShortCode {
} }
/// Create short code from raw `u32`, and will be checked for validity. /// Create short code from raw `u32`, and will be checked for validity.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::ShortCode; /// use klotski_ffi::ShortCode;
/// ///
/// match ShortCode::from(4091296) { /// let short_code = ShortCode::from(4091296);
/// Ok(code) => println!("result: {}", code), ///
/// Err(err) => println!("error: {}", err), /// assert!(short_code.is_ok());
/// } /// assert_eq!(short_code.unwrap().unwrap(), 4091296);
/// ``` /// ```
pub fn from(short_code: u32) -> Result<ShortCode, &'static str> { pub fn from(short_code: u32) -> Result<ShortCode, &'static str> {
match ShortCode::check(short_code) { match ShortCode::check(short_code) {
@ -60,14 +60,14 @@ impl ShortCode {
} }
/// Create short code from raw `&str`, and will be checked for validity. /// Create short code from raw `&str`, and will be checked for validity.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::ShortCode; /// use klotski_ffi::ShortCode;
/// ///
/// match ShortCode::from_str("4WVE1") { /// let short_code = ShortCode::from_str("4WVE1");
/// Ok(code) => println!("result: {}", code), ///
/// Err(err) => println!("error: {}", err), /// assert!(short_code.is_ok());
/// } /// assert_eq!(short_code.unwrap().unwrap(), 4091296);
/// ``` /// ```
pub fn from_str(short_code: &str) -> Result<ShortCode, &'static str> { pub fn from_str(short_code: &str) -> Result<ShortCode, &'static str> {
Ok(ShortCode { Ok(ShortCode {
@ -78,13 +78,14 @@ impl ShortCode {
impl ShortCode { impl ShortCode {
/// Build ShortCode from RawCode. /// Build ShortCode from RawCode.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::{RawCode, ShortCode}; /// use klotski_ffi::{RawCode, ShortCode};
/// ///
/// let s = ShortCode::from_raw_code(&RawCode::from(0x0_603_EDF_5CA_FFF_5E2).unwrap()); /// let raw_code = RawCode::from(0x0_603_EDF_5CA_FFF_5E2).unwrap();
/// let short_code = ShortCode::from_raw_code(&raw_code);
/// ///
/// println!("result: {}", s); /// assert_eq!(short_code.unwrap(), 4091296);
/// ``` /// ```
#[inline] #[inline]
pub fn from_raw_code(raw_code: &RawCode) -> ShortCode { pub fn from_raw_code(raw_code: &RawCode) -> ShortCode {
@ -92,13 +93,13 @@ impl ShortCode {
} }
/// Build ShortCode from raw `u64` type raw code. /// Build ShortCode from raw `u64` type raw code.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::ShortCode; /// use klotski_ffi::ShortCode;
/// ///
/// let s = ShortCode::from_raw_code_val(0x0_603_EDF_5CA_FFF_5E2).unwrap(); /// let short_code = ShortCode::from_raw_code_val(0x0_603_EDF_5CA_FFF_5E2).unwrap();
/// ///
/// println!("result: {}", s); /// assert_eq!(short_code.unwrap(), 4091296);
/// ``` /// ```
pub fn from_raw_code_val(raw_code: u64) -> Result<ShortCode, &'static str> { pub fn from_raw_code_val(raw_code: u64) -> Result<ShortCode, &'static str> {
let raw_code = RawCode::from(raw_code)?; let raw_code = RawCode::from(raw_code)?;
@ -106,13 +107,13 @@ impl ShortCode {
} }
/// Build ShortCode from CommonCode. /// Build ShortCode from CommonCode.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::{ShortCode, CommonCode}; /// use klotski_ffi::{ShortCode, CommonCode};
/// ///
/// let s = ShortCode::from_common_code(&CommonCode::from(0x1_A9BF_0C00).unwrap()); /// let short_code = ShortCode::from_common_code(&CommonCode::from(0x1_A9BF_0C00).unwrap());
/// ///
/// println!("result: {}", s); /// assert_eq!(short_code.unwrap(), 4091296);
/// ``` /// ```
#[inline] #[inline]
pub fn from_common_code(common_code: &CommonCode) -> ShortCode { pub fn from_common_code(common_code: &CommonCode) -> ShortCode {
@ -120,13 +121,13 @@ impl ShortCode {
} }
/// Build ShortCode from raw `u64` type common code. /// Build ShortCode from raw `u64` type common code.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::ShortCode; /// use klotski_ffi::ShortCode;
/// ///
/// let s = ShortCode::from_common_code_val(0x1_A9BF_0C00).unwrap(); /// let short_code = ShortCode::from_common_code_val(0x1_A9BF_0C00).unwrap();
/// ///
/// println!("result: {}", s); /// assert_eq!(short_code.unwrap(), 4091296);
/// ``` /// ```
pub fn from_common_code_val(common_code: u64) -> Result<ShortCode, &'static str> { pub fn from_common_code_val(common_code: u64) -> Result<ShortCode, &'static str> {
let common_code = CommonCode::from(common_code)?; let common_code = CommonCode::from(common_code)?;
@ -134,13 +135,13 @@ impl ShortCode {
} }
/// Build ShortCode from raw `&str` type common code. /// Build ShortCode from raw `&str` type common code.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::ShortCode; /// use klotski_ffi::ShortCode;
/// ///
/// let s = ShortCode::from_common_code_str("1A9BF0C00").unwrap(); /// let short_code = ShortCode::from_common_code_str("1A9BF0C00").unwrap();
/// ///
/// println!("result: {}", s); /// assert_eq!(short_code.unwrap(), 4091296);
/// ``` /// ```
pub fn from_common_code_str(common_code: &str) -> Result<ShortCode, &'static str> { pub fn from_common_code_str(common_code: &str) -> Result<ShortCode, &'static str> {
let common_code = CommonCode::from_str(common_code)?; let common_code = CommonCode::from_str(common_code)?;
@ -150,12 +151,13 @@ impl ShortCode {
impl ShortCode { impl ShortCode {
/// Return the original `u32` type short code value. /// Return the original `u32` type short code value.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::ShortCode; /// use klotski_ffi::ShortCode;
/// ///
/// let code = ShortCode::from_str("4WVE1").expect("invalid short code"); /// let short_code = ShortCode::from_str("4WVE1").expect("invalid short code");
/// println!("original: {}", code.unwrap()); ///
/// assert_eq!(short_code.unwrap(), 4091296);
/// ``` /// ```
#[inline] #[inline]
pub fn unwrap(&self) -> u32 { pub fn unwrap(&self) -> u32 {
@ -163,12 +165,13 @@ impl ShortCode {
} }
/// Returns the short code encoded as a string. /// Returns the short code encoded as a string.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::ShortCode; /// use klotski_ffi::ShortCode;
/// ///
/// let code = ShortCode::from(4091296).expect("invalid short code"); /// let short_code = ShortCode::from(4091296).expect("invalid short code");
/// println!("short code string: {}", code.to_string()); ///
/// assert_eq!(short_code.to_string(), "4WVE1");
/// ``` /// ```
#[inline] #[inline]
pub fn to_string(&self) -> String { pub fn to_string(&self) -> String {
@ -177,13 +180,13 @@ impl ShortCode {
/// Convert ShortCode to RawCode type, note that it will take a long time if there /// Convert ShortCode to RawCode type, note that it will take a long time if there
/// is no warm-up index. /// is no warm-up index.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::ShortCode; /// use klotski_ffi::ShortCode;
/// ///
/// let short_code = ShortCode::from_str("4WVE1").unwrap(); /// let short_code = ShortCode::from_str("4WVE1").unwrap();
/// let raw_code = short_code.to_raw_code(); ///
/// println!("{} => {}", short_code, raw_code); /// assert_eq!(short_code.to_raw_code().unwrap(), 0x0_603_EDF_5CA_FFF_5E2);
/// ``` /// ```
#[inline] #[inline]
pub fn to_raw_code(&self) -> RawCode { pub fn to_raw_code(&self) -> RawCode {
@ -194,13 +197,13 @@ impl ShortCode {
/// Convert ShortCode to CommonCode type, note that it will take a long time if there /// Convert ShortCode to CommonCode type, note that it will take a long time if there
/// is no warm-up index. /// is no warm-up index.
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::ShortCode; /// use klotski_ffi::ShortCode;
/// ///
/// let short_code = ShortCode::from_str("4WVE1").unwrap(); /// let short_code = ShortCode::from_str("4WVE1").unwrap();
/// let common_code = short_code.to_common_code(); ///
/// println!("{} => {}", short_code, common_code); /// assert_eq!(short_code.to_common_code().unwrap(), 0x1_A9BF_0C00);
/// ``` /// ```
#[inline] #[inline]
pub fn to_common_code(&self) -> CommonCode { pub fn to_common_code(&self) -> CommonCode {
@ -228,7 +231,7 @@ impl ShortCode {
/// ///
/// If you are already in fast mode, it will have no effect. /// If you are already in fast mode, it will have no effect.
/// ///
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::ShortCode; /// use klotski_ffi::ShortCode;
/// ///
@ -247,7 +250,7 @@ impl ShortCode {
/// ///
/// If you are already in normal mode, it will reduce part of time. /// If you are already in normal mode, it will reduce part of time.
/// ///
/// # Example /// # Examples
/// ``` /// ```
/// use klotski_ffi::ShortCode; /// use klotski_ffi::ShortCode;
/// ///

7
src/rust_ffi/src/metadata.rs

@ -17,6 +17,13 @@ pub struct Metadata {
pub version_patch: u32, pub version_patch: u32,
} }
/// Get klotski core metadata information.
/// # Examples
/// ```
/// use klotski_ffi::metadata;
///
/// println!("{:#?}", metadata());
/// ```
pub fn load_metadata() -> Metadata { pub fn load_metadata() -> Metadata {
unsafe { unsafe {
Metadata { Metadata {

Loading…
Cancel
Save