lib_cargo_crate/wrappers/
version.rs1use byte_unit::*;
2use chrono_humanize::HumanTime;
3use crates_io_api::Version;
4use std::fmt::Display;
5
6pub struct WrappedVersion<'a> {
7 pub version: &'a Version,
8}
9
10impl<'a> Display for WrappedVersion<'a> {
11 fn fmt(&self, fmt: &mut std::fmt::Formatter<'_>) -> std::result::Result<(), std::fmt::Error> {
12 let c = self.version;
13 let size = Byte::from_u64(c.crate_size.unwrap_or_default());
14 let adjusted_size = format!("{:.2}", size.get_appropriate_unit(UnitType::Decimal));
15
16 let publisher = match &c.published_by {
19 Some(user) => match user.name.as_ref() {
20 Some(name) => name,
21 _ => "n/a",
22 },
23 _ => "n/a",
24 };
25 fmt.write_fmt(format_args!(
26 " v{version:<9}\t{time:<16}\t{size:<-10}\t{publisher:<20}\t{downloads:<10}\t{yanked:>8}",
27 version = c.num,
28 size = adjusted_size,
29 yanked = if c.yanked { "⚠️ YANKED" } else { "" },
30 publisher = publisher,
31 time = HumanTime::from(c.updated_at),
32 downloads = c.downloads,
33 ))?;
34 Ok(())
39 }
40}
41
42impl<'a> From<&'a Version> for WrappedVersion<'a> {
43 fn from(version: &'a Version) -> Self {
44 WrappedVersion { version }
45 }
46}