|
@ -1,3 +1,5 @@ |
|
|
|
|
|
use std::fmt; |
|
|
|
|
|
use super::ShortCode; |
|
|
use super::ffi as codec_ffi; |
|
|
use super::ffi as codec_ffi; |
|
|
|
|
|
|
|
|
#[derive(Debug)] |
|
|
#[derive(Debug)] |
|
@ -5,12 +7,64 @@ pub struct CommonCode { |
|
|
code: u64 |
|
|
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 { |
|
|
pub(crate) fn new(common_code: u64) -> CommonCode { |
|
|
CommonCode { |
|
|
CommonCode { |
|
|
code: common_code |
|
|
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 |
|
|
|
|
|
} |
|
|
} |
|
|
} |
|
|