Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #![allow(unused)]
- use std::fmt;
- macro_rules! add_rust_log{
- (struct $struct_name:ident { $($field_name:ident: $field_type:ty),* }) => {
- struct $struct_name{
- $($field_name: $field_type),*
- }
- impl $struct_name{
- fn set_rust_log(&self){
- let fields : String = [$(format!("{}={},", stringify!($field_name), self.$field_name)),*]
- .iter()
- .fold("".to_string(), |accumulator, current_field| format!("{}{}", accumulator, current_field));
- std::env::set_var("RUST_LOG", format!("{}({})", stringify!($struct_name), fields));
- }
- }
- }
- }
- add_rust_log!{
- struct Coordinates{
- default: String,
- x: i32,
- y: i32
- }
- }
- #[test]
- fn when_parsing_coordinates_then_rust_log_should_be_populated_correctly(){
- let a = Coordinates{ default: "error", x: 2, y: 4 };
- a.set_rust_log();
- match std::env::var("RUST_LOG"){
- Ok(rust_log) => assert_eq!(rust_log, "error,x=2,y=4"),
- Err(_) => panic!("Something bad happened")
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement