Conversation
Allow &str and &[&str] as input for `command` Fixes #25
aed80a1 to
a04a0e1
Compare
colin-kiegel
left a comment
There was a problem hiding this comment.
Sorry, I commented in German - didn't notice. ^^
The general approach is very good, but I see some problems with the parsing implementation. IMO we should define some invariants which should hold, i.e. stringify!("...").to_cmd()] should equal ["..."].to_cmd() for any string "...".
| } else { | ||
| in_quote.push(c); | ||
| } | ||
| } |
There was a problem hiding this comment.
Ich glaube es gibt Randfälle, in denen sich das noch unerwartet verhalten könnte. Ich spiele mal Advocatus Diaboli:
assert_eq!(
r#"echo lorem ipsum"#.to_cmd(),
vec!["echo", "lorem", "ipsum"]
);
assert_eq!(
r#"echo "lorem' ipsum" dolor "sit' amet" '"#.to_cmd(),
vec!["echo", "lorem' ipsum", "dolor", "sit' amet"]
);
assert_eq!(
r#"echo "lorem\" ipsum" dolor "sit\" amet"'"#.to_cmd(),
vec!["echo", "lorem\" ipsum", "dolor", "sit\" amet"]
);Bei echo ist das unkritisch, bei anderen Befehlen könnte es schon problematisch sein, wenn Argument 3 plötzlich an Position 4 steht, etc. Vielleicht kann man sich hier auch irgendein fuzzy-testing überlegen? Das macht es natürlich alles sehr sehr kompliziert. Wichtig wäre aber, dass es sich vorhersehbar verhält.
M.E. wäre folgende Invariante sehr erstrebenswert:
- Wenn
"..."ein gültiger Rust String ist (...sind Platzhalter), dann solltestringify!("...").to_cmd()]gleich["..."].to_cmd()sein.
Ich kenne die stringify! Methode nicht im Detail, vermute aber, dass auf einen String angewendet, JSON::from_str(stringify!("...")) weitestgehend die Identität sein sollte. Auf dieser Annahme basiert ja die aktuelle Makro-Logik. Auch hier wäre vielleicht mal eine genauere Recherche oder Fuzzy Testing sinnvoll. :-)
There was a problem hiding this comment.
r#"echo "lorem' ipsum" dolor "sit' amet" '"#.to_cmd()
Great test case! I was so focused on getting other stuff that I think I actually went a bit overboard with this: We don't really have to deal with nested quotes at all – just recognize and ignore escaped quotes, right?
I've also just seen this crate, but I think it's more complex than we need here. I don't want to emulate shell syntax itself, just split some args :)
I think I can add quickcheck to test the invariant, but I'm not sure JSON::from_str(stringify!("...")) is that reliable. We'll see :)
|
PS: I would concentrate on supporting |
|
Let's put this on hold for now. |
Allow
&strand&[&str]as input forcommandFixes #25
Currently based on #24. Rebase on master before merging