#byte-size #human-readable #byte #format #utility

readable_byte

An implmentation of human-readable bytesize

15 releases

0.1.14 Aug 17, 2022
0.1.13 Aug 14, 2022
0.1.12 Jul 10, 2022
0.1.7 Jun 20, 2022
0.1.4 Apr 14, 2022

#804 in Development tools

Download history 2/week @ 2023-12-04 3/week @ 2023-12-18 5/week @ 2023-12-25 49/week @ 2024-01-08 39/week @ 2024-01-15 8/week @ 2024-01-22 5/week @ 2024-01-29 12/week @ 2024-02-05 16/week @ 2024-02-12 4/week @ 2024-02-19 108/week @ 2024-02-26 26/week @ 2024-03-04 33/week @ 2024-03-11 25/week @ 2024-03-18

192 downloads per month
Used in 2 crates (via get_sys_info)

MIT license

23KB
509 lines

readable_byte

crates.io API Docs unlicense

readable_byte is an implmentation of human-readable bytesize

Usage

Add this to your Cargo.toml:

[dependencies]
readable_byte="0.1.0"

Example

Human readable representations

    fn assert_to_string_u64(expected: &str, b: readable_byte, si: bool) {
        assert_eq!(expected.to_string(), b.to_string_as(si));
    }

    #[test]
    fn test_to_string_as() {
        assert_to_string_u64("215 B", readable_byte::b(215), true);
        assert_to_string_u64("215 B", readable_byte::b(215), false);

        assert_to_string_u64("1.0 kiB", readable_byte::kib(1), true);
        assert_to_string_u64("1.0 KB", readable_byte::kib(1), false);

        assert_to_string_u64("293.9 kiB", readable_byte::kb(301), true);
        assert_to_string_u64("301.0 KB", readable_byte::kb(301), false);

        assert_to_string_u64("1.0 MiB", readable_byte::mib(1), true);
        assert_to_string_u64("1048.6 KB", readable_byte::mib(1), false);

        assert_to_string_u64("1.9 GiB", readable_byte::mib(1907), true);
        assert_to_string_u64("2.0 GB", readable_byte::mib(1908), false);

        assert_to_string_u64("399.6 MiB", readable_byte::mb(419), true);
        assert_to_string_u64("419.0 MB", readable_byte::mb(419), false);

        assert_to_string_u64("482.4 GiB", readable_byte::gb(518), true);
        assert_to_string_u64("518.0 GB", readable_byte::gb(518), false);

        assert_to_string_u64("741.2 TiB", readable_byte::tb(815), true);
        assert_to_string_u64("815.0 TB", readable_byte::tb(815), false);

        assert_to_string_u64("540.9 PiB", readable_byte::pb(609), true);
        assert_to_string_u64("609.0 PB", readable_byte::pb(609), false);
    }
    #[test]
    fn test_default() {
        assert_eq!(readable_byte::b(0), readable_byte::default());
    }

    #[test]
    fn test_to_string_u64() {
        assert_to_string_u64("88.0 PB", readable_byte::pb(88), false);
    }
  #[test]
  fn test_parsing_from_str() {
      // shortcut for writing test cases
      fn parse(s: &str) -> u64 {
          s.parse::<readable_byte>().unwrap().0
      }

      assert_eq!("0".parse::<readable_byte>().unwrap().0, 0);
      assert_eq!(parse("0"), 0);
      assert_eq!(parse("500"), 500);
      assert_eq!(parse("1K"), Unit::KiloByte * 1);
      assert_eq!(parse("1Ki"), Unit::KibiByte * 1);
      assert_eq!(parse("1.5Ki"), (1.5 * Unit::KibiByte) as u64);
      assert_eq!(parse("1KiB"), 1 * Unit::KibiByte);
      assert_eq!(parse("1.5KiB"), (1.5 * Unit::KibiByte) as u64);
      assert_eq!(parse("3 MB"), Unit::MegaByte * 3);
      assert_eq!(parse("4 MiB"), Unit::MebiByte * 4);
      assert_eq!(parse("6 GB"), 6 * Unit::GigaByte);
      assert_eq!(parse("4 GiB"), 4 * Unit::GibiByte);
      assert_eq!(parse("88TB"), 88 * Unit::TeraByte);
      assert_eq!(parse("521TiB"), 521 * Unit::TebiByte);
      assert_eq!(parse("8 PB"), 8 * Unit::PetaByte);
      assert_eq!(parse("8P"), 8 * Unit::PetaByte);
      assert_eq!(parse("12 PiB"), 12 * Unit::PebiByte);
  }

Dependencies

~180KB