diff --git a/src/util/mod.rs b/src/util/mod.rs index 567d645f98bcb5353685d655513fbd6d8c88612d..d2fb80b901654ad86738f0da8b90bb5f116bbffe 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,9 +1,17 @@ +use crate::util::status::LevelError; +use std::result; + pub mod slice; -pub mod status; mod slice_test; pub mod coding; mod coding_test; pub mod arena; mod arena_test; -pub use arena::Arena; \ No newline at end of file +pub use arena::Arena; + +pub mod status; +mod status_test; + +/// 定义别名 +pub type Result = result::Result; diff --git a/src/util/status.rs b/src/util/status.rs index ae9e509f916cf277e9a414a6ec40278a0e76f793..c5761269edc3c970303e36002b446e32c72a35fa 100644 --- a/src/util/status.rs +++ b/src/util/status.rs @@ -1,4 +1,186 @@ +/// 使用 Status 这个类来得到你的函数的返回的状态 +/// +use std::borrow::{Borrow, Cow}; +use std::fmt::Debug; +use std::ptr; +use std::ptr::{NonNull, null}; +use crate::util::slice::Slice; +use crate::util::status::LevelError::{kCorruption, kInvalidArgument, kIOError, kNotFound, kNotSupported, kOk}; -pub enum Status { +/// Status 的状态 +pub enum LevelError { + kOk, + kNotFound(Option), + kCorruption(Option), + kNotSupported(Option), + kInvalidArgument(Option), + kIOError(Option), -} \ No newline at end of file +} + +impl Default for LevelError { + fn default() -> Self { + kOk + } +} + +impl LevelError { + pub fn get_code(&self) -> u32 { + match self { + kOk => {0}, + kNotFound(_) => {1}, + kCorruption(_) => {2}, + kNotSupported(_) => {3}, + kInvalidArgument(_) => {4}, + kIOError(_) => {5}, + } + } + + /// 得到 error 中的 slice 信息 + pub fn into_msg(self) -> Option { + match self { + kOk => {None}, + kNotFound(slice) => { + slice + }, + kCorruption(slice) => { + // println!("The slice to be {:?}", slice); + slice + }, + kNotSupported(slice) => { + // println!("The slice to be {:?}", slice); + slice + }, + kInvalidArgument(slice) => { + // println!("The slice to be {:?}", slice); + slice + }, + kIOError(slice) => { + // println!("The slice to be {:?}", slice); + slice + }, + } + } + + /// 判断 状态是否为默认值 + fn is_default(&self) -> bool { + self.ok() + } + + /// Returns true iff the status indicates success. + pub fn ok(&self) -> bool { + match self { + kOk => true, + _ => false + } + } + + /// Returns true iff the status indicates a NotFound error. + pub fn is_not_found(&self) -> bool { + match self { + kNotFound(_) => true, + _ => false + } + } + + /// Returns true iff the status indicates a Corruption error. + pub fn is_corruption(&self) -> bool { + match self { + kCorruption(_) => true, + _ => false + } + } + + /// Returns true iff the status indicates an IOError. + pub fn is_io_error(&self) -> bool { + match self { + kIOError(_) => true, + _ => false + } + } + + /// Returns true iff the status indicates a NotSupportedError. + pub fn is_not_supported_error(&self) -> bool { + match self { + kNotSupported(_) => true, + _ => false + } + } + + /// Returns true iff the status indicates an InvalidArgument. + pub fn is_invalid_argument(&self) -> bool { + match self { + kInvalidArgument(_) => true, + _ => false + } + } + + /// Return a string representation of this status suitable for printing. + /// Returns the string "OK" for success. + pub fn to_string(self) -> String { + if self.is_default() { + return String::from("OK") + } + + let _tmp:Vec = Vec::with_capacity(30); + let mut _type: &str = ""; + + match self { + kOk => { + _type = "OK"; + }, + kNotFound(_) => { + _type = "NotFound: "; + }, + kCorruption(_) => { + _type = "Corruption: "; + }, + kNotSupported(_) => { + _type = "Not implemented: "; + }, + kInvalidArgument(_) => { + _type = "Invalid argument: "; + }, + kIOError(_) => { + _type = "IO error: "; + } + } + + // todo + + String::from(_type) + } +} + +// 这一组函数用来组合指定的状态信息 +impl<'a> LevelError { + /// 返回 ok 的 Status + pub fn OK() -> LevelError { + kOk + } + + /// 返回 not_found 的 Status + pub fn not_found(msg: Slice, msg2: Slice) -> LevelError { + kNotFound(Some(msg)) + } + + /// 返回 Corruption 的 Status + pub fn corruption(msg: Slice, msg2: Slice) -> LevelError { + kCorruption(Some(msg)) + } + + /// 返回 NotSupported 的 Status + pub fn not_supportedfound(msg: Slice, msg2: Slice) -> LevelError { + LevelError::kNotSupported(Some(msg)) + } + + /// 返回 InvalidArgument 的 Status + pub fn invalid_argument(msg: Slice, msg2: Slice) -> LevelError { + LevelError::kInvalidArgument(Some(msg)) + } + + /// 返回 IOError 的 Status + pub fn io_error(msg: Slice, msg2: Slice) -> LevelError { + LevelError::kIOError(Some(msg)) + } +} diff --git a/src/util/status_test.rs b/src/util/status_test.rs new file mode 100644 index 0000000000000000000000000000000000000000..c15f2769bbd68756b5d7b03af64dfc3a1be65190 --- /dev/null +++ b/src/util/status_test.rs @@ -0,0 +1,48 @@ + +mod test { + use crate::util::slice::Slice; + use crate::util::status::LevelError; + + #[test] + fn test_of() { + let msg1 = "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc"; + let msg2 = "456456456456456456456456456456456456456456456456"; + + let err: LevelError = LevelError::io_error(String::from(msg1).into(), + String::from(msg2).into()); + let slice: Option = err.into_msg(); + assert_eq!("abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc", + String::from(slice.unwrap())); + + let err: LevelError = LevelError::OK(); + let slice: Option = err.into_msg(); + assert!(Option::None == slice); + } + + // #[test] + // fn test_toString() { + // // ok + // let status: LevelError = LevelError::OK(); + // assert_eq!("OK", status.to_string()); + // + // let msg1 = "abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc\ + // abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc\ + // abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc\ + // abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc\ + // abcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabcabc"; + // let msg2 = "456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456\ + // 456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456\ + // 456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456\ + // 456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456\ + // 456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456456"; + // + // let status: Status = Status::of(Code::kIOError, + // String::from(msg1).into(), + // String::from(msg2).into()); + // + // let expectString: String = String::from("".to_owned() + msg1 + ""); + // assert_eq!("IO error: 101".to_owned() + msg1 + ": " + msg2, + // status.to_string()); + // } + +}