Advertisement
Guest User

Untitled

a guest
Oct 21st, 2019
94
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.20 KB | None | 0 0
  1. #![allow(unused)]
  2.  
  3. use std::fmt;
  4.  
  5. macro_rules! add_rust_log{
  6. (struct $struct_name:ident { $custom_field_name:ident: &'static str, $($field_name:ident: $field_type:ty),* }) => {
  7. struct $struct_name{
  8. $custom_field_name: &'static str,
  9. $($field_name: $field_type),*
  10. }
  11.  
  12. impl $struct_name{
  13. fn set_rust_log(&self){
  14. let fields : String = [$(format!("{}={},", stringify!($field_name), self.$field_name)),*]
  15. .iter()
  16. .fold("".to_string(), |accumulator, current_field| format!("{}{}", accumulator, current_field));
  17. std::env::set_var("RUST_LOG", format!("{},{}", self.$custom_field_name, &fields[0 .. fields.len() - 1]));
  18. }
  19. }
  20. }
  21. }
  22.  
  23. add_rust_log!{
  24. struct Coordinates{
  25. default_entry: &'static str,
  26. x: i32,
  27. y: i32
  28. }
  29. }
  30.  
  31. #[test]
  32. fn when_parsing_coordinates_then_rust_log_should_be_populated_correctly(){
  33. let a = Coordinates{ default_entry: "error", x: 2, y: 4 };
  34. a.set_rust_log();
  35. match std::env::var("RUST_LOG"){
  36. Ok(rust_log) => assert_eq!(rust_log, "error,x=2,y=4"),
  37. Err(_) => panic!("Something bad happened")
  38. }
  39. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement