imgforge 0.17.0

Fast and secure image proxy and transformation server
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
1171
1172
1173
1174
1175
1176
1177
1178
1179
1180
1181
1182
1183
1184
1185
1186
1187
1188
1189
1190
1191
1192
1193
1194
1195
1196
1197
1198
1199
1200
1201
1202
1203
1204
1205
1206
1207
1208
1209
1210
1211
1212
1213
1214
1215
1216
1217
1218
1219
1220
1221
1222
1223
1224
1225
1226
1227
1228
1229
1230
1231
1232
1233
1234
1235
1236
1237
1238
1239
1240
1241
1242
1243
1244
1245
1246
1247
1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
//! Image processing module for imgforge.
//! This module contains functions and structs for parsing image processing options
//! and applying various transformations to images.

/// Represents a single image processing option from the URL path.
#[derive(Debug, Clone)]
pub struct ProcessingOption {
    /// The name of the processing option (e.g., "resize", "quality").
    pub name: String,
    /// Arguments for the processing option.
    pub args: Vec<String>,
}
use crate::limits::{MaxResultDimension, MaxSourceFileSize, MaxSourceResolution};
use base64::engine::general_purpose;
use base64::Engine as _;
use std::collections::HashMap;
use std::str::FromStr;
use thiserror::Error;
use tracing::debug;

