Browse Source

feat: more conversion interfaces for rust codec

master
Dnomd343 2 years ago
parent
commit
7df1dea0f2
  1. 71
      src/rust_ffi/src/codec/common_code.rs
  2. 87
      src/rust_ffi/src/codec/raw_code.rs
  3. 71
      src/rust_ffi/src/codec/short_code.rs

71
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<CommonCode, &'static str> {
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<CommonCode, &'static str> {
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<CommonCode, &'static str> {
let short_code = ShortCode::from_str(short_code)?;
Ok(short_code.to_common_code())
}
}
impl CommonCode {

87
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<RawCode, &'static str> {
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<RawCode, &'static str> {
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<RawCode, &'static str> {
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<RawCode, &'static str> {
let common_code = CommonCode::from_str(common_code)?;
Ok(common_code.to_raw_code())
}
}
impl RawCode {

71
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<ShortCode, &'static str> {
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<ShortCode, &'static str> {
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<ShortCode, &'static str> {
let common_code = CommonCode::from_str(common_code)?;
Ok(common_code.to_short_code())
}
}
impl ShortCode {

Loading…
Cancel
Save