Browse Source

feat: CommonCode framework of rust codec

master
Dnomd343 2 years ago
parent
commit
6ac4ad11e4
  1. 56
      src/rust_ffi/src/codec/common_code.rs
  2. 30
      src/rust_ffi/src/codec/mod.rs
  3. 11
      src/rust_ffi/src/codec/short_code.rs

56
src/rust_ffi/src/codec/common_code.rs

@ -1,3 +1,5 @@
use std::fmt;
use super::ShortCode;
use super::ffi as codec_ffi;
#[derive(Debug)]
@ -5,12 +7,64 @@ pub struct CommonCode {
code: u64
}
impl CommonCode {
impl Eq for CommonCode {}
impl PartialEq for CommonCode {
fn eq(&self, other: &Self) -> bool {
self.code == other.code
}
}
impl fmt::Display for CommonCode {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
// TODO: using hex output of CommonCode
write!(f, "{}", self.code)
}
}
impl CommonCode {
pub(crate) fn new(common_code: u64) -> CommonCode {
CommonCode {
code: common_code
}
}
pub fn from(common_code: u64) -> Result<CommonCode, &'static str> {
match codec_ffi::common_code_check(common_code) {
true => Ok(CommonCode {
code: common_code
}),
_ => Err("invalid common code"),
}
}
pub fn from_str(common_code: &str) -> Result<CommonCode, &'static str> {
Ok(CommonCode {
code: codec_ffi::common_code_from_string(common_code)?
})
}
// TODO: from RawCode (u64 / RawCode)
// TODO: from ShortCode (u32 / String / CommonCode)
pub fn to_string(&self, shorten: bool) -> String {
match shorten {
true => codec_ffi::common_code_to_string_unsafe(self.code),
false => codec_ffi::common_code_to_string_shorten_unsafe(self.code),
}
}
// TODO: to_string_shorten
pub fn to_short_code(&self) -> ShortCode {
ShortCode::new(
codec_ffi::common_code_to_short_code_unsafe(self.code)
)
}
// TODO: to RawCode
pub fn unwrap(&self) -> u64 {
self.code
}
}

30
src/rust_ffi/src/codec/mod.rs

@ -2,20 +2,38 @@ mod ffi;
mod short_code;
mod common_code;
use short_code::ShortCode;
use common_code::CommonCode;
pub use short_code::ShortCode;
pub use common_code::CommonCode;
pub fn demo() {
let s = ShortCode::from(12345678).unwrap();
let s = ShortCode::from(4091296).unwrap();
println!("{}", s.unwrap());
println!("{:?}", s);
println!("{}", s);
println!("{}", s.to_string());
let s = ShortCode::from_str("CSSBF").unwrap();
println!("{}", s.unwrap());
let s = ShortCode::from_str("4WVE1").unwrap();
println!("{}", s);
println!("ShortCode warm up");
ShortCode::warm_up();
println!("ShortCode warm up fast");
ShortCode::warm_up_fast();
println!("{}", s.to_string());
println!("{:?}", s.to_common_code());
let c = CommonCode::from(0x1A9BF0C00).unwrap();
println!("{}", c.unwrap());
println!("{:?}", c);
println!("{}", c);
let c = CommonCode::from_str("1A9BF0C00").unwrap();
println!("{}", c);
let c = CommonCode::from_str("1A9BF0C").unwrap();
println!("{}", c);
println!("{}", c.to_string(false));
println!("{}", c.to_string(true));
println!("{}", c.to_short_code());
}

11
src/rust_ffi/src/codec/short_code.rs

@ -22,6 +22,12 @@ impl fmt::Display for ShortCode {
}
impl ShortCode {
pub(crate) fn new(short_code: u32) -> ShortCode {
ShortCode {
code: short_code
}
}
pub fn from(short_code: u32) -> Result<ShortCode, &'static str> {
match codec_ffi::short_code_check(short_code) {
true => Ok(ShortCode {
@ -37,12 +43,15 @@ impl ShortCode {
})
}
// TODO: from CommonCode (u32 / String / CommonCode)
// TODO: from RawCode (u64 / RawCode)
// TODO: from CommonCode (u64 / String / CommonCode)
pub fn to_string(&self) -> String {
codec_ffi::short_code_to_string_unsafe(self.code)
}
// TODO: to RawCode
pub fn to_common_code(&self) -> CommonCode {
CommonCode::new(
codec_ffi::short_code_to_common_code_unsafe(self.code)

Loading…
Cancel
Save