-
Notifications
You must be signed in to change notification settings - Fork 25
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Allow reading torrent metainfo from stdin
Torrent metainfo can be read from standard input by passing `-`: cat a.torrent | imdl torrent verify --input - cat a.torrent | imdl torrent link --input - cat a.torrent | imdl torrent show --input - type: added
- Loading branch information
Showing
15 changed files
with
343 additions
and
86 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
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,29 @@ | ||
use crate::common::*; | ||
|
||
pub(crate) struct Input { | ||
source: InputTarget, | ||
data: Vec<u8>, | ||
} | ||
|
||
impl Input { | ||
pub(crate) fn new(source: InputTarget, data: Vec<u8>) -> Input { | ||
Self { source, data } | ||
} | ||
|
||
pub(crate) fn data(&self) -> &[u8] { | ||
&self.data | ||
} | ||
|
||
pub(crate) fn source(&self) -> &InputTarget { | ||
&self.source | ||
} | ||
|
||
#[cfg(test)] | ||
pub(crate) fn from_path(path: &Path) -> Result<Input> { | ||
let data = fs::read(path).context(error::Filesystem { path })?; | ||
Ok(Input { | ||
source: InputTarget::File(path.to_owned()), | ||
data, | ||
}) | ||
} | ||
} |
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,69 @@ | ||
use crate::common::*; | ||
|
||
#[derive(PartialEq, Debug, Clone)] | ||
pub(crate) enum InputTarget { | ||
File(PathBuf), | ||
Stdin, | ||
} | ||
|
||
impl From<&OsStr> for InputTarget { | ||
fn from(text: &OsStr) -> Self { | ||
if text == OsStr::new("-") { | ||
Self::Stdin | ||
} else { | ||
Self::File(text.into()) | ||
} | ||
} | ||
} | ||
|
||
impl Display for InputTarget { | ||
fn fmt(&self, f: &mut Formatter) -> fmt::Result { | ||
match self { | ||
Self::Stdin => write!(f, "standard input"), | ||
Self::File(path) => write!(f, "`{}`", path.display()), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
impl<P: AsRef<Path>> PartialEq<P> for InputTarget { | ||
fn eq(&self, other: &P) -> bool { | ||
match self { | ||
Self::File(path) => path == other.as_ref(), | ||
Self::Stdin => Path::new("-") == other.as_ref(), | ||
} | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
#[test] | ||
fn file() { | ||
assert_eq!( | ||
InputTarget::from(OsStr::new("foo")), | ||
InputTarget::File("foo".into()) | ||
); | ||
} | ||
|
||
#[test] | ||
fn stdio() { | ||
assert_eq!(InputTarget::from(OsStr::new("-")), InputTarget::Stdin); | ||
} | ||
|
||
#[test] | ||
fn display_file() { | ||
let path = PathBuf::from("./path"); | ||
let have = InputTarget::File(path).to_string(); | ||
let want = "`./path`"; | ||
assert_eq!(have, want); | ||
} | ||
|
||
#[test] | ||
fn display_stdio() { | ||
let have = InputTarget::Stdin.to_string(); | ||
let want = "standard input"; | ||
assert_eq!(have, want); | ||
} | ||
} |
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
Oops, something went wrong.