Skip to content

Commit 0959811

Browse files
fix: resolve clippy warnings
1 parent 39a6400 commit 0959811

5 files changed

Lines changed: 87 additions & 51 deletions

File tree

‎crates/pixl-cli/src/main.rs‎

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,8 +79,7 @@ fn read_image(path: &str, limits: Limits) -> Result<(Image, Vec<u8>), String> {
7979
let bytes = std::fs::read(path).map_err(|e| format!("{path}: {e}"))?;
8080
let format = format_for_path(path, &bytes)
8181
.ok_or_else(|| format!("{path}: cannot detect image format"))?;
82-
let image = decode_with_format(&bytes, format, limits)
83-
.map_err(|e| format!("{path}: {e}"))?;
82+
let image = decode_with_format(&bytes, format, limits).map_err(|e| format!("{path}: {e}"))?;
8483
Ok((image, bytes))
8584
}
8685

@@ -116,7 +115,10 @@ fn compare(args: &[String]) -> Result<(), String> {
116115
if (a.width(), a.height()) != (b.width(), b.height()) {
117116
return Err(format!(
118117
"cannot compare images with different dimensions: {}x{} vs {}x{}",
119-
a.width(), a.height(), b.width(), b.height()
118+
a.width(),
119+
a.height(),
120+
b.width(),
121+
b.height()
120122
));
121123
}
122124
let mut different = 0u64;
@@ -184,7 +186,11 @@ fn atomic_write(output: &Path, bytes: &[u8]) -> std::io::Result<()> {
184186
for _ in 0..32 {
185187
let id = NEXT_ID.fetch_add(1, Ordering::Relaxed);
186188
let candidate = parent.join(format!(".pixl-{}-{id}.tmp", std::process::id()));
187-
match OpenOptions::new().write(true).create_new(true).open(&candidate) {
189+
match OpenOptions::new()
190+
.write(true)
191+
.create_new(true)
192+
.open(&candidate)
193+
{
188194
Ok(created) => {
189195
temp_path = Some(candidate);
190196
file = Some(created);
@@ -200,9 +206,8 @@ fn atomic_write(output: &Path, bytes: &[u8]) -> std::io::Result<()> {
200206
"could not reserve a temporary output file",
201207
)
202208
})?;
203-
let mut file = file.ok_or_else(|| {
204-
std::io::Error::other("temporary output file handle was not created")
205-
})?;
209+
let mut file =
210+
file.ok_or_else(|| std::io::Error::other("temporary output file handle was not created"))?;
206211
let result = (|| {
207212
file.write_all(bytes)?;
208213
file.sync_all()?;

‎crates/pixl-cli/tests/cli.rs‎

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,11 @@ fn identify_decodes_tga_from_extension() {
148148
let image = solid(3, 2, [10, 20, 30, 255]);
149149
fs::write(&input, encode(&image, Format::Tga).unwrap()).unwrap();
150150
let out = pixl().arg("identify").arg(&input).output().unwrap();
151-
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
151+
assert!(
152+
out.status.success(),
153+
"{}",
154+
String::from_utf8_lossy(&out.stderr)
155+
);
152156
let text = String::from_utf8_lossy(&out.stdout);
153157
assert!(text.contains("3x2 tga"), "{text}");
154158
}
@@ -166,7 +170,11 @@ fn output_replaces_existing_file() {
166170
.arg(&output)
167171
.output()
168172
.unwrap();
169-
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
173+
assert!(
174+
out.status.success(),
175+
"{}",
176+
String::from_utf8_lossy(&out.stderr)
177+
);
170178
let decoded = decode(&fs::read(&output).unwrap(), Limits::default()).unwrap();
171179
let pixel = decoded.pixel(0, 0).unwrap();
172180
assert_eq!(pixel[0], pixel[1]);
@@ -184,7 +192,11 @@ fn input_can_also_be_the_output_path() {
184192
.arg(&path)
185193
.output()
186194
.unwrap();
187-
assert!(out.status.success(), "{}", String::from_utf8_lossy(&out.stderr));
195+
assert!(
196+
out.status.success(),
197+
"{}",
198+
String::from_utf8_lossy(&out.stderr)
199+
);
188200
let decoded = decode(&fs::read(&path).unwrap(), Limits::default()).unwrap();
189201
let pixel = decoded.pixel(0, 0).unwrap();
190202
assert_eq!(pixel[0], pixel[1]);

‎crates/pixl-codecs/src/lib.rs‎

Lines changed: 19 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -137,11 +137,7 @@ pub fn decode(bytes: &[u8], limits: Limits) -> Result<Image, Error> {
137137
}
138138

139139
/// decodes bytes using an explicitly supplied format, including formats without magic bytes.
140-
pub fn decode_with_format(
141-
bytes: &[u8],
142-
format: Format,
143-
limits: Limits,
144-
) -> Result<Image, Error> {
140+
pub fn decode_with_format(bytes: &[u8], format: Format, limits: Limits) -> Result<Image, Error> {
145141
match format {
146142
Format::Qoi => decode_qoi(bytes, limits),
147143
_ => decode_image(bytes, format, limits),
@@ -155,8 +151,7 @@ fn decode_image(bytes: &[u8], format: Format, limits: Limits) -> Result<Image, E
155151
let (width, height) = dimension_reader
156152
.into_dimensions()
157153
.map_err(|e| Error::Decode(e.to_string()))?;
158-
validate_dimensions(width, height, PixelFormat::Rgba8, limits)
159-
.map_err(Error::Limit)?;
154+
validate_dimensions(width, height, PixelFormat::Rgba8, limits).map_err(Error::Limit)?;
160155

161156
let mut reader = ImageReader::with_format(Cursor::new(bytes), image_format);
162157
reader.limits(decoder_limits(limits));
@@ -196,11 +191,7 @@ pub fn encode(image: &Image, format: Format) -> Result<Vec<u8>, Error> {
196191
}
197192

198193
/// encodes an image while enforcing conversion limits.
199-
pub fn encode_with_limits(
200-
image: &Image,
201-
format: Format,
202-
limits: Limits,
203-
) -> Result<Vec<u8>, Error> {
194+
pub fn encode_with_limits(image: &Image, format: Format, limits: Limits) -> Result<Vec<u8>, Error> {
204195
match format {
205196
Format::Qoi => {
206197
let rgba = image.try_to_rgba8(limits).map_err(Error::Limit)?;
@@ -349,18 +340,31 @@ mod tests {
349340
let bytes = encode(&img, Format::Pnm).unwrap();
350341
let decoded = decode(&bytes, Limits::default()).unwrap();
351342
assert_eq!((decoded.width(), decoded.height()), (4, 3));
352-
assert!(decoded.as_bytes().chunks_exact(4).all(|pixel| pixel[3] == 255));
343+
assert!(
344+
decoded
345+
.as_bytes()
346+
.as_chunks::<4>()
347+
.0
348+
.iter()
349+
.all(|pixel| pixel[3] == 255)
350+
);
353351
}
354352

355353
#[test]
356354
fn ico_is_decode_only() {
357-
assert_eq!(encode(&test_image(), Format::Ico), Err(Error::UnsupportedFormat));
355+
assert_eq!(
356+
encode(&test_image(), Format::Ico),
357+
Err(Error::UnsupportedFormat)
358+
);
358359
}
359360

360361
#[test]
361362
fn rejects_pixel_limit_before_decode() {
362363
let bytes = encode(&test_image(), Format::Png).unwrap();
363-
let limits = Limits { max_dimension: 10, max_pixels: 4 };
364+
let limits = Limits {
365+
max_dimension: 10,
366+
max_pixels: 4,
367+
};
364368
assert!(matches!(decode(&bytes, limits), Err(Error::Limit(_))));
365369
}
366370

‎crates/pixl-core/src/lib.rs‎

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -475,7 +475,10 @@ mod tests {
475475
#[test]
476476
fn fallible_clone_enforces_limits() {
477477
let image = Image::new(2, 2, PixelFormat::Rgb8, Limits::default()).unwrap();
478-
let limits = Limits { max_dimension: 1, max_pixels: 1 };
478+
let limits = Limits {
479+
max_dimension: 1,
480+
max_pixels: 1,
481+
};
479482
assert!(matches!(
480483
image.try_clone(limits),
481484
Err(Error::DimensionExceeded { .. })
@@ -485,7 +488,10 @@ mod tests {
485488
#[test]
486489
fn fallible_conversion_enforces_limits() {
487490
let image = Image::new(2, 2, PixelFormat::Rgb8, Limits::default()).unwrap();
488-
let limits = Limits { max_dimension: 1, max_pixels: 1 };
491+
let limits = Limits {
492+
max_dimension: 1,
493+
max_pixels: 1,
494+
};
489495
assert_eq!(
490496
image.try_to_rgba8(limits),
491497
Err(Error::DimensionExceeded {
@@ -497,14 +503,8 @@ mod tests {
497503

498504
#[test]
499505
fn converts_la8_to_rgb_on_white() {
500-
let image = Image::from_data(
501-
1,
502-
1,
503-
PixelFormat::La8,
504-
vec![100, 128],
505-
Limits::default(),
506-
)
507-
.unwrap();
506+
let image =
507+
Image::from_data(1, 1, PixelFormat::La8, vec![100, 128], Limits::default()).unwrap();
508508
let rgb = image.try_to_rgb8_on_white(Limits::default()).unwrap();
509509
assert_eq!(rgb.as_bytes(), &[177, 177, 177]);
510510
}

‎crates/pixl-ops/src/lib.rs‎

Lines changed: 31 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -89,7 +89,9 @@ pub fn resize(
8989
}
9090
}
9191
let mut dst = images::Image::new(width, height, PixelType::U8x4);
92-
let options = ResizeOptions::new().resize_alg(filter.alg()).use_alpha(false);
92+
let options = ResizeOptions::new()
93+
.resize_alg(filter.alg())
94+
.use_alpha(false);
9395
let mut resizer = Resizer::new();
9496
resizer
9597
.resize(&src, &mut dst, &options)
@@ -297,7 +299,9 @@ fn luma(r: u8, g: u8, b: u8) -> u8 {
297299
/// adjusts brightness by adding `amount` to each color channel.
298300
pub fn brightness(image: &Image, amount: i16, limits: Limits) -> Result<Image, Error> {
299301
if !(-255..=255).contains(&amount) {
300-
return Err(Error::InvalidArgument("brightness must be -255..255".into()));
302+
return Err(Error::InvalidArgument(
303+
"brightness must be -255..255".into(),
304+
));
301305
}
302306
map_channels(image, limits, |v, _| {
303307
clamp_i16(i32::from(v) + i32::from(amount))
@@ -421,8 +425,8 @@ pub fn gaussian_blur(image: &Image, sigma: f64, limits: Limits) -> Result<Image,
421425
for x in 0..w as usize {
422426
let mut acc = [0.0; 4];
423427
for (k, weight) in weights.iter().enumerate() {
424-
let sy = (y as i64 + k as i64 - i64::from(radius))
425-
.clamp(0, i64::from(h) - 1) as usize;
428+
let sy = (y as i64 + k as i64 - i64::from(radius)).clamp(0, i64::from(h) - 1)
429+
as usize;
426430
let offset = sy * w as usize * 4 + x * 4;
427431
for c in 0..4 {
428432
acc[c] += f64::from(horizontal[offset + c]) * weight;
@@ -478,8 +482,13 @@ pub fn composite(
478482
require_rgba(overlay)?;
479483
validate_dimensions(base.width(), base.height(), PixelFormat::Rgba8, limits)
480484
.map_err(Error::Limit)?;
481-
validate_dimensions(overlay.width(), overlay.height(), PixelFormat::Rgba8, limits)
482-
.map_err(Error::Limit)?;
485+
validate_dimensions(
486+
overlay.width(),
487+
overlay.height(),
488+
PixelFormat::Rgba8,
489+
limits,
490+
)
491+
.map_err(Error::Limit)?;
483492
let mut out = base.try_clone(limits).map_err(Error::Limit)?;
484493
let bw = base.width() as i64;
485494
let bh = base.height() as i64;
@@ -537,16 +546,16 @@ pub fn append(base: &Image, other: &Image, axis: Axis, limits: Limits) -> Result
537546
require_rgba(other)?;
538547
let (width, height) = match axis {
539548
Axis::Horizontal => (
540-
base.width().checked_add(other.width()).ok_or_else(|| {
541-
Error::InvalidArgument("appended width overflows".into())
542-
})?,
549+
base.width()
550+
.checked_add(other.width())
551+
.ok_or_else(|| Error::InvalidArgument("appended width overflows".into()))?,
543552
base.height().max(other.height()),
544553
),
545554
Axis::Vertical => (
546555
base.width().max(other.width()),
547-
base.height().checked_add(other.height()).ok_or_else(|| {
548-
Error::InvalidArgument("appended height overflows".into())
549-
})?,
556+
base.height()
557+
.checked_add(other.height())
558+
.ok_or_else(|| Error::InvalidArgument("appended height overflows".into()))?,
550559
),
551560
};
552561
let mut out = empty_like(width, height, limits)?;
@@ -751,7 +760,7 @@ mod tests {
751760
img.as_bytes_mut()[..4].copy_from_slice(&[255, 0, 0, 0]);
752761
img.as_bytes_mut()[4..8].copy_from_slice(&[0, 0, 255, 255]);
753762
let out = gaussian_blur(&img, 1.0, Limits::default()).unwrap();
754-
for pixel in out.as_bytes().chunks_exact(4) {
763+
for pixel in out.as_bytes().as_chunks::<4>().0 {
755764
if pixel[3] > 0 {
756765
assert_eq!(pixel[0], 0);
757766
assert!(pixel[2] > 0);
@@ -765,7 +774,7 @@ mod tests {
765774
img.as_bytes_mut()[..4].copy_from_slice(&[255, 0, 0, 0]);
766775
img.as_bytes_mut()[4..8].copy_from_slice(&[0, 0, 255, 255]);
767776
let out = resize(&img, 8, 1, Filter::Bilinear, Limits::default()).unwrap();
768-
for pixel in out.as_bytes().chunks_exact(4) {
777+
for pixel in out.as_bytes().as_chunks::<4>().0 {
769778
if pixel[3] > 0 {
770779
assert_eq!(pixel[0], 0);
771780
assert!(pixel[2] > 0);
@@ -799,7 +808,10 @@ mod tests {
799808
fn composite_enforces_limits_for_both_images() {
800809
let base = solid(2, 2, [0, 0, 0, 255]);
801810
let overlay = solid(1, 1, [0, 0, 0, 255]);
802-
let limits = Limits { max_dimension: 1, max_pixels: 1 };
811+
let limits = Limits {
812+
max_dimension: 1,
813+
max_pixels: 1,
814+
};
803815
assert!(composite(&base, &overlay, 0, 0, limits).is_err());
804816
assert!(composite(&overlay, &base, 0, 0, limits).is_err());
805817
}
@@ -817,7 +829,10 @@ mod tests {
817829
let base = solid(1, 1, [10, 20, 30, 255]);
818830
let overlay = solid(1, 1, [200, 100, 50, 255]);
819831
assert!(composite(&base, &overlay, i64::MAX, i64::MAX, Limits::default()).is_ok());
820-
assert_eq!(composite(&base, &overlay, i64::MAX, 0, Limits::default()).unwrap(), base);
832+
assert_eq!(
833+
composite(&base, &overlay, i64::MAX, 0, Limits::default()).unwrap(),
834+
base
835+
);
821836
}
822837

823838
#[test]

0 commit comments

Comments
 (0)