Browse Source

feat: ShortCode framework of rust codec

legacy
Dnomd343 2 years ago
parent
commit
e41318fb4a
  1. 3
      src/cli/src/main.rs
  2. 3
      src/rust_ffi/src/codec/mod.rs
  3. 69
      src/rust_ffi/src/codec/short_code.rs
  4. 2
      src/rust_ffi/src/lib.rs

3
src/cli/src/main.rs

@ -1,7 +1,7 @@
extern crate klotski_ffi; extern crate klotski_ffi;
fn main() { fn main() {
println!("{:#?}", klotski_ffi::metadata()); // println!("{:#?}", klotski_ffi::metadata());
// println!("{}", klotski_ffi::is_short_code_available()); // println!("{}", klotski_ffi::is_short_code_available());
// println!("{}", klotski_ffi::is_short_code_available_fast()); // 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("1A9BF0C").unwrap());
// println!("{}", klotski_ffi::common_code_from_string("1A9BF0C00").unwrap()); // println!("{}", klotski_ffi::common_code_from_string("1A9BF0C00").unwrap());
klotski_ffi::demo();
} }

3
src/rust_ffi/src/codec/mod.rs

@ -1 +1,4 @@
mod ffi; mod ffi;
mod short_code;
pub use short_code::demo;

69
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<ShortCode, &'static str> {
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<ShortCode, &'static str> {
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());
}

2
src/rust_ffi/src/lib.rs

@ -3,3 +3,5 @@ mod codec;
mod metadata; mod metadata;
pub use metadata::load_metadata as metadata; pub use metadata::load_metadata as metadata;
pub use codec::demo;

Loading…
Cancel
Save