Skip to content

Commit

Permalink
Add render subcommand (#71)
Browse files Browse the repository at this point in the history
  • Loading branch information
casey authored Oct 11, 2024
1 parent 9e5cabe commit 37e7557
Show file tree
Hide file tree
Showing 17 changed files with 515 additions and 11 deletions.
1 change: 1 addition & 0 deletions .gitattributes
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
* -text
70 changes: 70 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,12 +14,14 @@ include = ["CHANGELOG.md", "CONTRIBUTING", "LICENSE", "README.md", "src", "tests

[dependencies]
blake3 = { version = "1.5.4", features = ["mmap", "rayon", "serde"] }
boilerplate = "1.0.1"
camino = { version = "1.1.9", features = ["serde1"] }
clap = { version = "4.5.16", features = ["derive"] }
clap_mangen = "0.2.23"
dirs = "5.0.1"
ed25519-dalek = { version = "2.1.1", features = ["rand_core"] }
hex = "0.4.3"
html-escaper = "0.2.0"
indicatif = "0.17.8"
lexiclean = "0.0.1"
owo-colors = "4"
Expand All @@ -35,10 +37,12 @@ walkdir = "2.5.0"
assert_cmd = { version = "2.0.16", features = ["color-auto"] }
assert_fs = { version = "1.1.2", features = ["color-auto"] }
predicates = "3.1.2"
pretty_assertions = "1.4.1"
regex = "1.10.6"

[lints.clippy]
all = { level = "deny", priority = -1 }
float-cmp = "allow"
large_enum_variant = "allow"
needless-pass-by-value = "allow"
pedantic = { level = "deny", priority = -1 }
Expand Down
12 changes: 12 additions & 0 deletions justfile
Original file line number Diff line number Diff line change
Expand Up @@ -105,3 +105,15 @@ verify-release: tmp
--dir tmp \
$VERSION
cargo run verify tmp --key 3c977ea3a31cd37f0b540f02f33eab158f2ed7449f42b05613c921181aa95b79
render: tmp
#!/usr/bin/env bash
set -euxo pipefail
VERSION=`bin/version`
gh release download \
--repo casey/filepack \
--pattern '*' \
--dir tmp \
$VERSION
rm tmp/filepack.json
cargo run create tmp --sign --metadata metadata.yaml
1 change: 1 addition & 0 deletions metadata.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
title: filepack
67 changes: 67 additions & 0 deletions src/bytes.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,67 @@
use super::*;

pub(crate) struct Bytes(pub(crate) u64);

impl Display for Bytes {
fn fmt(&self, f: &mut Formatter) -> fmt::Result {
const SUFFIXES: &[&str] = &["KiB", "MiB", "GiB", "TiB", "PiB", "EiB"];

#[allow(clippy::cast_precision_loss)]
let mut value = self.0 as f64;

let mut i = 0;

while value >= 1024.0 {
value /= 1024.0;
i += 1;
}

let suffix = if i == 0 {
if value == 1.0 {
"byte"
} else {
"bytes"
}
} else {
SUFFIXES[i - 1]
};

let formatted = format!("{value:.2}");
let trimmed = formatted.trim_end_matches('0').trim_end_matches('.');
write!(f, "{trimmed} {suffix}")
}
}

#[cfg(test)]
mod tests {
use super::*;

const KI: u64 = 1 << 10;
const MI: u64 = KI << 10;
const GI: u64 = MI << 10;
const TI: u64 = GI << 10;
const PI: u64 = TI << 10;
const EI: u64 = PI << 10;

#[test]
fn display() {
#[track_caller]
fn case(bytes: u64, expected: &str) {
assert_eq!(Bytes(bytes).to_string(), expected);
}

case(0, "0 bytes");
case(1, "1 byte");
case(2, "2 bytes");
case(KI, "1 KiB");
case(512 * KI, "512 KiB");
case(MI, "1 MiB");
case(MI + 512 * KI, "1.5 MiB");
case(1024 * MI + 512 * MI, "1.5 GiB");
case(GI, "1 GiB");
case(TI, "1 TiB");
case(PI, "1 PiB");
case(EI, "1 EiB");
case(u64::MAX, "16 EiB");
}
}
4 changes: 4 additions & 0 deletions src/filesystem.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,3 +11,7 @@ pub(crate) fn metadata(path: &Utf8Path) -> Result<std::fs::Metadata> {
pub(crate) fn write(path: &Utf8Path, contents: &[u8]) -> Result {
std::fs::write(path, contents).context(error::Io { path })
}

pub(crate) fn exists(path: &Utf8Path) -> Result<bool> {
path.try_exists().context(error::Io { path })
}
14 changes: 9 additions & 5 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,17 @@
use {
self::{
arguments::Arguments, display_path::DisplayPath, display_secret::DisplaySecret, entry::Entry,
error::Error, hash::Hash, io_result_ext::IoResultExt, lint::Lint, lint_group::LintGroup,
list::List, manifest::Manifest, metadata::Metadata, options::Options,
owo_colorize_ext::OwoColorizeExt, private_key::PrivateKey, public_key::PublicKey,
arguments::Arguments, bytes::Bytes, display_path::DisplayPath, display_secret::DisplaySecret,
entry::Entry, error::Error, hash::Hash, io_result_ext::IoResultExt, lint::Lint,
lint_group::LintGroup, list::List, manifest::Manifest, metadata::Metadata, options::Options,
owo_colorize_ext::OwoColorizeExt, page::Page, private_key::PrivateKey, public_key::PublicKey,
relative_path::RelativePath, signature::Signature, signature_error::SignatureError,
style::Style, subcommand::Subcommand, template::Template, utf8_path_ext::Utf8PathExt,
},
blake3::Hasher,
boilerplate::Boilerplate,
camino::{Utf8Component, Utf8Path, Utf8PathBuf},
clap::{Parser, ValueEnum},
html_escaper::Escape,
indicatif::{ProgressBar, ProgressStyle},
lexiclean::Lexiclean,
owo_colors::Styled,
Expand All @@ -20,7 +22,7 @@ use {
array::TryFromSliceError,
backtrace::{Backtrace, BacktraceStatus},
cmp::Ordering,
collections::{BTreeMap, HashMap},
collections::{BTreeMap, HashMap, HashSet},
env,
fmt::{self, Display, Formatter},
fs::{self, File},
Expand All @@ -36,6 +38,7 @@ use {
use assert_fs::TempDir;

mod arguments;
mod bytes;
mod display_path;
mod display_secret;
mod entry;
Expand All @@ -50,6 +53,7 @@ mod manifest;
mod metadata;
mod options;
mod owo_colorize_ext;
mod page;
mod private_key;
mod progress_bar;
mod public_key;
Expand Down
9 changes: 9 additions & 0 deletions src/manifest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,9 +25,18 @@ impl Manifest {
hasher.finalize().into()
}

pub(crate) fn load(path: &Utf8Path) -> Result<Self> {
serde_json::from_str(&filesystem::read_to_string(path)?)
.context(error::DeserializeManifest { path })
}

pub(crate) fn to_json(&self) -> String {
serde_json::to_string(self).unwrap()
}

pub(crate) fn total_size(&self) -> u64 {
self.files.values().map(|entry| entry.size).sum()
}
}

#[cfg(test)]
Expand Down
7 changes: 6 additions & 1 deletion src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,12 +3,17 @@ use super::*;
#[derive(Debug, Deserialize, Serialize)]
#[serde(rename_all = "kebab-case")]
pub(crate) struct Metadata {
title: String,
pub(crate) title: String,
}

impl Metadata {
pub(crate) const FILENAME: &'static str = "metadata.json";

pub(crate) fn load(path: &Utf8Path) -> Result<Self> {
serde_json::from_str(&filesystem::read_to_string(path)?)
.context(error::DeserializeMetadata { path })
}

pub(crate) fn to_json(&self) -> String {
serde_json::to_string(self).unwrap()
}
Expand Down
Loading

0 comments on commit 37e7557

Please sign in to comment.