Skip to content

Commit 83ace81

Browse files
ss2165aborgna-q
andauthored
feat(core): builder pattern for EnvelopeConfig (#2330)
non exhaustive structs require constructors to be usable outside the crate --------- Co-authored-by: Agustín Borgna <121866228+aborgna-q@users.noreply.github.com>
1 parent c3a5c14 commit 83ace81

File tree

2 files changed

+31
-18
lines changed

2 files changed

+31
-18
lines changed

hugr-core/src/envelope/header.rs

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,7 @@ impl EnvelopeFormat {
9595
}
9696

9797
/// Configuration for encoding an envelope.
98-
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
98+
#[derive(Clone, Copy, Debug, Default, Eq, PartialEq)]
9999
#[non_exhaustive]
100100
pub struct EnvelopeConfig {
101101
/// The format to use for the payload.
@@ -105,19 +105,29 @@ pub struct EnvelopeConfig {
105105
pub zstd: Option<ZstdConfig>,
106106
}
107107

108-
impl Default for EnvelopeConfig {
109-
fn default() -> Self {
110-
let format = Default::default();
111-
let zstd = if cfg!(feature = "zstd") {
112-
Some(ZstdConfig::default())
113-
} else {
114-
None
115-
};
116-
Self { format, zstd }
108+
impl EnvelopeConfig {
109+
/// Create a new envelope configuration with the specified format.
110+
/// `zstd` compression is disabled by default.
111+
pub fn new(format: EnvelopeFormat) -> Self {
112+
Self {
113+
format,
114+
..Default::default()
115+
}
116+
}
117+
118+
/// Set the zstd compression configuration for the envelope.
119+
pub fn with_zstd(self, zstd: ZstdConfig) -> Self {
120+
Self {
121+
zstd: Some(zstd),
122+
..self
123+
}
124+
}
125+
126+
/// Disable zstd compression in the envelope configuration.
127+
pub fn disable_compression(self) -> Self {
128+
Self { zstd: None, ..self }
117129
}
118-
}
119130

120-
impl EnvelopeConfig {
121131
/// Create a new envelope header with the specified configuration.
122132
pub(super) fn make_header(&self) -> EnvelopeHeader {
123133
EnvelopeHeader {
@@ -162,6 +172,12 @@ pub struct ZstdConfig {
162172
}
163173

164174
impl ZstdConfig {
175+
/// Create a new zstd configuration with the specified compression level.
176+
pub fn new(level: u8) -> Self {
177+
Self {
178+
level: NonZeroU8::new(level),
179+
}
180+
}
165181
/// Create a new zstd configuration with default compression level.
166182
#[must_use]
167183
pub const fn default_level() -> Self {

hugr/benches/benchmarks/hugr.rs

Lines changed: 3 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -19,9 +19,7 @@ trait Serializer {
1919
struct JsonSer;
2020
impl Serializer for JsonSer {
2121
fn serialize(&self, hugr: &Hugr) -> Vec<u8> {
22-
let mut cfg = EnvelopeConfig::default();
23-
cfg.format = EnvelopeFormat::PackageJson;
24-
cfg.zstd = None;
22+
let cfg = EnvelopeConfig::new(EnvelopeFormat::PackageJson).disable_compression();
2523

2624
let mut bytes = Vec::new();
2725
hugr.store(&mut bytes, cfg).unwrap();
@@ -36,9 +34,8 @@ struct CapnpSer;
3634

3735
impl Serializer for CapnpSer {
3836
fn serialize(&self, hugr: &Hugr) -> Vec<u8> {
39-
let mut cfg = EnvelopeConfig::default();
40-
cfg.format = EnvelopeFormat::ModelWithExtensions;
41-
cfg.zstd = Some(Default::default());
37+
let cfg =
38+
EnvelopeConfig::new(EnvelopeFormat::ModelWithExtensions).with_zstd(Default::default());
4239

4340
let mut bytes = Vec::new();
4441
hugr.store(&mut bytes, cfg).unwrap();

0 commit comments

Comments
 (0)