matklad / xshell

Apache License 2.0
696 stars 30 forks source link

Allowing self.field in cmd! macro #62

Open rewk opened 1 year ago

rewk commented 1 year ago

Hi,

Would it be possible to allow using self.field in cmd! macros ? For example, this would allow to use a struct to store common args of commands.

struct Commons {
    sh: Shell,
    field: String,
}

impl Commons {
    fn execute(&self) {
        let command = cmd!(self.sh, "echo {self.field}");
        command.run();
    }
}

I have not attempted to make the change, so that i don't know if it would be possible.

If the self. is removed from the above code, the compiler hints at something that would work.

error[E0425]: cannot find value `field` in this scope
  --> src/main.rs:88:13
   |
88 | /             "
89 | |             echo {field}
90 | |             "
   | |_____________^ help: you might have meant to use the available field: `self.field`
matklad commented 1 year ago

Good question: this certainly is plausible to implement. I don't remeber why I didn't do it that way, one argument would be that rust format! itself only supports identifiers, but that's a relatively week one.

I think we should just support arbitrary expressions there.

rewk commented 1 year ago

That would be very nice! Thanks!