Skip to content

Expose BODYSTRUCTURE octet count as MessagePart.size (supersedes #184)#187

Merged
odrobnik merged 3 commits into
mainfrom
claude/pr-184-size-fix
Jul 17, 2026
Merged

Expose BODYSTRUCTURE octet count as MessagePart.size (supersedes #184)#187
odrobnik merged 3 commits into
mainfrom
claude/pr-184-size-fix

Conversation

@odrobnik

@odrobnik odrobnik commented Jul 17, 2026

Copy link
Copy Markdown
Contributor

What

Carries #184 forward with the review finding fixed. The branch contains the original feature commit from @appinteractive as-is (6245c30), with main merged in on top plus a follow-up fix — so merging this PR (with a merge commit) makes GitHub mark #184 as merged automatically.

MessagePart gains an optional size property populated from the BODYSTRUCTURE body-fld-octets field, so clients can show size labels and enforce download caps from the structure fetch alone, without fetching part bytes:

/// The size of the part body in octets, as reported by BODYSTRUCTURE.
/// Zero is a valid size (an empty part); `nil` means the size is unknown
/// because the part was not built from a BODYSTRUCTURE response.
public let size: Int?

Fix over #184

The review on #184 flagged that the conversion collapsed a reported octet count of 0 to nil. In the pinned NIOIMAP 0.3.0 parser body-fld-octets is mandatory — a missing field makes the BODYSTRUCTURE invalid and never reaches this converter — so 0 is a legitimately reported size of an empty part, not a missing-value sentinel. The converter now passes the octet count through unchanged, keeping empty parts distinguishable from parts whose size metadata is unavailable (e.g. not built from BODYSTRUCTURE). The doc comment spells out these semantics, and the test asserting 0 → nil now asserts 0 is preserved.

Changes

  • MessagePart: new size: Int? property, threaded through both public initializers and the Codable implementation (encoded with encodeIfPresent, so existing archives decode fine).
  • MessagePart+BodyStructure: read fields.octetCount when flattening singleparts, passing it through as-is.
  • MessagePartSizeTests: singlepart carries the octet count, zero is preserved, multipart children each carry their own size.

Note: the size is the encoded (transfer-encoding) octet count per RFC 3501 — for base64 parts roughly 4/3 of the decoded file size.

Test plan

  • swift build clean
  • Full suite: 352 tests in 48 suites pass locally on macOS

🤖 Generated with Claude Code

appinteractive and others added 3 commits July 14, 2026 20:03
BODYSTRUCTURE reports each part's body size in octets, but the value was
dropped when building MessagePart from a BodyStructure. Carry it through
as an optional size property (nil when the server reports 0/absent) so
clients can show size labels and apply download caps without fetching
part bytes.
body-fld-octets is mandatory in BODYSTRUCTURE, so NIOIMAP 0.3.0 never
uses 0 as a missing-field sentinel: a missing value makes the body
structure invalid and never reaches this converter. Collapsing 0 to nil
made a known-empty part indistinguishable from unavailable size
metadata, so callers showing exact sizes or enforcing download caps
would wrongly treat empty parts as unknown-size.

Addresses review feedback on #184.

Co-Authored-By: Claude Fable 5 <noreply@anthropic.com>
@odrobnik
odrobnik force-pushed the claude/pr-184-size-fix branch from 6341ab9 to 6c62988 Compare July 17, 2026 14:40
@odrobnik
odrobnik merged commit dab5f98 into main Jul 17, 2026
6 checks 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.

2 participants