Advertisement
Guest User

Untitled

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