/// Errors produced while parsing image processing options.
#[derive(Debug, Error)]
#[non_exhaustive]
pub enum OptionParseError {
    #[error("invalid {option} value {value:?}")]
    Integer {
        option: String,
        value: String,
        #[source]
        source: std::num::ParseIntError,
    },
    #[error("invalid {option} value {value:?}")]
    Float {
        option: String,
        value: String,
        #[source]
        source: std::num::ParseFloatError,
    },
    #[error("invalid Base64 for {option}")]
    Base64 {
        option: String,
        #[source]
        source: base64::DecodeError,
    },
    #[error("invalid UTF-8 for {option}")]
    Utf8 {
        option: String,
        #[source]
        source: std::string::FromUtf8Error,
    },
    #[error("invalid {option}: {source}")]
    SecurityLimit {
        option: String,
        #[source]
        source: crate::limits::SecurityLimitError,
    },
    #[error("invalid background color")]
    Color(#[source] super::utils::ColorParseError),
    #[error("{0}")]
    InvalidValue(String),
}

impl OptionParseError {
    fn invalid(message: impl Into<String>) -> Self {
        Self::InvalidValue(message.into())
    }
}

fn parse_integer<T>(value: &str, option: &str) -> Result<T, OptionParseError>
where
    T: FromStr<Err = std::num::ParseIntError>,
{
    value.parse().map_err(|source| OptionParseError::Integer {
        option: option.to_string(),
        value: value.to_string(),
        source,
    })
}

fn parse_float(value: &str, option: &str) -> Result<f32, OptionParseError> {
    value.parse().map_err(|source| OptionParseError::Float {
        option: option.to_string(),
        value: value.to_string(),
        source,
    })
}

fn decode_base64(value: &str, option: &str) -> Result<Vec<u8>, OptionParseError> {
    general_purpose::URL_SAFE_NO_PAD
        .decode(value)
        .map_err(|source| OptionParseError::Base64 {
            option: option.to_string(),
            source,
        })
}

fn decode_utf8(value: Vec<u8>, option: &str) -> Result<String, OptionParseError> {
    String::from_utf8(value).map_err(|source| OptionParseError::Utf8 {
        option: option.to_string(),
        source,
    })
}

/// Option name for resizing.
const RESIZE: &str = "resize";
/// Shorthand for resize.
const RESIZE_SHORT: &str = "rs";
/// Option name for resizing type.
const RESIZING_TYPE: &str = "resizing_type";
/// Shorthand for resizing type.
const RESIZING_TYPE_SHORT: &str = "rt";
/// Option name for size.
const SIZE: &str = "size";
/// Shorthand for size.
const SIZE_SHORT: &str = "s";
/// Option name for width.
const WIDTH: &str = "width";
/// Shorthand for width.
const WIDTH_SHORT: &str = "w";
/// Option name for height.
const HEIGHT: &str = "height";
/// Shorthand for height.
const HEIGHT_SHORT: &str = "h";
/// Option name for gravity.
const GRAVITY: &str = "gravity";
/// Shorthand for gravity.
const GRAVITY_SHORT: &str = "g";
/// Option name for quality.
const QUALITY: &str = "quality";
/// Shorthand for quality.
const QUALITY_SHORT: &str = "q";
/// Option name for format-specific quality.
const FORMAT_QUALITY: &str = "format_quality";
/// Shorthand for format_quality.
const FORMAT_QUALITY_SHORT: &str = "fq";
/// Option name for auto_rotate.
const AUTO_ROTATE: &str = "auto_rotate";
/// Shorthand for auto_rotate.
const AUTO_ROTATE_SHORT: &str = "ar";
/// Option name for background.
const BACKGROUND: &str = "background";
/// Shorthand for background.
const BACKGROUND_SHORT: &str = "bg";
/// Option name for enlarge.
const ENLARGE: &str = "enlarge";
/// Shorthand for enlarge.
const ENLARGE_SHORT: &str = "el";
/// Option name for extend.
const EXTEND: &str = "extend";
/// Shorthand for extend.
const EXTEND_SHORT: &str = "ex";
/// Option name for padding.
const PADDING: &str = "padding";
/// Shorthand for padding.
const PADDING_SHORT: &str = "pd";
/// Option name for rotation.
const ROTATE: &str = "rotate";
/// Shorthand for rotation.
const ROTATE_SHORT: &str = "rot";
/// Option name for flip.
const FLIP: &str = "flip";
/// Shorthand for flip.
const FLIP_SHORT: &str = "fl";
/// Option name for raw.
const RAW: &str = "raw";
/// Option name for blur.
const BLUR: &str = "blur";
/// Shorthand for blur.
const BLUR_SHORT: &str = "bl";
/// Option name for crop.
const CROP: &str = "crop";
/// Shorthand for crop.
const CROP_SHORT: &str = "c";
/// Option name for format.
const FORMAT: &str = "format";
/// Shorthand for format.
const FORMAT_SHORT: &str = "f";
/// Alternate shorthand for format.
const FORMAT_EXT: &str = "ext";
/// Option name for max_src_resolution.
const MAX_SRC_RESOLUTION: &str = "max_src_resolution";
/// Shorthand for max_src_resolution.
const MAX_SRC_RESOLUTION_SHORT: &str = "msr";
/// Option name for trim.
const TRIM: &str = "trim";
/// Shorthand for trim.
const TRIM_SHORT: &str = "t";
/// Option name for max_result_dimension.
const MAX_RESULT_DIMENSION: &str = "max_result_dimension";
/// Shorthand for max_result_dimension.
const MAX_RESULT_DIMENSION_SHORT: &str = "mrd";
/// Option name for max_src_file_size.
const MAX_SRC_FILE_SIZE: &str = "max_src_file_size";
/// Shorthand for max_src_file_size.
const MAX_SRC_FILE_SIZE_SHORT: &str = "msfs";
/// Option name for cache buster.
const CACHEBUSTER: &str = "cachebuster";
/// Shorthand for cachebuster.
const CACHEBUSTER_SHORT: &str = "cb";
/// Option name for dpr.
const DPR: &str = "dpr";
/// Option name for min-width.
const MIN_WIDTH: &str = "min-width";
/// Shorthand for min_width.
const MIN_WIDTH_SHORT: &str = "mw";
/// Option name for min-height.
const MIN_HEIGHT: &str = "min-height";
/// Shorthand for min_height.
const MIN_HEIGHT_SHORT: &str = "mh";
/// Option name for zoom.
const ZOOM: &str = "zoom";
/// Shorthand for zoom.
const ZOOM_SHORT: &str = "z";
/// Option name for sharpen.
const SHARPEN: &str = "sharpen";
/// Shorthand for sharpen.
const SHARPEN_SHORT: &str = "sh";
/// Option name for pixelate.
const PIXELATE: &str = "pixelate";
/// Shorthand for pixelate.
const PIXELATE_SHORT: &str = "pix";
/// Option name for watermark.
const WATERMARK: &str = "watermark";
/// Shorthand for watermark.
const WATERMARK_SHORT: &str = "wm";
/// Option name for watermark_url.
const WATERMARK_URL: &str = "watermark_url";
/// Shorthand for watermark_url.
const WATERMARK_URL_SHORT: &str = "wmu";
/// Option name for resizing_algorithm.
const RESIZING_ALGORITHM: &str = "resizing_algorithm";
/// Shorthand for resizing_algorithm.
const RESIZING_ALGORITHM_SHORT: &str = "ra";
/// Option name for background_alpha.
const BACKGROUND_ALPHA: &str = "background_alpha";
/// Shorthand for background_alpha.
const BACKGROUND_ALPHA_SHORT: &str = "bga";
/// Option name for adjust.
const ADJUST: &str = "adjust";
/// Shorthand for adjust.
const ADJUST_SHORT: &str = "a";
/// Option name for brightness.
const BRIGHTNESS: &str = "brightness";
/// Shorthand for brightness.
const BRIGHTNESS_SHORT: &str = "br";
/// Option name for contrast.
const CONTRAST: &str = "contrast";
/// Shorthand for contrast.
const CONTRAST_SHORT: &str = "co";
/// Option name for saturation.
const SATURATION: &str = "saturation";
/// Shorthand for saturation.
const SATURATION_SHORT: &str = "sa";
/// Option name for max_bytes.
const MAX_BYTES: &str = "max_bytes";
/// Shorthand for max_bytes.
const MAX_BYTES_SHORT: &str = "mb";
/// Option name for strip_metadata.
const STRIP_METADATA: &str = "strip_metadata";
/// Shorthand for strip_metadata.
const STRIP_METADATA_SHORT: &str = "sm";
/// Option name for strip_color_profile.
const STRIP_COLOR_PROFILE: &str = "strip_color_profile";
/// Shorthand for strip_color_profile.
const STRIP_COLOR_PROFILE_SHORT: &str = "scp";
/// Option name for JPEG options.
const JPEG_OPTIONS: &str = "jpeg_options";
/// Shorthand for JPEG options.
const JPEG_OPTIONS_SHORT: &str = "jpgo";
/// Option name for PNG options.
const PNG_OPTIONS: &str = "png_options";
/// Shorthand for PNG options.
const PNG_OPTIONS_SHORT: &str = "pngo";
/// Option name for WebP options.
const WEBP_OPTIONS: &str = "webp_options";
/// Shorthand for WebP options.
const WEBP_OPTIONS_SHORT: &str = "webpo";
/// Option name for AVIF options.
const AVIF_OPTIONS: &str = "avif_options";
/// Shorthand for AVIF options.
const AVIF_OPTIONS_SHORT: &str = "avifo";
/// Option name for page.
const PAGE: &str = "page";
/// Shorthand for page.
const PAGE_SHORT: &str = "pg";
/// Option name for pages.
const PAGES: &str = "pages";
/// Shorthand for pages.
const PAGES_SHORT: &str = "pgs";
/// Option name for disable_animation.
const DISABLE_ANIMATION: &str = "disable_animation";
/// Shorthand for disable_animation.
const DISABLE_ANIMATION_SHORT: &str = "da";
/// Option name for skip_processing.
const SKIP_PROCESSING: &str = "skip_processing";
/// Shorthand for skip_processing.
const SKIP_PROCESSING_SHORT: &str = "skp";
/// Option name for expires.
const EXPIRES: &str = "expires";
/// Shorthand for expires.
const EXPIRES_SHORT: &str = "exp";
/// Option name for filename.
const FILENAME: &str = "filename";
/// Shorthand for filename.
const FILENAME_SHORT: &str = "fn";
/// Option name for return_attachment.
const RETURN_ATTACHMENT: &str = "return_attachment";
/// Shorthand for return_attachment.
const RETURN_ATTACHMENT_SHORT: &str = "att";

const VALID_ROTATIONS: [u16; 4] = [0, 90, 180, 270];
const VALID_RESIZING_TYPES: [&str; 4] = ["fill", "fit", "force", "auto"];

fn is_valid_rotation(rotation: u16) -> bool {
    VALID_ROTATIONS.contains(&rotation)
}

fn is_valid_resizing_type(resizing_type: &str) -> bool {
    VALID_RESIZING_TYPES.contains(&resizing_type)
}

fn parse_positive_f32(value: &str, option_name: &str) -> Result<f32, OptionParseError> {
    let parsed = parse_float(value, option_name)?;

    if !parsed.is_finite() || parsed <= 0.0 {
        return Err(OptionParseError::invalid(format!(
            "{} must be a finite positive number",
            option_name
        )));
    }

    Ok(parsed)
}

fn parse_unit_f32(value: &str, option_name: &str) -> Result<f32, OptionParseError> {
    let parsed = parse_float(value, option_name)?;

    if !parsed.is_finite() || !(0.0..=1.0).contains(&parsed) {
        return Err(OptionParseError::invalid(format!(
            "{} must be a finite number between 0 and 1",
            option_name
        )));
    }

    Ok(parsed)
}

fn parse_quality(value: &str, option_name: &str) -> Result<u8, OptionParseError> {
    Ok(parse_integer::<u8>(value, option_name)?.clamp(1, 100))
}

fn parse_optional_bool(args: &[String], index: usize) -> Option<bool> {
    args.get(index)
        .filter(|arg| !arg.is_empty())
        .map(|arg| super::utils::parse_boolean(arg))
}

/// Represents the parameters for a resize operation.
#[derive(Debug, Default)]
pub struct Resize {
    /// The type of resizing to perform (e.g., "fill", "fit", "force").
    pub resizing_type: String,
    /// The target width for the resize operation.
    pub width: u32,
    /// The target height for the resize operation.
    pub height: u32,
}

/// Controls how an image is aligned when cropping or extending.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum Gravity {
    Center,
    North,
    South,
    East,
    West,
    NorthEast,
    NorthWest,
    SouthEast,
    SouthWest,
}

