Address consumer feedback: serde-with for plain integers, schemars, ParseError, parse(), precision, Div#11
Merged
Merged
Conversation
…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.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.
Implements items 1–6 from downstream (conveyor) feedback. Every item has test coverage in bytescale.
1.
#[serde(with = "humanbyte::serde")]on plainu64/usizeNew
humanbyte::serdemodule, generic over integer types (TryInto<u64>/TryFrom<u64>), plushumanbyte::serde::map_keysforBTreeMaps keyed by byte sizes. Human-readable formats (JSON/TOML) get"1.5 KiB"strings; binary formats get rawu64. This should let conveyor drop its hand-rolledbyte_size.rsand consolidate offubyte.Note:
humanbyte::serdewas previously the re-export of the serde crate (used by derive-generated code). That re-export moved to a#[doc(hidden)] serde_cratealias. Anyone importinghumanbyte::serde::Serializedirectly (unlikely outside the derive) breaks.2.
schemarsfeatureimpl JsonSchemaemitted by the fullHumanBytebundle when the feature is on (also available à la carte asHumanByteSchema). 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.
ParseErroris now the realFromStr::ErrDerived
FromStrreturnshumanbyte::ParseError(implementsstd::error::Errorunderstd), so?-chaining into TAF-style error types works.ParseErroris 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)+ derivedsize.to_string_with_precision(Format::IEC, 2)→"1.00 TiB".to_stringkeeps its 1-decimal default, so no display output changes.6.
Div/DivAssign(+Rem)size / size → u64(a dimensionless count:file_size / chunk_sizechunks),size / scalar → size,DivAssign<scalar>, andRem<size>since chunking is the motivating case. Also madeFormatCopy + PartialEq + Eq.Minor items — deliberately not done
Format::IEC→Format::Iec:upper_case_acronymsonly 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.HumanByteRangeremoval: left in place; that's a separate decision.Breaking changes (needs 0.3.0 at release)
FromStr::Err:String→ParseErrorhumanbyte::serdepath repurposedUnit::from_strerror type likewiseParseError