Skip to content

Address consumer feedback: serde-with for plain integers, schemars, ParseError, parse(), precision, Div#11

Merged
lthiery merged 1 commit into
mainfrom
consumer-feedback
Jul 7, 2026
Merged

Address consumer feedback: serde-with for plain integers, schemars, ParseError, parse(), precision, Div#11
lthiery merged 1 commit into
mainfrom
consumer-feedback

Conversation

@lthiery

@lthiery lthiery commented Jul 7, 2026

Copy link
Copy Markdown
Owner

Implements items 1–6 from downstream (conveyor) feedback. Every item has test coverage in bytescale.

1. #[serde(with = "humanbyte::serde")] on plain u64/usize

New humanbyte::serde module, generic over integer types (TryInto<u64>/TryFrom<u64>), plus humanbyte::serde::map_keys for BTreeMaps keyed by byte sizes. Human-readable formats (JSON/TOML) get "1.5 KiB" strings; binary formats get raw u64. This should let conveyor drop its hand-rolled byte_size.rs and consolidate off ubyte.

Note: humanbyte::serde was previously the re-export of the serde crate (used by derive-generated code). That re-export moved to a #[doc(hidden)] serde_crate alias. Anyone importing humanbyte::serde::Serialize directly (unlikely outside the derive) breaks.

2. schemars feature

impl JsonSchema emitted by the full HumanByte bundle when the feature is on (also available à la carte as HumanByteSchema). Schema is "type": ["string", "integer"], matching what the deserializer actually accepts. Targets schemars 1.x — say the word if conveyor is still on 0.8/0.9 and I'll widen it.

3. ParseError is now the real FromStr::Err

Derived FromStr returns humanbyte::ParseError (implements std::error::Error under std), so ?-chaining into TAF-style error types works. ParseError is no longer dead code.

4. humanbyte::parse(&str) -> Result<u64, ParseError>

The parse logic moved out of the generated code into the core crate (the derive now just calls it — less token bloat per derive, too). No more throwaway struct DiskSize(u64).

5. Configurable precision

humanbyte::to_string_with_precision(bytes, format, precision) + derived size.to_string_with_precision(Format::IEC, 2)"1.00 TiB". to_string keeps its 1-decimal default, so no display output changes.

6. Div/DivAssign (+ Rem)

size / size → u64 (a dimensionless count: file_size / chunk_size chunks), size / scalar → size, DivAssign<scalar>, and Rem<size> since chunking is the motivating case. Also made Format Copy + PartialEq + Eq.

Minor items — deliberately not done

  • Format::IECFormat::Iec: upper_case_acronyms only fires at the definition site (allowed here), consumers don't lint on use — renaming would break all 7 existing use sites for no lint win.
  • HumanByteRange removal: left in place; that's a separate decision.

Breaking changes (needs 0.3.0 at release)

  • FromStr::Err: StringParseError
  • humanbyte::serde path repurposed
  • Unit::from_str error type likewise ParseError

…arseError, parse(), precision, Div

- humanbyte::serde module usable as #[serde(with = "humanbyte::serde")]
  directly on u64/usize fields, plus map_keys for BTreeMaps keyed by
  byte sizes; human-readable formats get strings, binary formats get
  raw u64. The serde crate re-export moves to a hidden serde_crate
  alias to free up the path (derive output updated in lockstep)
- new schemars feature: derives emit impl JsonSchema (string-or-integer
  schema) via the new HumanByteSchema derive, included in the full
  HumanByte bundle when the feature is on
- derived FromStr now uses humanbyte::ParseError (impls
  std::error::Error) instead of a bare String; parsing logic moves out
  of the generated code into a public humanbyte::parse(&str) ->
  Result<u64, ParseError> usable without declaring a newtype
- to_string_with_precision(bytes, format, precision) plus a matching
  derived method; to_string keeps the 1-decimal default
- ops derive gains Div (size/size -> u64 count, size/scalar -> size),
  DivAssign, and Rem
- Format is now Copy + PartialEq + Eq

BREAKING: FromStr::Err changed from String to ParseError;
humanbyte::serde is no longer the serde crate re-export. Needs a minor
version bump before release.
@lthiery lthiery merged commit ef31c5c into main Jul 7, 2026
1 check passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant