Skip to content

Commit b510495

Browse files
committed
Validate Encoder effort eagerly at construction time
`Encoder::new` now validates effort (must be 1..=10) at construction, matching the existing `decoding_speed` validation, instead of only failing later in call_inner when it maps effort to an `EncoderSpeed`. Error message states the valid range. Added tests covering effort=0/11 (raise ValueError) and effort=10 (succeeds).
1 parent 9d817d1 commit b510495

2 files changed

Lines changed: 20 additions & 0 deletions

File tree

src/encode.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -100,6 +100,11 @@ impl Encoder {
100100
}
101101
};
102102

103+
let effort = match effort {
104+
1..=10 => effort,
105+
_ => return Err(PyValueError::new_err("Effort must be between 1 and 10")),
106+
};
107+
103108
let use_original_profile = match lossless {
104109
true => true,
105110
false => use_original_profile,

test/test_plugin.py

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,21 @@ def test_debug_mode():
1515
assert exit_code == 0
1616

1717

18+
@pytest.mark.parametrize("effort", [0, 11])
19+
def test_encode_invalid_effort_raises(effort):
20+
img = Image.open("test/images/sample.png")
21+
temp = tempfile.mktemp(suffix=".jxl")
22+
with pytest.raises(ValueError):
23+
img.save(temp, effort=effort)
24+
25+
26+
def test_encode_boundary_effort_succeeds():
27+
img = Image.open("test/images/sample.png")
28+
temp = tempfile.mktemp(suffix=".jxl")
29+
img.save(temp, effort=10)
30+
assert Image.open(temp).size == img.size
31+
32+
1833
def test_decode():
1934
img_jxl = Image.open("test/images/sample.jxl")
2035
img_png = Image.open("test/images/sample.png")

0 commit comments

Comments
 (0)