Skip to content

Deconstruct in the Rust shape, name the variants at top level, and release 0.10.0 - #85

Closed
nz wants to merge 27 commits into
mainfrom
2026-09-10-errgonomic-0.9.x-triage/deconstruct
Closed

nz wants to merge 27 commits into
mainfrom
2026-09-10-errgonomic-0.9.x-triage/deconstruct

Conversation

@nz

@nz nz commented Sep 10, 2026 •

Copy link
Copy Markdown
Member

Errgonomic 0.10.0 gives deconstruct the Rust shape and names Some, None, Ok and Err at top level, so a pattern reads as in Some(v). 0.9.x answered [self, value], which bound the wrapper to v under an else, missed without one, could not nest as in Ok(Some(v)), and left None asymmetric with Some. This pull request stacks on the 0.9.3 branch (#84) and targets main, so until #84 merges its diff also shows the earlier releases' commits; it merges after that one. One choice is the reviewer's to confirm: the top-level constants, which the decision note marked as strikeable.

Closes #77.

What changes

  • deconstruct answers [value] for a Some, an Ok and an Err and [] for a None, so in Some(v), in None, in Ok(Some(v)) and in Some({ id: }) all match, and a two-branch case/in with no else raises NoMatchingPatternError on a wrong type. There is no deconstruct_keys.
  • A value-less Err() deconstructs to [], so in Err and in Err() match it and in Err(e) matches only an Err that carries a value. The sentinel behind Err() is internal and never binds to a pattern variable.
  • Some, None, Ok and Err are top-level names beside the constructors of the same name. After review they are Errgonomic::VariantName objects rather than the classes; see below.
  • Version 0.10.0, with a changelog section whose upgrading note shows each old pattern beside its new spelling.

How it works

A probe that loaded active_record, action_view, active_storage, action_text and rails found all four constants undefined at top level, so they collide with nothing Rails defines.

Evidence

Run locally on 2026-09-10, aarch64-darwin, inside the flake devshell:

  • bundle exec rubocop: 24 files inspected, 6 offenses, all six pre-existing on main (Metrics on method_missing in option.rb and result.rb, Metrics on optional_dig, Style/Documentation on Errgonomic::Rails).
  • bundle exec yard doctest: 185 runs, 476 assertions, 0 failures, 0 errors.
  • bundle exec rake test: 132 runs, 464 assertions, 0 failures, 0 errors.
  • bundle exec rake test:strict: 132 runs, 464 assertions, 0 failures, 0 errors.
  • bundle exec rake: the same three counts.
  • nix build .#errgonomic: built errgonomic-0.10.0.
  • nix flake check --all-systems: all checks passed.

CI runs yard doctest, rake test and rake test:strict on Ruby 3.4 on ubuntu-latest.

What is left open

The constants are the line the decision note said the driver may strike. Striking them is one deletion in each of option.rb and result.rb plus the README examples reverting to the long class names; the deconstruct shape does not depend on them. Recommendation: keep them, since in Some(v) is the point of the shape.

Background

Project charter: cowork/projects/2026-09-10-errgonomic-0.9.x-triage/README.md, decision decisions/2026-09-10-deconstruct-takes-the-rust-shape.md.

After review

Review found that the constants made a paren-less None, Rust's spelling of the value, evaluate to the class, whose name then reached a hostname, a JSON payload, a column or a WHERE with no exception, and that an application's own class None silently reopened the gem's. The driver kept the constants and asked for them to be made safe, red first. This branch now carries:

  • Each top-level name is an Errgonomic::VariantName: it matches as its class in a pattern and a case/when, inspects as the class, and raises Errgonomic::SerializeError from to_s, to_json and as_json. in Some(v), in None, in Ok(v), in Err(e) and nested patterns work as before, and the four classes are untouched, so reached by their full name (Errgonomic::Option::Some, and so on) their inspect, name, backtraces and every message and Minitest failure that names them render as before. The bare top-level name does not forward any of that; see below.
  • Every Rails boundary that unwraps an Option refuses a bare name with the same error, because a boolean column cast a bare None to true and an integer column to nil: attribute writers, where and find_by, update_all, insert_all, upsert and declared defaults.
  • An application constant with one of the names fails loudly: defined before the gem loads, it stops the load with a NameError; a class None or module Ok written after raises TypeError. Two forms get past this and the README says so: None = … assigned after the load, which Ruby replaces with only a warning, and a Rails file Zeitwerk skips because the name is already defined, where the first call raises NoMethodError.
  • The doctests for a value that falls through the patterns show the error's message, including the case where the subject is a wrapper and the message refuses to print.

Two other shapes were measured and rejected: a raising singleton to_s on the four classes broke the gem's own strict-equality and sequence_results messages and Minitest's assert_kind_of output, and let class None reopen the class; a top-level None that is the None value made in None raise under strict equality on any subject that is not an Option.

Correction to the comparison above: the chosen shape is not exempt from the assert_kind_of problem either, and is worse for it. Against the bare top-level name, is_a?, kind_of?, instance_of?, .new, <=, .name and assert_kind_of Some, x all raise TypeError or NoMethodError, before any assertion runs. The rejected to_s shape only made assert_kind_of's failure output ugly; it still ran and reported a real pass or fail. Only the fully qualified classes keep all of this working normally. The README's Some/None/Ok/Err section lists every affected reflex and the spelling to use instead.

Gates after the fixes, 2026-09-10: rubocop 6 offenses, the same six; yard doctest 188 runs, 487 assertions; rake test and rake test:strict 142 runs, 640 assertions each; nix build and nix flake check --all-systems pass. CI passed on 9c16c2f.

nz added 13 commits September 10, 2026 10:25
0.9.0 made Option#to_s render as inspect does, to keep a raise out of
the rescue block that interpolates a wrapper into its log line. In a
mass conversion of every model in an application, every site that
built a string from a reader then wrote wrapper text into data with no
exception: a UNIQUE identity column, a hostname, a hashed auth token
and a customer-facing page. Each of those raised under 0.8.3.

The raise comes back as the same Errgonomic::SerializeError, so an
existing rescue still matches, and its message now carries the remedy
the log-line case needs: the value via the bounded inspect, that to_s
is refused, and inspect for a log line or unwrap_or / expect! for the
value. The bounded inspect moves into its own helper so the to_json
refusal and this one name the value the same way.
The same reversal as for Option, with the same message shape: the
value via a bounded inspect, that to_s is refused, and the spellings
to reach for. Result had no bounded inspect helper, so one is added
privately for the to_s refusal; the to_json and as_json refusals keep
their fixed message.
Each reproduction from the 0.9.0 field reports is a test: join,
interpolation, format and String() for both containers; a bare ERB
<%= %> through ActionView, whose template error carries the refusal as
its cause; and an Option or Result in a Hash key, which both the json
gem and ActiveSupport's as_json stringify through to_s, so the raising
to_s closes the key position that 0.9.0 opened.

A None in a bare ERB tag renders as nothing rather than raising: the
Rails integration's None answers nil?, and ActionView's output buffer
skips a nil before it asks for to_s. The test records that as it is.
The changelog names the to_s reversal as a behavior change with an
upgrading note for anyone who wrote against 0.9.0's rendering. The
0.9.0 section stays as history.
or already refused a bare operand with Errgonomic::ArgumentError.
and, xor, zip and zip_with did not: and handed the bare operand back
out of the Option chain, xor and zip fell into a NoMethodError on
some? or none?, and whether anything fired at all depended on the
receiver's variant, so a None-heavy path never learned its operand was
wrong. The check now runs before the discriminant is consulted, on all
five combinators, with or's message.
Some, Ok and Err exposed value as a public accessor, so a wrapper could
be mutated through an alias, a Hash key could move buckets under its
own hash, and the reader reached the inner value with no None branch,
the ambiguous failure the type exists to prevent. The reader is now
protected, for the sibling reads equality, ordering and zip need, and
every instance freezes on construction. A call to value or value= from
outside reaches method_missing and gets the combinator teaching.

sequence_options and sequence_results were the only readers outside
the classes; they collect through tap_some and tap_ok instead.
deconstruct answered [self, value] for a Some, an Ok and an Err, and
[None] for a None, so the Rust-shaped pattern `in Some(v)` bound the
wrapper to v under an else and missed without one, nesting such as
`in Ok(Some(v))` could not be written, and None was asymmetric with
Some. It now answers [value] for Some, Ok and Err and [] for None, the
shape Data.define(:value) and Rust's tuple variants share, so a
two-branch case/in with no else is exhaustive and a wrong type reaches
NoMatchingPatternError. There is no deconstruct_keys: a one-payload
sum type has no named field, and a Some around a Hash nests through
the Hash's own protocol as `in Some({id:})`.

Some, None, Ok and Err are also defined as top-level constants beside
the constructors of the same name, so a pattern reads as Rust does.
Rails defines none of the four.
Err() carries an internal sentinel in place of a value, and a sentinel
must never bind to a pattern variable. It now deconstructs to [], so
`in Err` and `in Err()` match it and `in Err(e)` matches only an Err
that carries a value.
nz added 14 commits September 10, 2026 12:27
unwrap_err! passed the Ok's value as the exception message, so the
message was the value's to_s. With to_s refused on an Option or a
Result, Ok(Some(1)).unwrap_err! printed only the class name and its
message raised. The message is now the value's inspect, bounded as the
to_s refusal bounds it, and the error carries the value as unwrap!'s
does.
The join test covered the same shape with different values, and the
group_by test grouped bare Options rather than a converted model's
reader. Both lines now appear as #75 writes them: a join of a Some and
a None, and a group_by over a converted model's optional association
on its way to JSON.
With ActiveSupport loaded, Hash#to_json reaches a key through as_json,
which refused an Option under 0.9.0 as well, so the two to_json
assertions in the hash-key test passed before the to_s change and did
not test it. They move to a child process that loads the json gem and
errgonomic alone, where 0.9.0 rendered {"Some(1)":2} and a raising to_s
refuses.
A case/in with several branches and none matching raises
NoMatchingPatternError with the subject itself as its message, so on
an Option or a Result the message refuses. The top-level printer then
shows only the class name, and Minitest's reporter crashes before it
names the test. The README says so and that the fix is the missing
branch, and a test pins each of the three behaviors so the text cannot
drift from them. The json-gem child test shares the new run_ruby
helper.
Logger's own formatter inspects a message that is not a String, so
logger.info(opt) renders. ActiveSupport::TaggedLogging interpolates the
message once a tag is set, and Rails tags every production request with
its request_id, so the same call passes in tests and raises in
production. The README and the 0.9.1 upgrading note say to write
logger.info(opt.inspect), and a test pins both loggers' behavior.
…26-09-10-errgonomic-0.9.x-triage/value-is-not-a-slot
The changelog and README said every instance is frozen, but a copy
that skips construction is not: dup, Marshal.load, a YAML load and
ActiveSupport's deep_dup all return an unfrozen instance, while clone
keeps it frozen. With no writer, such a copy changes only through
instance_variable_set. The wording now states that guarantee.
… into 2026-09-10-errgonomic-0.9.x-triage/deconstruct
The top-level Some, None, Ok and Err were the four classes, so a
paren-less None, Rust's spelling of the value, evaluated to the class
and its name reached a hostname, a JSON payload or a column with no
exception. An application's own class None reopened the gem's, and
one defined before the gem loaded was replaced with only a warning.

Each name is now an Errgonomic::VariantName: it matches as its class
in a pattern and a case/when, inspects as the class, and raises
SerializeError from to_s, to_json and as_json. Not being a class, an
application's class None or module Ok written after the gem loads
raises TypeError, and defining the names refuses a constant the
application already holds with a NameError. The classes themselves
are untouched, so their inspect, name and every message that names
them render as before.
A string or json column already refused a bare None through to_s, but
a boolean column cast it to true and an integer column to nil, and a
where or a bulk write bound it the same way. The Rails integration
unwraps an Option at every write, query and bulk boundary before a
column type sees it, so it refuses a bare variant name there too.
The README says a bare Some, None, Ok or Err is a name for patterns,
that it refuses to stand in for a value, that is_a? takes the full
class name, and how an application constant of the same name fails,
including the two forms that only warn or defer: an assignment after
the gem loads, and a file Zeitwerk skips. The 0.10.0 changelog says
the same.
The doctests for a value that falls through the two-branch patterns
asserted only the error's class, so they could not show that its
message refuses to print when the subject is an Option or a Result.
Each now shows the message of a plain subject, and a second example
shows a wrapper subject whose message raises SerializeError. The
case/in test from 0.9.1 takes the Rust-shaped patterns.
kind_of?, instance_of?, .new, <=, .name and Minitest's assert_kind_of
all raise on Some/None/Ok/Err the same way is_a? does, since none of
them go through anything but Object's own type machinery. Spell each
against the fully qualified class or the some?/none?/ok?/err? readers
instead, and pin one case as a doctest.
@nz
nz force-pushed the 2026-09-10-errgonomic-0.9.x-triage/deconstruct branch from c8020f2 to 19afc3b Compare September 10, 2026 18:31
@nz
nz marked this pull request as ready for review September 10, 2026 18:32
@nz
nz enabled auto-merge September 10, 2026 18:32
lutzcc1
lutzcc1 previously approved these changes Sep 10, 2026
@nz
nz dismissed lutzcc1’s stale review September 10, 2026 19:47

The merge-base changed after approval.

lutzcc1
lutzcc1 previously approved these changes Sep 10, 2026
@nz
nz dismissed lutzcc1’s stale review September 10, 2026 19:56

The merge-base changed after approval.

@nz
nz disabled auto-merge September 10, 2026 20:15
@nz

nz commented Sep 10, 2026

Copy link
Copy Markdown
Member Author

Superseded by #87, which landed this release's commits on main as a linear history (merged 2026-09-10).

@nz nz closed this Sep 10, 2026
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.

deconstruct returns [self, value] so Rust-shaped Some(v) patterns silently miss; no deconstruct_keys

2 participants