Skip to content

Add builder pattern for Config - #366

Closed
LonerDan wants to merge 1 commit into
tiberius-rs:mainfrom
LonerDan:config-builder
Closed

LonerDan wants to merge 1 commit into
tiberius-rs:mainfrom
LonerDan:config-builder

Conversation

@LonerDan

Copy link
Copy Markdown

Changes the way Config is constructed by introducing helper struct ConfigBuilder, which follows the builder pattern. This enables more ergonomic function call chaining, while still preserving the option to call them one by one as previously.

Based on this and this, the variant consuming and producing mutable references was chosen, since it allows both ergonomic function call chaining and manually calling individual functions, which is required in case of values set conditionally.
As a side effect, this also allows constructing multiple Configs from the same builder.

Changes the way `Config` is contructed by introducing helper struct `ConfigBuilder`, which follows the builder pattern. This enables more ergonomic function call chaining, while still preserving the option to call them one by one as previously.
joelparkerhenderson added a commit to mssql-rust/mssql-rust that referenced this pull request Aug 29, 2026
Mirrors the idea of tiberius-rs/tiberius#366, but not its diff: as tasks.md
flagged, the upstream PR outright deleted `Config::new()` and every one
of its setters, replacing them with a builder-only API - a breaking
change for essentially every existing caller (including this fork's own
docs, examples, and every test in the suite). Reimplemented from scratch
as a pure addition instead: `Config::new()` and all of its setters are
completely untouched.

`Config::builder()` returns a new `ConfigBuilder` wrapping a `Config`,
with one chainable (`&mut Self`-returning) method per existing setter -
covering every setter this fork now has, including the ones added this
session that didn't exist when #366 was opened (`client_name`,
`host_name_in_certificate`, `send_string_parameters_as_unicode`,
`multi_subnet_failover`, `packet_size`). Each builder method just calls
straight through to the corresponding `Config` method, so there is
exactly one implementation of each setting's logic. `packet_size` is the
one fallible setter (range-validated, added this session in #400) - its
builder method returns `crate::Result<&mut Self>` instead of `&mut Self`,
so chaining continues normally with `?` on success. `build(&self)` clones
the wrapped `Config` rather than consuming the builder, so a builder can
be reused as a shared base for producing several slightly different
`Config`s (matching the shape of the original PR's `build`, which was
worth keeping even though the rest of that PR wasn't).

Added unit tests confirming: `Config::new()` and its direct setters still
compile and work unmodified, the builder produces a `Config` equivalent
to the direct-setter path for the same values, the builder remains usable
after `build()`, and `packet_size`'s validation/chaining behavior.

Verified:
- cargo check: default, --features=rustls, --features=vendored-openssl,
  --no-default-features
- cargo clippy --all-targets -- -D warnings, for default and rustls
- cargo fmt --check
- cargo test --lib (181 passed)
- cargo test --doc against a live server (28 passed, including the new
  ConfigBuilder doctest)
- Live integration tests over rustls: 305 passed (102 bulk + 203 query),
  unaffected since this is purely additive

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0156Di1tRRLsJK8ctU1AmAJr
MattJackson referenced this pull request in MattJackson/tiberius-ng Aug 29, 2026
Adapted from upstream #366 (author @LonerDan), non-breaking (existing
Config API retained).
joelparkerhenderson added a commit to mssql-rust/mssql-rust that referenced this pull request Aug 30, 2026
Mirrors the idea of tiberius-rs/tiberius#366, but not its diff: as tasks.md
flagged, the upstream PR outright deleted `Config::new()` and every one
of its setters, replacing them with a builder-only API - a breaking
change for essentially every existing caller (including this fork's own
docs, examples, and every test in the suite). Reimplemented from scratch
as a pure addition instead: `Config::new()` and all of its setters are
completely untouched.

`Config::builder()` returns a new `ConfigBuilder` wrapping a `Config`,
with one chainable (`&mut Self`-returning) method per existing setter -
covering every setter this fork now has, including the ones added this
session that didn't exist when #366 was opened (`client_name`,
`host_name_in_certificate`, `send_string_parameters_as_unicode`,
`multi_subnet_failover`, `packet_size`). Each builder method just calls
straight through to the corresponding `Config` method, so there is
exactly one implementation of each setting's logic. `packet_size` is the
one fallible setter (range-validated, added this session in #400) - its
builder method returns `crate::Result<&mut Self>` instead of `&mut Self`,
so chaining continues normally with `?` on success. `build(&self)` clones
the wrapped `Config` rather than consuming the builder, so a builder can
be reused as a shared base for producing several slightly different
`Config`s (matching the shape of the original PR's `build`, which was
worth keeping even though the rest of that PR wasn't).

Added unit tests confirming: `Config::new()` and its direct setters still
compile and work unmodified, the builder produces a `Config` equivalent
to the direct-setter path for the same values, the builder remains usable
after `build()`, and `packet_size`'s validation/chaining behavior.

Verified:
- cargo check: default, --features=rustls, --features=vendored-openssl,
  --no-default-features
- cargo clippy --all-targets -- -D warnings, for default and rustls
- cargo fmt --check
- cargo test --lib (181 passed)
- cargo test --doc against a live server (28 passed, including the new
  ConfigBuilder doctest)
- Live integration tests over rustls: 305 passed (102 bulk + 203 query),
  unaffected since this is purely additive

Co-Authored-By: Claude Sonnet 5 <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_0156Di1tRRLsJK8ctU1AmAJr
MattJackson added a commit that referenced this pull request Sep 3, 2026
Adapted from upstream #366 (author @LonerDan), non-breaking (existing
Config API retained).
MattJackson added a commit that referenced this pull request Sep 4, 2026
Adapted from upstream #366 (author @LonerDan), non-breaking (existing
Config API retained).
MattJackson added a commit that referenced this pull request Sep 6, 2026
Adapted from upstream #366 (author @LonerDan), non-breaking (existing
Config API retained).
MattJackson added a commit that referenced this pull request Sep 16, 2026
Adapted from upstream #366 (author @LonerDan), non-breaking (existing
Config API retained).
MattJackson added a commit that referenced this pull request Sep 24, 2026
Adapted from upstream #366 (author @LonerDan), non-breaking (existing
Config API retained).
@MattJackson

Copy link
Copy Markdown
Contributor

Included in #442 — shipped in tiberius v0.13.0. Thanks for this! Closing.

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