-
Notifications
You must be signed in to change notification settings - Fork 2
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add customizable terminators (#15)
While using `cargo-get` in scripts, it is often useful to be able to change the ending character of the output -- ensuring that a newline does *not* get printed, adding a NUL terminator, etc. This commit adds an option `--terminator` which changes the terminator that is printed after the requested output from `cargo-get`. Signed-off-by: vados <[email protected]>
- Loading branch information
Showing
4 changed files
with
167 additions
and
5 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,73 @@ | ||
use std::fmt; | ||
|
||
#[derive(Debug, PartialEq, Clone, Default)] | ||
pub enum Terminator { | ||
Cr, | ||
CrLf, | ||
#[default] | ||
Lf, | ||
Nul, | ||
String(String), | ||
} | ||
|
||
impl fmt::Display for Terminator { | ||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { | ||
f.write_str(match self { | ||
Self::Cr => "\r", | ||
Self::CrLf => "\r\n", | ||
Self::Lf => "\n", | ||
Self::Nul => "\0", | ||
Self::String(s) => s, | ||
}) | ||
} | ||
} | ||
|
||
impl std::str::FromStr for Terminator { | ||
type Err = std::convert::Infallible; | ||
|
||
fn from_str(s: &str) -> std::result::Result<Self, Self::Err> { | ||
match s.to_lowercase().as_ref() { | ||
"cr" => Ok(Self::Cr), | ||
"crlf" => Ok(Self::CrLf), | ||
"lf" => Ok(Self::Lf), | ||
"nul" => Ok(Self::Nul), | ||
_ => Ok(Self::String(s.to_owned())), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod test { | ||
use super::*; | ||
|
||
#[test] | ||
fn display_ok() { | ||
let res = format!( | ||
"{} {} {} {} {}", | ||
Terminator::Cr, | ||
Terminator::CrLf, | ||
Terminator::Lf, | ||
Terminator::Nul, | ||
Terminator::String("abc!@#$%^&*()".to_owned()) | ||
); | ||
|
||
let expected = "\r \r\n \n \0 abc!@#$%^&*()".to_owned(); | ||
|
||
assert_eq!(res, expected); | ||
} | ||
|
||
#[test] | ||
fn parse_ok() -> Result<(), Box<dyn std::error::Error>> { | ||
for (input, result) in [ | ||
("Cr", Terminator::Cr), | ||
("CrLf", Terminator::CrLf), | ||
("Lf", Terminator::Lf), | ||
("Nul", Terminator::Nul), | ||
] { | ||
assert_eq!(input.parse::<Terminator>()?, result); | ||
assert_eq!(input.to_lowercase().parse::<Terminator>()?, result); | ||
assert_eq!(input.to_uppercase().parse::<Terminator>()?, result); | ||
} | ||
Ok(()) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,74 @@ | ||
use assert_cmd::Command; | ||
use predicates::prelude::*; | ||
|
||
#[test] | ||
fn pkg_name_with_custom_terminator() { | ||
let mut cmd = Command::cargo_bin("cargo-get").unwrap(); | ||
let p = std::fs::canonicalize("tests/data/toml_02").unwrap(); | ||
cmd.current_dir(p); | ||
|
||
let assert = cmd.arg("package.name").arg("--terminator=.exe").assert(); | ||
assert | ||
.success() | ||
.stdout(predicate::eq(b"test-name.exe" as &[u8])); | ||
} | ||
|
||
#[test] | ||
fn pkg_name_with_cr_as_terminator() { | ||
let mut cmd = Command::cargo_bin("cargo-get").unwrap(); | ||
let p = std::fs::canonicalize("tests/data/toml_02").unwrap(); | ||
cmd.current_dir(p); | ||
|
||
let assert = cmd.arg("package.name").arg("--terminator=cr").assert(); | ||
assert | ||
.success() | ||
.stdout(predicate::eq(b"test-name\r" as &[u8])); | ||
} | ||
|
||
#[test] | ||
fn pkg_name_with_lf_as_terminator() { | ||
let mut cmd = Command::cargo_bin("cargo-get").unwrap(); | ||
let p = std::fs::canonicalize("tests/data/toml_02").unwrap(); | ||
cmd.current_dir(p); | ||
|
||
let assert = cmd.arg("package.name").arg("--terminator=lf").assert(); | ||
assert | ||
.success() | ||
.stdout(predicate::eq(b"test-name\n" as &[u8])); | ||
} | ||
|
||
#[test] | ||
fn pkg_name_with_crlf_as_terminator() { | ||
let mut cmd = Command::cargo_bin("cargo-get").unwrap(); | ||
let p = std::fs::canonicalize("tests/data/toml_02").unwrap(); | ||
cmd.current_dir(p); | ||
|
||
let assert = cmd.arg("package.name").arg("--terminator=crlf").assert(); | ||
assert | ||
.success() | ||
.stdout(predicate::eq(b"test-name\r\n" as &[u8])); | ||
} | ||
|
||
#[test] | ||
fn pkg_name_with_semicolon_as_terminator() { | ||
let mut cmd = Command::cargo_bin("cargo-get").unwrap(); | ||
let p = std::fs::canonicalize("tests/data/toml_02").unwrap(); | ||
cmd.current_dir(p); | ||
|
||
let assert = cmd.arg("package.name").arg("--terminator=;").assert(); | ||
assert | ||
.success() | ||
.stdout(predicate::eq(b"test-name;" as &[u8])); | ||
} | ||
|
||
#[test] | ||
fn pkg_name_with_nul_as_terminator() { | ||
let mut cmd = Command::cargo_bin("cargo-get").unwrap(); | ||
let p = std::fs::canonicalize("tests/data/toml_02").unwrap(); | ||
cmd.current_dir(p); | ||
|
||
let assert = cmd.arg("package.name").arg("--terminator=nul").assert(); | ||
assert | ||
.success() | ||
.stdout(predicate::eq(b"test-name\0" as &[u8])); | ||
} |