impl Gravity {
    pub fn parse(value: &str) -> Option<Self> {
        match value {
            "ce" => Some(Self::Center),
            "no" => Some(Self::North),
            "so" => Some(Self::South),
            "ea" => Some(Self::East),
            "we" => Some(Self::West),
            "noea" => Some(Self::NorthEast),
            "nowe" => Some(Self::NorthWest),
            "soea" => Some(Self::SouthEast),
            "sowe" => Some(Self::SouthWest),
            _ => None,
        }
    }
}

/// Represents the parameters for a flip operation.
#[derive(Debug, Clone, Copy, Default)]
pub struct Flip {
    pub horizontal: bool,
    pub vertical: bool,
}

/// Represents the parameters for color adjustment.
#[derive(Debug, Clone, Copy)]
pub struct Adjust {
    pub brightness: i16,
    pub contrast: f32,
    pub saturation: f32,
}

impl Default for Adjust {
    fn default() -> Self {
        Self {
            brightness: 0,
            contrast: 1.0,
            saturation: 1.0,
        }
    }
}

/// Encoder-specific output controls.
#[derive(Debug, Clone, Default)]
pub struct SaveOptions {
    pub format_quality: HashMap<String, u8>,
    pub max_bytes: Option<usize>,
    pub strip_metadata: Option<bool>,
    pub strip_color_profile: Option<bool>,
    pub jpeg: JpegOptions,
    pub png: PngOptions,
    pub webp: WebpOptions,
    pub avif: AvifOptions,
}

/// JPEG encoder controls.
#[derive(Debug, Clone, Default)]
pub struct JpegOptions {
    pub progressive: Option<bool>,
    pub no_subsample: Option<bool>,
    pub trellis_quant: Option<bool>,
    pub overshoot_deringing: Option<bool>,
    pub optimize_scans: Option<bool>,
    pub quant_table: Option<i32>,
}

/// PNG encoder controls.
#[derive(Debug, Clone, Default)]
pub struct PngOptions {
    pub interlaced: Option<bool>,
    pub quantize: Option<bool>,
    pub quantization_colors: Option<u16>,
}

/// WebP encoder controls.
#[derive(Debug, Clone, Default)]
pub struct WebpOptions {
    pub lossless: Option<bool>,
    pub smart_subsample: Option<bool>,
    pub preset: Option<String>,
}

