Skip to content

Instantly share code, notes, and snippets.

@peterschwarz
Forked from rust-play/playground.rs
Created September 12, 2018 13:58
Show Gist options
  • Save peterschwarz/2a3f9a566f9047cf619e13de04337a00 to your computer and use it in GitHub Desktop.
Save peterschwarz/2a3f9a566f9047cf619e13de04337a00 to your computer and use it in GitHub Desktop.
Code shared from the Rust Playground
#[derive(Default)]
struct X {
a: String,
b: String,
c: String,
}
macro_rules! reject_empty {
($obj:ident, $first_field:tt, $($field:tt),*) => {
{
if $obj.$first_field.is_empty() {
Err(format!("{}.{} is empty", stringify!($obj), stringify!($first_field)))
}
$(
else if $obj.$field.is_empty() {
Err(format!("{}.{} is empty", stringify!($obj), stringify!($field)))
}
)*
else {
Ok(())
}
}
};
}
fn main() {
{
let x = X::default();
println!("Checked?: {:?}", reject_empty!(x, a, b));
}
{
let x = X { a: "One".to_string(), b: "".to_string(), c: "x".to_string() };
println!("Checked?: {:?}", reject_empty!(x, a, b));
}
{
let x = X { a: "One".to_string(), b: "Two".to_string(), c: "x".to_string() };
println!("Checked?: {:?}", reject_empty!(x, a, b));
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment