From b9ca02fbaabcfb601fc91ee8762ddea2f41dff9e Mon Sep 17 00:00:00 2001 From: Casey Rodarmor Date: Tue, 4 Feb 2020 08:36:00 -0800 Subject: [PATCH] Show information about torrents after creation type: changed --- src/metainfo.rs | 4 ++++ src/torrent/create.rs | 39 +++++++++++++++++++++++++++++++++++++-- src/torrent/show.rs | 6 +----- src/torrent_summary.rs | 35 ++++++++++++++++++++++++++--------- 4 files changed, 68 insertions(+), 16 deletions(-) diff --git a/src/metainfo.rs b/src/metainfo.rs index b9453e8..010dc17 100644 --- a/src/metainfo.rs +++ b/src/metainfo.rs @@ -32,4 +32,8 @@ impl Metainfo { let path = path.as_ref(); serde_bencode::de::from_bytes(&bytes).context(error::MetainfoLoad { path }) } + + pub(crate) fn serialize(&self) -> Result, Error> { + serde_bencode::ser::to_bytes(&self).context(error::MetainfoSerialize) + } } diff --git a/src/torrent/create.rs b/src/torrent/create.rs index b6a1d7b..6af20aa 100644 --- a/src/torrent/create.rs +++ b/src/torrent/create.rs @@ -130,7 +130,7 @@ impl Linter { } impl Create { - pub(crate) fn run(self, env: &Env) -> Result<(), Error> { + pub(crate) fn run(self, env: &mut Env) -> Result<(), Error> { let mut linter = Linter::new(); linter.allow(self.allowed_lints.iter().cloned()); @@ -239,7 +239,11 @@ impl Create { info, }; - metainfo.dump(&output)?; + let bytes = metainfo.serialize()?; + + fs::write(&output, &bytes).context(error::Filesystem { path: &output })?; + + TorrentSummary::from_metainfo(metainfo)?.write(env)?; if self.open { Platform::open(&output)?; @@ -253,6 +257,8 @@ impl Create { mod tests { use super::*; + use pretty_assertions::assert_eq; + fn environment(args: &[&str]) -> TestEnv { testing::env(["torrent", "create"].iter().chain(args).cloned()) } @@ -855,4 +861,33 @@ mod tests { fs::create_dir(&dir).unwrap(); env.run().unwrap(); } + + #[test] + fn output() { + let mut env = environment(&[ + "--input", + "foo", + "--announce", + "http://bar", + "--no-creation-date", + ]); + let dir = env.resolve("foo"); + fs::create_dir(&dir).unwrap(); + fs::write(dir.join("a"), "abc").unwrap(); + fs::write(dir.join("x"), "xyz").unwrap(); + fs::write(dir.join("h"), "hij").unwrap(); + env.run().unwrap(); + let have = env.out(); + let want = " Name foo + Info Hash 8197efe97f10f50f249e8d5c63eb5c0d4e1d9b49 +Torrent Size 166 bytes +Content Size 0 bytes + Private no + Tracker http://bar/ + Piece Size 512 KiB + Piece Count 1 + File Count 0 +"; + assert_eq!(have, want); + } } diff --git a/src/torrent/show.rs b/src/torrent/show.rs index 60c6b97..c039a4d 100644 --- a/src/torrent/show.rs +++ b/src/torrent/show.rs @@ -19,11 +19,7 @@ impl Show { pub(crate) fn run(self, env: &mut Env) -> Result<(), Error> { let summary = TorrentSummary::load(&env.resolve(self.input))?; - let table = summary.table(); - - table - .write_human_readable(&mut env.out) - .context(error::Stdout)?; + summary.write(env)?; Ok(()) } diff --git a/src/torrent_summary.rs b/src/torrent_summary.rs index 5f47fc8..7b88dfe 100644 --- a/src/torrent_summary.rs +++ b/src/torrent_summary.rs @@ -7,11 +7,7 @@ pub(crate) struct TorrentSummary { } impl TorrentSummary { - pub(crate) fn load(path: &Path) -> Result { - let bytes = fs::read(path).context(error::Filesystem { path })?; - - let metainfo = Metainfo::deserialize(path, &bytes)?; - + fn new(bytes: &[u8], metainfo: Metainfo) -> Result { let value = bencode::Value::decode(&bytes).unwrap(); let infohash = if let bencode::Value::Dict(items) = value { @@ -26,16 +22,37 @@ impl TorrentSummary { unreachable!() }; - let metadata = path.metadata().context(error::Filesystem { path })?; - Ok(Self { - size: Bytes(metadata.len().into()), + size: Bytes::from(bytes.len().into_u64()), infohash, metainfo, }) } - pub(crate) fn table(&self) -> Table { + pub(crate) fn from_metainfo(metainfo: Metainfo) -> Result { + let bytes = metainfo.serialize()?; + Self::new(&bytes, metainfo) + } + + pub(crate) fn load(path: &Path) -> Result { + let bytes = fs::read(path).context(error::Filesystem { path })?; + + let metainfo = Metainfo::deserialize(path, &bytes)?; + + Self::new(&bytes, metainfo) + } + + pub(crate) fn write(&self, env: &mut Env) -> Result<(), Error> { + let table = self.table(); + + table + .write_human_readable(&mut env.out) + .context(error::Stdout)?; + + Ok(()) + } + + fn table(&self) -> Table { let mut table = Table::new(); table.row("Name", &self.metainfo.info.name);