From c17da77a93369518863296e9232c464124c42d79 Mon Sep 17 00:00:00 2001 From: fengyang Date: Sat, 10 Dec 2022 00:20:19 +0800 Subject: [PATCH 1/2] =?UTF-8?q?Status=20=E6=9E=84=E9=80=A0=E6=96=B9?= =?UTF-8?q?=E6=B3=95=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/util/mod.rs | 3 +- src/util/status.rs | 157 ++++++++++++++++++++++++++++++++++++++++++++- 2 files changed, 157 insertions(+), 3 deletions(-) diff --git a/src/util/mod.rs b/src/util/mod.rs index fe7c884..3be3ed1 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -2,4 +2,5 @@ pub mod slice; pub mod status; mod slice_test; pub mod coding; -mod coding_test; \ No newline at end of file +mod coding_test; +mod status_test; \ No newline at end of file diff --git a/src/util/status.rs b/src/util/status.rs index ae9e509..968bb88 100644 --- a/src/util/status.rs +++ b/src/util/status.rs @@ -1,4 +1,157 @@ +/// 使用 Status 这个类来得到你的函数的返回的状态 +/// +use std::borrow::{Borrow, Cow}; +use std::io::Write; +use std::ptr; +use std::ptr::{NonNull, null}; +use crate::util::slice::Slice; +use crate::util::status::Code::kOk; -pub enum Status { +#[derive(Debug)] +pub struct Status { + /// state_[0, 3] 字节代表消息的长度,这个长度是从 state_[5, ...] 开始的,前面的 5 个字节不算。 + /// state_[4] 字节代表消息的类型,就是上面介绍的 enum Code 的 6 种类型。 + /// state_[5, ...] 代表实际的消息体。 -} \ No newline at end of file + /// OK status has a null state_. Otherwise, state_ is a new[] array + /// of the following form: + /// state_[0..3] == length of message + /// state_[4] == code + /// state_[5..] == message + pub state_: String, +} + +impl Default for Status { + fn default() -> Self { + unsafe { + Self { + state_: String::new() + } + } + } +} + +impl Status { + /// 构造一个 Status + fn off(state: String) -> Status { + unsafe { + Status { + state_: state + } + } + } + + /// 构造一个 Status + pub fn of(code: Code, msg: Slice, msg2:Slice) -> Status { + let len2: usize = msg2.len(); + let msgSize: usize = msg.len() + (2 + len2); + + let mut vecStatus: Vec = Vec::with_capacity(msgSize); + + let msgSizeV: u32 = msgSize.try_into().unwrap(); + + // 0,1,2,3 + // unsafe { + // vecStatus.as_mut_ptr().cast::().offset(0) + // .write(msgSizeV); + // } + vecStatus.push(msgSizeV as u8); + vecStatus.push((msgSizeV >> 8) as u8); + vecStatus.push((msgSizeV >> 16) as u8); + vecStatus.push((msgSizeV >> 24) as u8); + + // 4 + vecStatus.push(code as u8); + + // data 1 + vecStatus.write(msg.as_ref()); + + if len2 > 0 { + vecStatus.write(":".as_bytes()); + vecStatus.write(" ".as_bytes()); + vecStatus.write(msg2.as_ref()); + } + + unsafe { + return Status::off(String::from_utf8_unchecked(vecStatus)); + } + } + +// fn code(&self) -> Code { +// if self.state_ == nullptr +// return Code::kOk; +// +// +// return () ? : self.state_[4]; +// } +// +// /// Returns true iff the status indicates success. +// pub fn ok(&self) -> bool { +// self.state_ == NonNull +// // let code: Cow = Slice::borrow_data(&Slice::from(String::from("123").into())); +// // true +// } +// +// /// Returns true iff the status indicates a NotFound error. +// pub fn is_not_found(&self) -> bool { +// true +// } +// +// /// Returns true iff the status indicates a Corruption error. +// pub fn is_corruption(&self) -> bool { +// true +// } +// +// /// Returns true iff the status indicates an IOError. +// pub fn is_io_error(&self) -> bool { +// true +// } +// +// /// Returns true iff the status indicates a NotSupportedError. +// pub fn is_not_supported_error(&self) -> bool { +// true +// } +// +// /// Returns true iff the status indicates an InvalidArgument. +// pub fn is_invalid_argument(&self) -> bool { +// true +// } +// +// /// Return a string representation of this status suitable for printing. +// /// Returns the string "OK" for success. +// pub fn to_string(&self) -> String { +// format!("{}", "") +// } +} + +impl<'a> Status { + /// 返回 ok的 Status + pub fn ok(&self) -> Status { + Status::default() + } + + // /// 返回 ok的 Status + // pub fn not_found(msg: Slice) -> Status { + // Status::default() + // } + // + // /// 借取 Slice 中的数据, 调用方只拥有读权限 + // pub fn copy_state(&self) -> Cow<'a, String> { + // let size : u32; + // let str = unsafe { + // String::from_raw_parts(self.data.as_ptr(), self.len, self.len) + // }; + // Cow::Owned(str) + // } +} + +/// Status 的状态 +#[derive(Debug, PartialEq)] +pub enum Code { + kOk = 0, + kNotFound = 1, + kCorruption = 2, + kNotSupported = 3, + kInvalidArgument = 4, + kIOError = 5, +} -- Gitee From b02863bcf061bf7942583f7dc1b9cc38d9907d7c Mon Sep 17 00:00:00 2001 From: fengyang Date: Mon, 12 Dec 2022 23:25:14 +0800 Subject: [PATCH 2/2] =?UTF-8?q?status=20=E5=AE=9E=E7=8E=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/util/mod.rs | 11 +- src/util/status.rs | 283 ++++++++++++++++++++++------------------ src/util/status_test.rs | 48 +++++++ 3 files changed, 213 insertions(+), 129 deletions(-) create mode 100644 src/util/status_test.rs diff --git a/src/util/mod.rs b/src/util/mod.rs index fcbc066..d2fb80b 100644 --- a/src/util/mod.rs +++ b/src/util/mod.rs @@ -1,5 +1,7 @@ +use crate::util::status::LevelError; +use std::result; + pub mod slice; -pub mod status; mod slice_test; pub mod coding; mod coding_test; @@ -7,4 +9,9 @@ pub mod arena; mod arena_test; pub use arena::Arena; -mod status_test; \ No newline at end of file + +pub mod status; +mod status_test; + +/// 定义别名 +pub type Result = result::Result; diff --git a/src/util/status.rs b/src/util/status.rs index 968bb88..c576126 100644 --- a/src/util/status.rs +++ b/src/util/status.rs @@ -1,157 +1,186 @@ /// 使用 Status 这个类来得到你的函数的返回的状态 /// use std::borrow::{Borrow, Cow}; -use std::io::Write; +use std::fmt::Debug; use std::ptr; use std::ptr::{NonNull, null}; use crate::util::slice::Slice; -use crate::util::status::Code::kOk; - -#[derive(Debug)] -pub struct Status { - /// state_[0, 3] 字节代表消息的长度,这个长度是从 state_[5, ...] 开始的,前面的 5 个字节不算。 - /// state_[4] 字节代表消息的类型,就是上面介绍的 enum Code 的 6 种类型。 - /// state_[5, ...] 代表实际的消息体。 - - /// OK status has a null state_. Otherwise, state_ is a new[] array - /// of the following form: - /// state_[0..3] == length of message - /// state_[4] == code - /// state_[5..] == message - pub state_: String, +use crate::util::status::LevelError::{kCorruption, kInvalidArgument, kIOError, kNotFound, kNotSupported, kOk}; + +/// Status 的状态 +pub enum LevelError { + kOk, + kNotFound(Option), + kCorruption(Option), + kNotSupported(Option), + kInvalidArgument(Option), + kIOError(Option), + } -impl Default for Status { +impl Default for LevelError { fn default() -> Self { - unsafe { - Self { - state_: String::new() - } - } + kOk } } -impl Status { - /// 构造一个 Status - fn off(state: String) -> Status { - unsafe { - Status { - state_: state - } +impl LevelError { + pub fn get_code(&self) -> u32 { + match self { + kOk => {0}, + kNotFound(_) => {1}, + kCorruption(_) => {2}, + kNotSupported(_) => {3}, + kInvalidArgument(_) => {4}, + kIOError(_) => {5}, } } - /// 构造一个 Status - pub fn of(code: Code, msg: Slice, msg2:Slice) -> Status { - let len2: usize = msg2.len(); - let msgSize: usize = msg.len() + (2 + len2); + /// 得到 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 + }, + } + } - let mut vecStatus: Vec = Vec::with_capacity(msgSize); + /// 判断 状态是否为默认值 + fn is_default(&self) -> bool { + self.ok() + } - let msgSizeV: u32 = msgSize.try_into().unwrap(); + /// Returns true iff the status indicates success. + pub fn ok(&self) -> bool { + match self { + kOk => true, + _ => false + } + } - // 0,1,2,3 - // unsafe { - // vecStatus.as_mut_ptr().cast::().offset(0) - // .write(msgSizeV); - // } - vecStatus.push(msgSizeV as u8); - vecStatus.push((msgSizeV >> 8) as u8); - vecStatus.push((msgSizeV >> 16) as u8); - vecStatus.push((msgSizeV >> 24) as u8); + /// Returns true iff the status indicates a NotFound error. + pub fn is_not_found(&self) -> bool { + match self { + kNotFound(_) => true, + _ => false + } + } - // 4 - vecStatus.push(code as u8); + /// Returns true iff the status indicates a Corruption error. + pub fn is_corruption(&self) -> bool { + match self { + kCorruption(_) => true, + _ => false + } + } - // data 1 - vecStatus.write(msg.as_ref()); + /// Returns true iff the status indicates an IOError. + pub fn is_io_error(&self) -> bool { + match self { + kIOError(_) => true, + _ => false + } + } - if len2 > 0 { - vecStatus.write(":".as_bytes()); - vecStatus.write(" ".as_bytes()); - vecStatus.write(msg2.as_ref()); + /// Returns true iff the status indicates a NotSupportedError. + pub fn is_not_supported_error(&self) -> bool { + match self { + kNotSupported(_) => true, + _ => false } + } - unsafe { - return Status::off(String::from_utf8_unchecked(vecStatus)); + /// Returns true iff the status indicates an InvalidArgument. + pub fn is_invalid_argument(&self) -> bool { + match self { + kInvalidArgument(_) => true, + _ => false } } -// fn code(&self) -> Code { -// if self.state_ == nullptr -// return Code::kOk; -// -// -// return () ? : self.state_[4]; -// } -// -// /// Returns true iff the status indicates success. -// pub fn ok(&self) -> bool { -// self.state_ == NonNull -// // let code: Cow = Slice::borrow_data(&Slice::from(String::from("123").into())); -// // true -// } -// -// /// Returns true iff the status indicates a NotFound error. -// pub fn is_not_found(&self) -> bool { -// true -// } -// -// /// Returns true iff the status indicates a Corruption error. -// pub fn is_corruption(&self) -> bool { -// true -// } -// -// /// Returns true iff the status indicates an IOError. -// pub fn is_io_error(&self) -> bool { -// true -// } -// -// /// Returns true iff the status indicates a NotSupportedError. -// pub fn is_not_supported_error(&self) -> bool { -// true -// } -// -// /// Returns true iff the status indicates an InvalidArgument. -// pub fn is_invalid_argument(&self) -> bool { -// true -// } -// -// /// Return a string representation of this status suitable for printing. -// /// Returns the string "OK" for success. -// pub fn to_string(&self) -> String { -// format!("{}", "") -// } -} + /// 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 -impl<'a> Status { - /// 返回 ok的 Status - pub fn ok(&self) -> Status { - Status::default() - } - - // /// 返回 ok的 Status - // pub fn not_found(msg: Slice) -> Status { - // Status::default() - // } - // - // /// 借取 Slice 中的数据, 调用方只拥有读权限 - // pub fn copy_state(&self) -> Cow<'a, String> { - // let size : u32; - // let str = unsafe { - // String::from_raw_parts(self.data.as_ptr(), self.len, self.len) - // }; - // Cow::Owned(str) - // } + String::from(_type) + } } -/// Status 的状态 -#[derive(Debug, PartialEq)] -pub enum Code { - kOk = 0, - kNotFound = 1, - kCorruption = 2, - kNotSupported = 3, - kInvalidArgument = 4, - kIOError = 5, +// 这一组函数用来组合指定的状态信息 +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 0000000..c15f276 --- /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()); + // } + +} -- Gitee