feat: add placeholder cmdline concept
This commit is contained in:
@@ -916,3 +916,72 @@ impl FromStr for Verb {
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region: Command Line
|
||||
|
||||
/// The error occurs when constructing CmdLine with bad arguments.
|
||||
#[derive(Debug, TeError)]
|
||||
#[error("given file extension body \"{inner}\" is invalid")]
|
||||
pub struct BadCmdLineError {
|
||||
inner: String,
|
||||
}
|
||||
|
||||
/// The struct representing a Windows command line.
|
||||
///
|
||||
/// # Note
|
||||
///
|
||||
/// This struct currently does nothing for validation for given command line.
|
||||
/// Because there is no standard for validating this.
|
||||
/// So I just write this struct as a placeholder for future extension.
|
||||
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
|
||||
pub struct CmdLine {
|
||||
inner: Vec<String>,
|
||||
}
|
||||
|
||||
impl CmdLine {
|
||||
/// Create a new command line.
|
||||
pub fn new<S: AsRef<str>>(args: &[S]) -> Result<Self, BadCmdLineError> {
|
||||
Ok(Self { inner: args.iter().map(|s| s.as_ref().to_string()).collect() })
|
||||
}
|
||||
|
||||
/// Get the full command line.
|
||||
pub fn full(&self) -> String {
|
||||
self.inner.join(" ")
|
||||
}
|
||||
|
||||
/// Get the iterator of command line arguments.
|
||||
pub fn iter(&self) -> impl Iterator<Item = &str> {
|
||||
self.inner.iter().map(|s| s.as_str())
|
||||
}
|
||||
}
|
||||
|
||||
/// The error occurs when constructing CmdLine with bad syntax.
|
||||
#[derive(Debug, TeError)]
|
||||
#[error("given command line \"{inner}\" is invalid")]
|
||||
pub struct ParseCmdLineError {
|
||||
inner: String,
|
||||
}
|
||||
|
||||
impl ParseCmdLineError {
|
||||
fn new(inner: &str) -> Self {
|
||||
Self {
|
||||
inner: inner.to_string(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for CmdLine {
|
||||
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
||||
write!(f, "{}", self.full())
|
||||
}
|
||||
}
|
||||
|
||||
impl FromStr for CmdLine {
|
||||
type Err = ParseCmdLineError;
|
||||
|
||||
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
||||
// We simply split it by space for rough result.
|
||||
Ok(Self { inner: s.split(' ').map(|s| s.to_string()).collect() })
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -276,3 +276,26 @@ fn test_verb() {
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
// region: Command Line
|
||||
|
||||
#[test]
|
||||
fn test_cmd_line() {
|
||||
fn ok_tester(s: &str) {
|
||||
let rv = CmdLine::from_str(s);
|
||||
assert!(rv.is_ok());
|
||||
// let rv = rv.unwrap();
|
||||
// assert_eq!(s, rv.full());
|
||||
}
|
||||
fn err_tester(s: &str) {
|
||||
let rv = CmdLine::from_str(s);
|
||||
assert!(rv.is_err());
|
||||
}
|
||||
|
||||
ok_tester("notepad.exe");
|
||||
ok_tester("calc.exe /c");
|
||||
ok_tester("unix-like.exe -i some.file --option");
|
||||
// err_tester("");
|
||||
}
|
||||
|
||||
// endregion
|
||||
|
||||
Reference in New Issue
Block a user