From 8896e4968a2edb38aac128917ddf7aa45a972ec3 Mon Sep 17 00:00:00 2001 From: rasul Date: Sun, 5 Apr 2020 12:08:39 -0500 Subject: [PATCH 01/10] remove old commented out version of a macro --- src/macros.rs | 11 ----------- 1 file changed, 11 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 08df41d..6735cb0 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -99,14 +99,3 @@ macro_rules! try_option_send_error { } }; } - -// #[macro_export] -// macro_rules! try_option_send_error { -// ($i:ident, $e:expr) => { -// if let Ok(Some(r)) = $e { -// r -// } else { -// return SendQueue::error($i); -// } -// }; -// } From fe317c0ca547658aec321aee2ed0d347e2f65a18 Mon Sep 17 00:00:00 2001 From: rasul Date: Sun, 5 Apr 2020 12:39:10 -0500 Subject: [PATCH 02/10] document try_log!() macro --- src/macros.rs | 25 +++++++++++++++++++++++-- 1 file changed, 23 insertions(+), 2 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 6735cb0..5cf3c4c 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -1,4 +1,24 @@ -/// Unwrap a Result, and log the error. +/// Unwrap a Result. If it's Err then log the error and provided format string +/// and return. +/// +/// # Arguments +/// +/// * `$e:expr` : Expression to evaluate, must return `Result>` +/// * `$($arg:tt)*` : Format string and parameters (like `println!()`) +/// +/// # Example +/// +/// ``` +/// use std::fs::File; +/// use rude::result::RudeResult; +/// use rude::try_log; +/// +/// fn main() -> RudeResult<()> { +/// let path = "README.md"; +/// let readme = try_log!(File::open(path), "Unable to open {}", path); +/// Ok(()) +/// } +/// ``` #[macro_export] macro_rules! try_log { ($e:expr, $($arg:tt)*) => { @@ -13,7 +33,8 @@ macro_rules! try_log { }; } -/// Unwrap a Result and print if it's an error +/// Unwrap a Result, if it's Err then print the error and the provided format +/// string and return. #[macro_export] macro_rules! try_print { ($e:expr, $($arg:tt)*) => { From 632d5dfcbe568692729f677e834b07dc542c1542 Mon Sep 17 00:00:00 2001 From: rasul Date: Sun, 5 Apr 2020 12:52:30 -0500 Subject: [PATCH 03/10] document try_print!() macro --- src/macros.rs | 22 ++++++++++++++++++++-- 1 file changed, 20 insertions(+), 2 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 5cf3c4c..875efac 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -33,8 +33,26 @@ macro_rules! try_log { }; } -/// Unwrap a Result, if it's Err then print the error and the provided format -/// string and return. +/// Unwrap a Result. If it's Err then print the error and provided format string +/// and return. +/// +/// # Arguments +/// +/// * `$e:expr` : Expression to evaluate, must return `Result>` +/// * `$($arg:tt)*` : Format string and parameters (like `println!()`) +/// +/// # Example +/// +/// ``` +/// use std::fs::File; +/// use rude::result::RudeResult; +/// use rude::try_print; +/// +/// fn main() -> RudeResult<()> { +/// let readme = try_print!(File::open(file!()), "Unable to open {}", file!()); +/// Ok(()) +/// } +/// ``` #[macro_export] macro_rules! try_print { ($e:expr, $($arg:tt)*) => { From 3619302f34136028159dd1ce20604d826dc81fb9 Mon Sep 17 00:00:00 2001 From: rasul Date: Sun, 5 Apr 2020 12:53:28 -0500 Subject: [PATCH 04/10] use file!() instead of README.md --- src/macros.rs | 5 ++--- 1 file changed, 2 insertions(+), 3 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 875efac..cd3ccf0 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -14,8 +14,7 @@ /// use rude::try_log; /// /// fn main() -> RudeResult<()> { -/// let path = "README.md"; -/// let readme = try_log!(File::open(path), "Unable to open {}", path); +/// let file = try_log!(File::open(file!()), "Unable to open {}", file!()); /// Ok(()) /// } /// ``` @@ -49,7 +48,7 @@ macro_rules! try_log { /// use rude::try_print; /// /// fn main() -> RudeResult<()> { -/// let readme = try_print!(File::open(file!()), "Unable to open {}", file!()); +/// let file = try_print!(File::open(file!()), "Unable to open {}", file!()); /// Ok(()) /// } /// ``` From 4d526f9df991f0f98ce3602743f3517f2d2ffc81 Mon Sep 17 00:00:00 2001 From: rasul Date: Sun, 5 Apr 2020 13:01:06 -0500 Subject: [PATCH 05/10] document the try_send_error!() macro --- src/macros.rs | 23 ++++++++++++++++++++++- 1 file changed, 22 insertions(+), 1 deletion(-) diff --git a/src/macros.rs b/src/macros.rs index cd3ccf0..7a91e86 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -66,7 +66,28 @@ macro_rules! try_print { }; } -/// Unwrap a `Result`, if it's an error then let client know and return. +/// Unwrap a Result. If it's Err then print the error and return a generic +/// error message for the client. +/// +/// # Arguments +/// +/// * `$i:ident` : mio::Token to designate the client +/// * `$e:expr` : Expression to evaluate, must return `Result>` +/// +/// # Example +/// +/// ``` +/// use std::fs::File; +/// use mio::Token; +/// use rude::result::RudeResult; +/// use rude::try_send_error; +/// +/// fn main() -> RudeResult<()> { +/// let token = Token(12); +/// let file = try_send_error!(token, File::open(file!())); +/// Ok(()) +/// } +/// ``` #[macro_export] macro_rules! try_send_error { ($i:ident, $e:expr) => { From fa848985fb90000cb3f99cf4b859c9c2b3da3021 Mon Sep 17 00:00:00 2001 From: rasul Date: Sun, 5 Apr 2020 13:02:07 -0500 Subject: [PATCH 06/10] use full path to SendQueue --- src/macros.rs | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/macros.rs b/src/macros.rs index 7a91e86..e7b6f3d 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -100,7 +100,7 @@ macro_rules! try_send_error { line!(), e ); - return SendQueue::error($i); + return $crate::queue::SendQueue::error($i); } } }; From aa9c13444d4bb52f8bb21deadce19195320c8508 Mon Sep 17 00:00:00 2001 From: rasul Date: Sun, 5 Apr 2020 13:15:16 -0500 Subject: [PATCH 07/10] don't use main() --- src/macros.rs | 8 +++++--- 1 file changed, 5 insertions(+), 3 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index e7b6f3d..d87a707 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -79,14 +79,16 @@ macro_rules! try_print { /// ``` /// use std::fs::File; /// use mio::Token; -/// use rude::result::RudeResult; +/// use rude::queue::SendQueue; /// use rude::try_send_error; /// -/// fn main() -> RudeResult<()> { +/// fn check_file() -> SendQueue { /// let token = Token(12); /// let file = try_send_error!(token, File::open(file!())); -/// Ok(()) +/// SendQueue::ok(token) /// } +/// +/// # assert_eq!(check_file().0[0].1, SendQueue::ok(Token(12)).0[0].1); /// ``` #[macro_export] macro_rules! try_send_error { From 01bb588a05d24ee961972856233e76abdf7ae111 Mon Sep 17 00:00:00 2001 From: rasul Date: Sun, 5 Apr 2020 13:17:00 -0500 Subject: [PATCH 08/10] use fully qualified path to SendQueue --- src/macros.rs | 16 ++++++++++------ 1 file changed, 10 insertions(+), 6 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index d87a707..5cccbc5 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -115,9 +115,11 @@ macro_rules! try_send { match $e { Ok(r) => r, Err(_e) => { - return SendQueue(vec![($i, std::fmt::format(format_args!($($arg)*)), true)].into()); - } + return $crate::queue::SendQueue( + vec![($i, std::fmt::format(format_args!($($arg)*)), true)].into() + ); } + } }; } @@ -128,9 +130,11 @@ macro_rules! option_send { match $e { Some(r) => r, None => { - return SendQueue(vec![($i, std::fmt::format(format_args!($($arg)*)), true)].into()); - } + return $crate::queue::SendQueue( + vec![($i, std::fmt::format(format_args!($($arg)*)), true)].into() + ); } + } }; } @@ -146,7 +150,7 @@ macro_rules! try_option_send_error { file!(), line!() ); - return SendQueue::error($i); + return $crate::queue::SendQueue::error($i); } Err(e) => { log::error!( @@ -155,7 +159,7 @@ macro_rules! try_option_send_error { line!(), e ); - return SendQueue::error($i); + return $crate::queue::SendQueue::error($i); } } }; From ab2522249e4df582c24be26f3f6e940d9cf0d8b8 Mon Sep 17 00:00:00 2001 From: rasul Date: Sun, 5 Apr 2020 13:27:44 -0500 Subject: [PATCH 09/10] document the try_send!() macro --- src/macros.rs | 25 ++++++++++++++++++++++++- 1 file changed, 24 insertions(+), 1 deletion(-) diff --git a/src/macros.rs b/src/macros.rs index 5cccbc5..8ee092a 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -108,7 +108,30 @@ macro_rules! try_send_error { }; } -/// Unwrap a `Result`, send a message if it's E. +/// Unwrap a `Result` and return a custom message to send to the client for Err. +/// +/// # Arguments +/// +/// * `$i:ident` : `mio::Token` client identifier +/// * `$e:expr` : Expression to evaluate, must return `Result>` +/// * `$($arg:tt)*` : Format string and parameters (like `println!()`) +/// +/// # Example +/// +/// ``` +/// use std::fs::File; +/// use mio::Token; +/// use rude::queue::SendQueue; +/// use rude::try_send; +/// +/// fn check_file() -> SendQueue { +/// let token = Token(12); +/// let file = try_send!(token, File::open(file!()), "Unable to open {}", file!()); +/// SendQueue::ok(token) +/// } +/// +/// # assert_eq!(check_file().0[0].1, SendQueue::ok(Token(12)).0[0].1); +/// ``` #[macro_export] macro_rules! try_send { ($i:ident, $e:expr, $($arg:tt)*) => { From d8e3b21143cc8242f48a2e476acd2c73105bafdf Mon Sep 17 00:00:00 2001 From: rasul Date: Sun, 5 Apr 2020 14:57:17 -0500 Subject: [PATCH 10/10] document try_option_send_error!() macro --- src/macros.rs | 51 +++++++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 49 insertions(+), 2 deletions(-) diff --git a/src/macros.rs b/src/macros.rs index 8ee092a..9677179 100644 --- a/src/macros.rs +++ b/src/macros.rs @@ -146,7 +146,30 @@ macro_rules! try_send { }; } -/// Unwrap a `Option`, send a message if it's None. +/// Unarap an `Option` and return a custom message for the client if it is None. +/// +/// # Arguments +/// +/// * `$i:ident` : `mio::Token` client identifier +/// * `$e:expr` : Expression to evaluate, must return `Option` +/// * `$($arg:tt)*` : Format string and parameters (like `println!()`) +/// +/// # Example +/// +/// ``` +/// use mio::Token; +/// use rude::queue::SendQueue; +/// use rude::option_send; +/// +/// fn check_file() -> SendQueue { +/// let token = Token(12); +/// let option = Some(4); +/// let send = option_send!(token, option, "Option unwrapped to None"); +/// SendQueue::ok(token) +/// } +/// +/// # assert_eq!(check_file().0[0].1, SendQueue::ok(Token(12)).0[0].1); +/// ``` #[macro_export] macro_rules! option_send { ($i:ident, $e:expr, $($arg:tt)*) => { @@ -161,7 +184,31 @@ macro_rules! option_send { }; } -/// Unwrap a `Result, E>`, log message if it's error or none +/// Unwrap a `Result, E>` and for Err or None, log a message and send +/// a generic error message to the client. +/// +/// # Arguments +/// +/// * `$i:ident` : `mio::Token` client identifier +/// * `$e:expr` : Expression to evaluate, must return `Result>` +/// +/// # Example +/// +/// ``` +/// use std::error::Error; +/// use mio::Token; +/// use rude::queue::SendQueue; +/// use rude::try_option_send_error; +/// +/// fn tor() -> SendQueue { +/// let token = Token(12); +/// let tor: Result, Box> = Ok(Some(42)); +/// let file = try_option_send_error!(token, tor); +/// SendQueue::ok(token) +/// } +/// +/// # assert_eq!(tor().0[0].1, SendQueue::ok(Token(12)).0[0].1); +/// ``` #[macro_export] macro_rules! try_option_send_error { ($i:ident, $e:expr) => {