/// Border trimming controls.
#[derive(Debug, Clone, Copy)]
pub struct Trim {
    /// How far a pixel may differ from the background and still be trimmed.
    pub threshold: f64,
    /// Colour to treat as background. Detected from the top-left pixel when absent.
    pub color: Option<[u8; 4]>,
    /// Cut equal amounts from the left and right.
    pub equal_hor: bool,
    /// Cut equal amounts from the top and bottom.
    pub equal_ver: bool,
}

/// AVIF/HEIF encoder controls.
#[derive(Debug, Clone, Default)]
pub struct AvifOptions {
    pub no_subsample: Option<bool>,
}

/// Represents the parameters for a crop operation.
#[derive(Debug, Default)]
pub struct Crop {
    /// The x-coordinate of the top-left corner of the crop area.
    pub x: u32,
    /// The y-coordinate of the top-left corner of the crop area.
    pub y: u32,
    /// The width of the crop area.
    pub width: u32,
    /// The height of the crop area.
    pub height: u32,
    /// Optional crop gravity.
    pub gravity: Option<Gravity>,
}

/// Represents the parameters for a watermark operation.
#[derive(Debug, Clone, Default)]
pub struct Watermark {
    /// The opacity of the watermark.
    pub opacity: f32,
    /// The position of the watermark.
    pub position: String,
}

/// Holds all parsed image processing options.
#[derive(Debug)]
pub struct ParsedOptions {
    /// Optional resize operation parameters.
    pub resize: Option<Resize>,
    /// Optional blur sigma value.
    pub blur: Option<f32>,
    /// Optional crop operation parameters.
    pub crop: Option<Crop>,
    /// Optional output image format.
    pub format: Option<String>,
    /// Optional output image quality (1-100).
    pub quality: Option<u8>,
    /// Optional background color for transparent areas or extending.
    pub background: Option<[u8; 4]>, // RGBA array
    /// Optional target width (used with `resize` if no explicit resize type).
    pub width: Option<u32>,
    /// Optional target height (used with `resize` if no explicit resize type).
    pub height: Option<u32>,
    /// Optional gravity for cropping or extending.
    pub gravity: Option<Gravity>,
    /// Whether to allow enlarging the image beyond its original dimensions.
    pub enlarge: bool,
    /// Whether to extend the image with a background if target dimensions are larger.
    pub extend: bool,
    /// Optional padding values (top, right, bottom, left).
    pub padding: Option<(u32, u32, u32, u32)>,
    /// Optional image rotation (rotation angle).
    pub rotation: Option<u16>,
    /// Optional flip operation.
    pub flip: Option<Flip>,
    /// Whether to automatically rotate the image based on EXIF data.
    pub auto_rotate: bool,
    /// Whether to bypass processing limits (e.g., worker limits).
    pub raw: bool,
    /// Maximum allowed source image resolution in megapixels.
    pub max_src_resolution: Option<MaxSourceResolution>,
    /// Ceiling for either dimension of the processed image.
    pub max_result_dimension: Option<MaxResultDimension>,
    /// Border trimming, applied before crop and resize.
    pub trim: Option<Trim>,
    /// Maximum allowed source image file size in bytes.
    pub max_src_file_size: Option<MaxSourceFileSize>,
    /// Value to bypass cache (e.g., timestamp).
    pub cache_buster: Option<String>,
    /// Optional unix timestamp after which the request expires.
    pub expires: Option<u64>,
    /// Optional response filename for Content-Disposition.
    pub filename: Option<String>,
    /// Whether to return Content-Disposition as attachment.
    pub return_attachment: bool,
    /// Device pixel ratio factor to scale up dimensions.
    pub dpr: Option<f32>,
    /// Minimum width for the image.
    pub min_width: Option<u32>,
    /// Minimum height for the image.
    pub min_height: Option<u32>,
    /// Zoom factor for the image.
    pub zoom: Option<f32>,
    /// Sharpen factor for the image.
    pub sharpen: Option<f32>,
    /// Pixelate factor for the image.
    pub pixelate: Option<u32>,
    pub watermark: Option<Watermark>,
    /// Optional URL for a watermark image.
    pub watermark_url: Option<String>,
    /// Resizing algorithm to use (nearest, linear, cubic, lanczos2, lanczos3).
    pub resizing_algorithm: Option<String>,
    /// Optional alpha value applied to background.
    pub background_alpha: Option<f32>,
    /// Optional color adjustments.
    pub adjust: Option<Adjust>,
    /// Encoder-specific output options.
    pub save: SaveOptions,
    /// Optional page number for multi-page sources.
    pub page: Option<u32>,
    /// Optional page count for multi-page sources.
    pub pages: Option<u32>,
    /// Whether to disable animation handling.
    pub disable_animation: bool,
    /// Source formats that may bypass processing when output format matches.
    pub skip_processing: Vec<String>,
}

impl Default for ParsedOptions {
    fn default() -> Self {
        Self {
            resize: None,
            blur: None,
            crop: None,
            format: None,
            quality: None,
            background: None,
            width: None,
            height: None,
            gravity: None,
            enlarge: false,
            extend: false,
            padding: None,
            rotation: None,
            flip: None,
            auto_rotate: true,
            raw: false,
            max_src_resolution: None,
            max_result_dimension: None,
            trim: None,
            max_src_file_size: None,
            cache_buster: None,
            expires: None,
            filename: None,
            return_attachment: false,
            dpr: Some(1.0),
            min_width: None,
            min_height: None,
            zoom: None,
            sharpen: None,
            pixelate: None,
            watermark: None,
            watermark_url: None,
            resizing_algorithm: Some("lanczos3".to_string()),
            background_alpha: None,
            adjust: None,
            save: SaveOptions::default(),
            page: None,
            pages: None,
            disable_animation: false,
            skip_processing: Vec::new(),
        }
    }
}

