|
@ -1,7 +1,6 @@ |
|
|
use std::fmt; |
|
|
use std::fmt; |
|
|
use crate::codec::raw_code::RawCode; |
|
|
|
|
|
use super::CommonCode; |
|
|
|
|
|
use super::ffi as codec_ffi; |
|
|
use super::ffi as codec_ffi; |
|
|
|
|
|
use super::{RawCode, CommonCode}; |
|
|
|
|
|
|
|
|
#[derive(Debug)] |
|
|
#[derive(Debug)] |
|
|
pub struct ShortCode { |
|
|
pub struct ShortCode { |
|
@ -17,20 +16,42 @@ impl PartialEq for ShortCode { |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
impl fmt::Display for ShortCode { |
|
|
impl fmt::Display for ShortCode { |
|
|
|
|
|
/// Output ShortCode for debug.
|
|
|
|
|
|
/// Format: XXXXX(...num...)
|
|
|
|
|
|
/// Example: 4WVE1(4091296)
|
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
|
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { |
|
|
write!(f, "{}({})", self.to_string(), self.code) |
|
|
write!(f, "{}({})", self.to_string(), self.code) |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
impl ShortCode { |
|
|
impl ShortCode { |
|
|
|
|
|
/// Check if the input `u32` value is a valid short code.
|
|
|
|
|
|
#[inline] |
|
|
|
|
|
pub(crate) fn check(short_code: u32) -> bool { |
|
|
|
|
|
codec_ffi::short_code_check(short_code) |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Creates an unchecked short code, which is not safe and is only used
|
|
|
|
|
|
/// during encoding conversion (to ensure no errors).
|
|
|
|
|
|
#[inline] |
|
|
pub(crate) fn new(short_code: u32) -> ShortCode { |
|
|
pub(crate) fn new(short_code: u32) -> ShortCode { |
|
|
ShortCode { |
|
|
ShortCode { |
|
|
code: short_code |
|
|
code: short_code |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Create short code from raw `u32`, and will be checked for validity.
|
|
|
|
|
|
/// # Example
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
/// use klotski_ffi::ShortCode;
|
|
|
|
|
|
///
|
|
|
|
|
|
/// match ShortCode::from(4091296) {
|
|
|
|
|
|
/// Ok(code) => println!("result: {}", code),
|
|
|
|
|
|
/// Err(err) => println!("error: {}", err),
|
|
|
|
|
|
/// }
|
|
|
|
|
|
/// ```
|
|
|
pub fn from(short_code: u32) -> Result<ShortCode, &'static str> { |
|
|
pub fn from(short_code: u32) -> Result<ShortCode, &'static str> { |
|
|
match codec_ffi::short_code_check(short_code) { |
|
|
match ShortCode::check(short_code) { |
|
|
true => Ok(ShortCode { |
|
|
true => Ok(ShortCode { |
|
|
code: short_code |
|
|
code: short_code |
|
|
}), |
|
|
}), |
|
@ -38,27 +59,65 @@ impl ShortCode { |
|
|
} |
|
|
} |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Create short code from raw `&str`, and will be checked for validity.
|
|
|
|
|
|
/// # Example
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
/// use klotski_ffi::ShortCode;
|
|
|
|
|
|
///
|
|
|
|
|
|
/// match ShortCode::from_str("4WVE1") {
|
|
|
|
|
|
/// Ok(code) => println!("result: {}", code),
|
|
|
|
|
|
/// Err(err) => println!("error: {}", err),
|
|
|
|
|
|
/// }
|
|
|
|
|
|
/// ```
|
|
|
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 { |
|
|
code: codec_ffi::short_code_from_string(short_code)? |
|
|
code: codec_ffi::short_code_from_string(short_code)? |
|
|
}) |
|
|
}) |
|
|
} |
|
|
} |
|
|
|
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
impl ShortCode { |
|
|
// TODO: from RawCode (u64 / RawCode)
|
|
|
// TODO: from RawCode (u64 / RawCode)
|
|
|
// TODO: from CommonCode (u64 / String / CommonCode)
|
|
|
// TODO: from CommonCode (u64 / String / CommonCode)
|
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
impl ShortCode { |
|
|
impl ShortCode { |
|
|
|
|
|
/// Return the original `u32` type short code value.
|
|
|
|
|
|
/// # Example
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
/// use klotski_ffi::ShortCode;
|
|
|
|
|
|
///
|
|
|
|
|
|
/// let code = ShortCode::from_str("4WVE1").expect("invalid short code");
|
|
|
|
|
|
/// println!("original: {}", code.unwrap());
|
|
|
|
|
|
/// ```
|
|
|
#[inline] |
|
|
#[inline] |
|
|
pub fn unwrap(&self) -> u32 { |
|
|
pub fn unwrap(&self) -> u32 { |
|
|
self.code |
|
|
self.code |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Returns the short code encoded as a string.
|
|
|
|
|
|
/// # Example
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
/// use klotski_ffi::ShortCode;
|
|
|
|
|
|
///
|
|
|
|
|
|
/// let code = ShortCode::from_str("4WVE1").expect("invalid short code");
|
|
|
|
|
|
/// println!("short code string: {}", code.to_string());
|
|
|
|
|
|
/// ```
|
|
|
#[inline] |
|
|
#[inline] |
|
|
pub fn to_string(&self) -> String { |
|
|
pub fn to_string(&self) -> String { |
|
|
codec_ffi::short_code_to_string_unsafe(self.code) |
|
|
codec_ffi::short_code_to_string_unsafe(self.code) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Convert to RawCode type, note that it will take a long time if there is no
|
|
|
|
|
|
/// warm-up index.
|
|
|
|
|
|
/// # Example
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
/// use klotski_ffi::ShortCode;
|
|
|
|
|
|
///
|
|
|
|
|
|
/// let short_code = ShortCode::from_str("4WVE1").unwrap();
|
|
|
|
|
|
/// let raw_code = short_code.to_raw_code();
|
|
|
|
|
|
/// println!("{} => {}", short_code, raw_code);
|
|
|
|
|
|
/// ```
|
|
|
#[inline] |
|
|
#[inline] |
|
|
pub fn to_raw_code(&self) -> RawCode { |
|
|
pub fn to_raw_code(&self) -> RawCode { |
|
|
RawCode::new( |
|
|
RawCode::new( |
|
@ -66,6 +125,16 @@ impl ShortCode { |
|
|
) |
|
|
) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
/// Convert to CommonCode type, note that it will take a long time if there is no
|
|
|
|
|
|
/// warm-up index.
|
|
|
|
|
|
/// # Example
|
|
|
|
|
|
/// ```
|
|
|
|
|
|
/// use klotski_ffi::ShortCode;
|
|
|
|
|
|
///
|
|
|
|
|
|
/// let short_code = ShortCode::from_str("4WVE1").unwrap();
|
|
|
|
|
|
/// let common_code = short_code.to_common_code();
|
|
|
|
|
|
/// println!("{} => {}", short_code, common_code);
|
|
|
|
|
|
/// ```
|
|
|
#[inline] |
|
|
#[inline] |
|
|
pub fn to_common_code(&self) -> CommonCode { |
|
|
pub fn to_common_code(&self) -> CommonCode { |
|
|
CommonCode::new( |
|
|
CommonCode::new( |
|
|