diff --git a/src/util/status.rs b/src/util/status.rs index 051a424452e7d74e5e5e5dd84fda51eda2dcb0bf..9b534bda30f4f936a2ee7e70a7268fc016f5c52f 100644 --- a/src/util/status.rs +++ b/src/util/status.rs @@ -34,7 +34,11 @@ impl Status { /// ``` /// /// ``` - pub fn wrapper(err: LevelError, slice: Slice) -> Status { + pub fn wrapper(err: LevelError, mut slice: Slice) -> Status { + if err.is_ok() { + slice = Slice::default(); + } + Self { err, msg: slice @@ -50,6 +54,31 @@ impl Status { } } + pub fn is_ok(&self) -> bool { + self.err.is_ok() + } + + pub fn is_not_found(&self) -> bool { + self.err.is_not_found() + } + + pub fn is_corruption(&self) -> bool { + self.err.is_corruption() + } + + pub fn is_io_error(&self) -> bool { + self.err.is_io_error() + } + + pub fn is_not_supported_error(&self) -> bool { + self.err.is_not_supported_error() + } + + pub fn is_invalid_argument(&self) -> bool { + self.err.is_invalid_argument() + } + + /// 请注意, err 的所有权会发生转移!!! pub fn get_error(self) -> LevelError { self.err } @@ -93,7 +122,7 @@ impl Status { /// ``` #[inline] pub fn to_string(self) -> String { - let err = self.err; + let err = &self.err; let msg_type = match &err { KOk => "OK", @@ -126,7 +155,7 @@ pub enum LevelError { } impl LevelError { - pub fn is_ok(self) -> bool { + pub fn is_ok(&self) -> bool { match self { KOk => true, _ => false diff --git a/src/util/status_test.rs b/src/util/status_test.rs index 8b6fa1f21f2815eee1be52eae0224f3509d852f5..af745d0092f04023f07ccda66736d11eb0862202 100644 --- a/src/util/status_test.rs +++ b/src/util/status_test.rs @@ -5,15 +5,24 @@ mod test { use crate::util::status::{LevelError, Status}; #[test] - fn test_error_code() { + fn test_wraper() { let msg1 = "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc"; - let msg2 = "456456456456456456456456456456456456456456456456"; - let err: Status = LevelError::io_error(String::from(msg1).into(), String::from(msg2).into()); - assert!(&err.get_error().is_io_error()); + let status = Status::wrapper(LevelError::KIOError, String::from(msg1).into()); + assert!(&status.is_io_error()); + let slice: Option = status.into_msg(); + assert_eq!("abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc", + String::from(slice.unwrap())); + + let ss = Status::wrapper(LevelError::KOk, String::from(msg1).into()); + assert!(&ss.is_ok()); + assert_eq!("OK", &ss.to_string()); + } - let status = Status::default(); - assert!(&status.get_error().is_ok()); + #[test] + fn test_wrappers() { + let msg1 = "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc"; + let msg2 = "456456456456456456456456456456456456456456456456"; let status = Status::wrappers(LevelError::KIOError, String::from(msg1).into(), String::from(msg2).into()); let slice: Option = status.into_msg();