/// Parses a vector of `ProcessingOption` into a `ParsedOptions` struct.
///
/// This function iterates through the raw processing options, validates their arguments,
/// and converts them into a structured `ParsedOptions` object.
///
/// # Arguments
///
/// * `options` - A `Vec<ProcessingOption>` containing the raw options from the URL.
///
/// # Returns
///
/// A `Result` containing the `ParsedOptions` on success, or a typed parsing error.
pub fn parse_all_options(options: Vec<ProcessingOption>) -> Result<ParsedOptions, OptionParseError> {
    let mut parsed_options = ParsedOptions::default();

    for option in options {
        debug!("Parsing option: {} with args: {:?}", option.name, option.args);
        match option.name.as_str() {
            RESIZE | RESIZE_SHORT => {
                let mut store_resize = parsed_options.resize.is_some();
                let mut resize = parsed_options.resize.take().unwrap_or_default();

                if let Some(arg) = option.args.first() {
                    if !arg.is_empty() {
                        resize.resizing_type = arg.clone();
                        store_resize = true;
                    }
                }
                if let Some(arg) = option.args.get(1) {
                    if !arg.is_empty() {
                        resize.width = parse_integer(arg, "resize width")?;
                        store_resize = true;
                    }
                }
                if let Some(arg) = option.args.get(2) {
                    if !arg.is_empty() {
                        resize.height = parse_integer(arg, "resize height")?;
                        store_resize = true;
                    }
                }
                if let Some(arg) = option.args.get(3) {
                    if !arg.is_empty() {
                        parsed_options.enlarge = super::utils::parse_boolean(arg);
                    }
                }
                if let Some(arg) = option.args.get(4) {
                    if !arg.is_empty() {
                        parsed_options.extend = super::utils::parse_boolean(arg);
                    }
                }

                if store_resize {
                    parsed_options.resize = Some(resize);
                }
            }
            RESIZING_TYPE | RESIZING_TYPE_SHORT => {
                let resizing_type = option
                    .args
                    .first()
                    .filter(|value| !value.is_empty())
                    .ok_or_else(|| OptionParseError::invalid("resizing_type option requires one argument"))?;
                if !is_valid_resizing_type(resizing_type) {
                    return Err(OptionParseError::invalid(
                        "resizing_type must be one of: fill, fit, force, auto",
                    ));
                }
                parsed_options
                    .resize
                    .get_or_insert_with(Resize::default)
                    .resizing_type
                    .clone_from(resizing_type);
            }
            SIZE | SIZE_SHORT => {
                let mut store_resize = parsed_options.resize.is_some();
                let mut resize = parsed_options.resize.take().unwrap_or_default();
                let mut width_height_set = false;

                if let Some(arg) = option.args.first() {
                    if !arg.is_empty() {
                        resize.width = parse_integer(arg, "size width")?;
                        store_resize = true;
                        width_height_set = true;
                    }
                }
                if let Some(arg) = option.args.get(1) {
                    if !arg.is_empty() {
                        resize.height = parse_integer(arg, "size height")?;
                        store_resize = true;
                        width_height_set = true;
                    }
                }

                if let Some(arg) = option.args.get(2) {
                    if !arg.is_empty() {
                        parsed_options.enlarge = super::utils::parse_boolean(arg);
                    }
                }
                if let Some(arg) = option.args.get(3) {
                    if !arg.is_empty() {
                        parsed_options.extend = super::utils::parse_boolean(arg);
                    }
                }

                if store_resize && (width_height_set || resize.resizing_type.is_empty()) {
                    resize.resizing_type = "fit".to_string();
                }

                if store_resize {
                    parsed_options.resize = Some(resize);
                }
            }
            WIDTH | WIDTH_SHORT => {
                let width_arg = option.args.first().map(|s| s.as_str()).unwrap_or("0");
                let width = if width_arg.is_empty() {
                    0
                } else {
                    parse_integer(width_arg, "width")?
                };
                parsed_options.width = Some(width);
            }
            HEIGHT | HEIGHT_SHORT => {
                let height_arg = option.args.first().map(|s| s.as_str()).unwrap_or("0");
                let height = if height_arg.is_empty() {
                    0
                } else {
                    parse_integer(height_arg, "height")?
                };
                parsed_options.height = Some(height);
            }
            GRAVITY | GRAVITY_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("gravity option requires one argument"));
                }
                let gravity = option.args[0].as_str();
                let gravity = Gravity::parse(gravity).ok_or_else(|| {
                    OptionParseError::invalid("gravity must be one of: ce, no, so, ea, we, noea, nowe, soea, sowe")
                })?;
                parsed_options.gravity = Some(gravity);
            }
            ENLARGE | ENLARGE_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("enlarge option requires one argument"));
                }
                parsed_options.enlarge = super::utils::parse_boolean(&option.args[0]);
            }
            EXTEND | EXTEND_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("extend option requires one argument"));
                }
                parsed_options.extend = super::utils::parse_boolean(&option.args[0]);
                if let Some(gravity) = option.args.get(1).filter(|arg| !arg.is_empty()) {
                    parsed_options.gravity = Some(Gravity::parse(gravity).ok_or_else(|| {
                        OptionParseError::invalid(
                            "extend gravity must be one of: ce, no, so, ea, we, noea, nowe, soea, sowe",
                        )
                    })?);
                }
            }
            PADDING | PADDING_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid(
                        "padding option requires at least one argument",
                    ));
                }
                let values: Vec<u32> = option
                    .args
                    .iter()
                    .map(|value| parse_integer(value, "padding"))
                    .collect::<Result<Vec<_>, _>>()?;
                parsed_options.padding = Some(match values.len() {
                    1 => (values[0], values[0], values[0], values[0]),
                    2 => (values[0], values[1], values[0], values[1]),
                    4 => (values[0], values[1], values[2], values[3]),
                    _ => {
                        return Err(OptionParseError::invalid("padding must have 1, 2, or 4 arguments"));
                    }
                });
            }
            ROTATE | ROTATE_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("rotation option requires one argument"));
                }
                let rotation = parse_integer(&option.args[0], "rotation")?;
                if !is_valid_rotation(rotation) {
                    return Err(OptionParseError::invalid("rotation must be one of: 0, 90, 180, 270"));
                }
                parsed_options.rotation = Some(rotation);
            }
            FLIP | FLIP_SHORT => {
                parsed_options.flip = Some(Flip {
                    horizontal: parse_optional_bool(&option.args, 0).unwrap_or(false),
                    vertical: parse_optional_bool(&option.args, 1).unwrap_or(false),
                });
            }
            AUTO_ROTATE | AUTO_ROTATE_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("auto_rotate option requires one argument"));
                }
                parsed_options.auto_rotate = super::utils::parse_boolean(&option.args[0]);
            }
            RAW => {
                parsed_options.raw = option
                    .args
                    .first()
                    .filter(|arg| !arg.is_empty())
                    .map(|arg| super::utils::parse_boolean(arg))
                    .unwrap_or(true);
            }
            BLUR | BLUR_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("blur option requires one argument: sigma"));
                }
                parsed_options.blur = Some(parse_positive_f32(&option.args[0], "blur")?);
            }
            CROP | CROP_SHORT => {
                if option.args.len() < 2 {
                    return Err(OptionParseError::invalid(
                        "crop option requires at least two arguments: width, height",
                    ));
                }
                let gravity = option
                    .args
                    .get(2)
                    .filter(|arg| !arg.is_empty())
                    .map(|arg| {
                        Gravity::parse(arg).ok_or_else(|| {
                            OptionParseError::invalid(
                                "crop gravity must be one of: ce, no, so, ea, we, noea, nowe, soea, sowe",
                            )
                        })
                    })
                    .transpose()?;
                parsed_options.crop = Some(Crop {
                    x: 0,
                    y: 0,
                    width: parse_integer(&option.args[0], "crop width")?,
                    height: parse_integer(&option.args[1], "crop height")?,
                    gravity,
                });
            }
            FORMAT | FORMAT_SHORT | FORMAT_EXT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("format option requires one argument"));
                }
                parsed_options.format = Some(option.args[0].clone());
            }
            QUALITY | QUALITY_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("quality option requires one argument"));
                }
                parsed_options.quality = Some(parse_quality(&option.args[0], "quality")?);
            }
            FORMAT_QUALITY | FORMAT_QUALITY_SHORT => {
                if option.args.len() < 2 || option.args.len() % 2 != 0 {
                    return Err(OptionParseError::invalid(
                        "format_quality option requires format/quality pairs",
                    ));
                }

                for pair in option.args.chunks_exact(2) {
                    parsed_options
                        .save
                        .format_quality
                        .insert(pair[0].to_lowercase(), parse_quality(&pair[1], "format_quality")?);
                }
            }
            BACKGROUND | BACKGROUND_SHORT => {
                if option.args.is_empty() {
                    parsed_options.background = None;
                    continue;
                }
                let mut background = if option.args.len() >= 3 {
                    [
                        parse_integer(&option.args[0], "background red channel")?,
                        parse_integer(&option.args[1], "background green channel")?,
                        parse_integer(&option.args[2], "background blue channel")?,
                        255,
                    ]
                } else {
                    super::utils::parse_hex_color(&option.args[0]).map_err(OptionParseError::Color)?
                };
                if let Some(alpha) = parsed_options.background_alpha {
                    background[3] = (alpha * 255.0).round() as u8;
                }
                parsed_options.background = Some(background);
            }
            BACKGROUND_ALPHA | BACKGROUND_ALPHA_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid(
                        "background_alpha option requires one argument",
                    ));
                }
                let alpha = parse_unit_f32(&option.args[0], "background_alpha")?;
                parsed_options.background_alpha = Some(alpha);
                if let Some(ref mut background) = parsed_options.background {
                    background[3] = (alpha * 255.0).round() as u8;
                }
            }
            TRIM | TRIM_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid(
                        "trim option requires at least one argument: threshold",
                    ));
                }
                let threshold = parse_float(&option.args[0], "trim threshold")?;
                if !threshold.is_finite() || threshold < 0.0 {
                    return Err(OptionParseError::invalid(
                        "trim threshold must be a finite, non-negative number",
                    ));
                }
                // An empty colour means "work it out from the image", which is
                // how imgproxy behaves when the argument is omitted.
                let color = match option.args.get(1).filter(|arg| !arg.is_empty()) {
                    Some(arg) => Some(super::utils::parse_hex_color(arg).map_err(OptionParseError::Color)?),
                    None => None,
                };
                parsed_options.trim = Some(Trim {
                    threshold: f64::from(threshold),
                    color,
                    equal_hor: option
                        .args
                        .get(2)
                        .filter(|arg| !arg.is_empty())
                        .map(|arg| super::utils::parse_boolean(arg))
                        .unwrap_or(false),
                    equal_ver: option
                        .args
                        .get(3)
                        .filter(|arg| !arg.is_empty())
                        .map(|arg| super::utils::parse_boolean(arg))
                        .unwrap_or(false),
                });
            }
            MAX_RESULT_DIMENSION | MAX_RESULT_DIMENSION_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid(
                        "max_result_dimension option requires one argument",
                    ));
                }
                parsed_options.max_result_dimension =
                    Some(option.args[0].parse::<MaxResultDimension>().map_err(|source| {
                        OptionParseError::SecurityLimit {
                            option: "max_result_dimension".to_string(),
                            source,
                        }
                    })?);
            }
            MAX_SRC_RESOLUTION | MAX_SRC_RESOLUTION_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid(
                        "max_src_resolution option requires one argument",
                    ));
                }
                parsed_options.max_src_resolution =
                    Some(option.args[0].parse::<MaxSourceResolution>().map_err(|source| {
                        OptionParseError::SecurityLimit {
                            option: "max_src_resolution".to_string(),
                            source,
                        }
                    })?);
            }
            MAX_SRC_FILE_SIZE | MAX_SRC_FILE_SIZE_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid(
                        "max_src_file_size option requires one argument",
                    ));
                }
                parsed_options.max_src_file_size =
                    Some(option.args[0].parse::<MaxSourceFileSize>().map_err(|source| {
                        OptionParseError::SecurityLimit {
                            option: "max_src_file_size".to_string(),
                            source,
                        }
                    })?);
            }
            CACHEBUSTER | CACHEBUSTER_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("cachebuster option requires one argument"));
                }
                parsed_options.cache_buster = Some(option.args[0].clone());
            }
            DPR => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("dpr option requires one argument"));
                }
                let dpr = parse_float(&option.args[0], "dpr")?;
                if !(1.0..=5.0).contains(&dpr) {
                    return Err(OptionParseError::invalid("dpr value must be between 1.0 and 5.0"));
                }
                parsed_options.dpr = Some(dpr);
            }
            MIN_WIDTH | MIN_WIDTH_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("min-width option requires one argument"));
                }
                parsed_options.min_width = Some(parse_integer(&option.args[0], "min-width")?);
            }
            MIN_HEIGHT | MIN_HEIGHT_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("min-height option requires one argument"));
                }
                parsed_options.min_height = Some(parse_integer(&option.args[0], "min-height")?);
            }
            ZOOM | ZOOM_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("zoom option requires one argument"));
                }
                parsed_options.zoom = Some(parse_positive_f32(&option.args[0], "zoom")?);
            }
            SHARPEN | SHARPEN_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("sharpen option requires one argument"));
                }
                parsed_options.sharpen = Some(parse_positive_f32(&option.args[0], "sharpen")?);
            }
            PIXELATE | PIXELATE_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("pixelate option requires one argument"));
                }
                parsed_options.pixelate = Some(parse_integer(&option.args[0], "pixelate")?);
            }
            ADJUST | ADJUST_SHORT => {
                let mut adjust = parsed_options.adjust.unwrap_or_default();
                if let Some(arg) = option.args.first().filter(|arg| !arg.is_empty()) {
                    adjust.brightness = parse_brightness(arg)?;
                }
                if let Some(arg) = option.args.get(1).filter(|arg| !arg.is_empty()) {
                    adjust.contrast = parse_positive_f32(arg, "contrast")?;
                }
                if let Some(arg) = option.args.get(2).filter(|arg| !arg.is_empty()) {
                    adjust.saturation = parse_positive_f32(arg, "saturation")?;
                }
                parsed_options.adjust = Some(adjust);
            }
            BRIGHTNESS | BRIGHTNESS_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("brightness option requires one argument"));
                }
                let mut adjust = parsed_options.adjust.unwrap_or_default();
                adjust.brightness = parse_brightness(&option.args[0])?;
                parsed_options.adjust = Some(adjust);
            }
            CONTRAST | CONTRAST_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("contrast option requires one argument"));
                }
                let mut adjust = parsed_options.adjust.unwrap_or_default();
                adjust.contrast = parse_positive_f32(&option.args[0], "contrast")?;
                parsed_options.adjust = Some(adjust);
            }
            SATURATION | SATURATION_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("saturation option requires one argument"));
                }
                let mut adjust = parsed_options.adjust.unwrap_or_default();
                adjust.saturation = parse_positive_f32(&option.args[0], "saturation")?;
                parsed_options.adjust = Some(adjust);
            }
            WATERMARK | WATERMARK_SHORT => {
                if option.args.len() < 2 {
                    return Err(OptionParseError::invalid(
                        "watermark option requires two arguments: opacity, position",
                    ));
                }
                parsed_options.watermark = Some(Watermark {
                    opacity: parse_float(&option.args[0], "watermark opacity")?,
                    position: option.args[1].clone(),
                });
            }
            WATERMARK_URL | WATERMARK_URL_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("watermark_url option requires one argument"));
                }
                let decoded_url = decode_base64(&option.args[0], "watermark_url")?;
                let url = decode_utf8(decoded_url, "watermark_url")?;
                parsed_options.watermark_url = Some(url);
            }
            RESIZING_ALGORITHM | RESIZING_ALGORITHM_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid(
                        "resizing_algorithm option requires one argument",
                    ));
                }
                let algorithm = option.args[0].to_lowercase();
                if !matches!(
                    algorithm.as_str(),
                    "nearest" | "linear" | "cubic" | "lanczos2" | "lanczos3"
                ) {
                    return Err(OptionParseError::invalid(format!(
                        "Invalid resizing algorithm: {}. Must be one of: nearest, linear, cubic, lanczos2, lanczos3",
                        algorithm
                    )));
                }
                parsed_options.resizing_algorithm = Some(algorithm);
            }
            MAX_BYTES | MAX_BYTES_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("max_bytes option requires one argument"));
                }
                parsed_options.save.max_bytes = Some(parse_integer(&option.args[0], "max_bytes")?);
            }
            STRIP_METADATA | STRIP_METADATA_SHORT => {
                parsed_options.save.strip_metadata = Some(
                    option
                        .args
                        .first()
                        .map(|arg| super::utils::parse_boolean(arg))
                        .unwrap_or(true),
                );
            }
            STRIP_COLOR_PROFILE | STRIP_COLOR_PROFILE_SHORT => {
                parsed_options.save.strip_color_profile = Some(
                    option
                        .args
                        .first()
                        .map(|arg| super::utils::parse_boolean(arg))
                        .unwrap_or(true),
                );
            }
            JPEG_OPTIONS | JPEG_OPTIONS_SHORT => {
                parsed_options.save.jpeg.progressive = parse_optional_bool(&option.args, 0);
                parsed_options.save.jpeg.no_subsample = parse_optional_bool(&option.args, 1);
                parsed_options.save.jpeg.trellis_quant = parse_optional_bool(&option.args, 2);
                parsed_options.save.jpeg.overshoot_deringing = parse_optional_bool(&option.args, 3);
                parsed_options.save.jpeg.optimize_scans = parse_optional_bool(&option.args, 4);
                if let Some(arg) = option.args.get(5).filter(|arg| !arg.is_empty()) {
                    parsed_options.save.jpeg.quant_table = Some(parse_integer(arg, "jpeg quant_table")?);
                }
            }
            PNG_OPTIONS | PNG_OPTIONS_SHORT => {
                parsed_options.save.png.interlaced = parse_optional_bool(&option.args, 0);
                parsed_options.save.png.quantize = parse_optional_bool(&option.args, 1);
                if let Some(arg) = option.args.get(2).filter(|arg| !arg.is_empty()) {
                    parsed_options.save.png.quantization_colors = Some(parse_integer(arg, "png quantization_colors")?);
                }
            }
            WEBP_OPTIONS | WEBP_OPTIONS_SHORT => {
                parsed_options.save.webp.lossless = parse_optional_bool(&option.args, 0);
                parsed_options.save.webp.smart_subsample = parse_optional_bool(&option.args, 1);
                if let Some(arg) = option.args.get(2).filter(|arg| !arg.is_empty()) {
                    parsed_options.save.webp.preset = Some(arg.to_lowercase());
                }
            }
            AVIF_OPTIONS | AVIF_OPTIONS_SHORT => {
                parsed_options.save.avif.no_subsample = parse_optional_bool(&option.args, 0);
            }
            PAGE | PAGE_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("page option requires one argument"));
                }
                parsed_options.page = Some(parse_integer(&option.args[0], "page")?);
            }
            PAGES | PAGES_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("pages option requires one argument"));
                }
                parsed_options.pages = Some(parse_integer(&option.args[0], "pages")?);
            }
            DISABLE_ANIMATION | DISABLE_ANIMATION_SHORT => {
                parsed_options.disable_animation = option
                    .args
                    .first()
                    .map(|arg| super::utils::parse_boolean(arg))
                    .unwrap_or(true);
            }
            SKIP_PROCESSING | SKIP_PROCESSING_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid(
                        "skip_processing option requires at least one argument",
                    ));
                }
                parsed_options.skip_processing = option.args.iter().map(|arg| arg.to_lowercase()).collect();
            }
            EXPIRES | EXPIRES_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("expires option requires one argument"));
                }
                parsed_options.expires = Some(parse_integer(&option.args[0], "expires timestamp")?);
            }
            FILENAME | FILENAME_SHORT => {
                if option.args.is_empty() {
                    return Err(OptionParseError::invalid("filename option requires one argument"));
                }
                let encoded = option
                    .args
                    .get(1)
                    .map(|arg| super::utils::parse_boolean(arg))
                    .unwrap_or(false);
                parsed_options.filename = Some(if encoded {
                    let decoded = decode_base64(&option.args[0], "filename")?;
                    decode_utf8(decoded, "filename")?
                } else {
                    option.args[0].clone()
                });
            }
            RETURN_ATTACHMENT | RETURN_ATTACHMENT_SHORT => {
                parsed_options.return_attachment = option
                    .args
                    .first()
                    .map(|arg| super::utils::parse_boolean(arg))
                    .unwrap_or(true);
            }
            _ => {
                debug!("Unknown option: {}", option.name);
            }
        }
    }

    // Default resize type is `fit`
    if parsed_options.resize.is_none() && (parsed_options.width.is_some() || parsed_options.height.is_some()) {
        debug!("Applying default 'fit' resize due to width/height options");
        parsed_options.resize = Some(Resize {
            resizing_type: "fit".to_string(),
            width: parsed_options.width.unwrap_or(0),
            height: parsed_options.height.unwrap_or(0),
        });
    }

    Ok(parsed_options)
}

fn parse_brightness(value: &str) -> Result<i16, OptionParseError> {
    let parsed = parse_integer::<i16>(value, "brightness")?;
    if !(-255..=255).contains(&parsed) {
        return Err(OptionParseError::invalid("brightness must be between -255 and 255"));
    }
    Ok(parsed)
}