tus-protocol 0.0.1

Rust implementation of the TUS resumable upload protocol
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
//! TUS server configuration.
//!
//! This module defines the configuration options for a TUS server,
//! including supported extensions, size limits, and timeouts.

use std::collections::HashSet;
use std::time::Duration;

use crate::error::Error;

/// Characters escaped when an upload id is placed in a URL path segment:
/// everything except RFC 3986 unreserved characters.
const PATH_SEGMENT_ENCODE: &percent_encoding::AsciiSet = &percent_encoding::NON_ALPHANUMERIC
    .remove(b'-')
    .remove(b'_')
    .remove(b'.')
    .remove(b'~');

/// The TUS protocol version supported by this implementation.
pub const TUS_VERSION: &str = "1.0.0";

/// Resumable header value for the TUS protocol.
pub const TUS_RESUMABLE: &str = "1.0.0";

/// Default cap on bytes buffered in memory while accepting a request body whose
/// length is not known in advance (chunked transfer encoding / no
/// `Content-Length`). See [`Config::max_intake_buffer`].
pub const DEFAULT_MAX_INTAKE_BUFFER: u64 = 8 * 1024 * 1024;

/// TUS server configuration.
///
/// # Defaults
///
/// [`Config::default`] enables Creation, Termination, and
/// Creation-Defer-Length; accepts standard empty Creation requests; uses
/// `/files` as the base path; has no maximum upload size, no expiration, and a
/// 30-second lock timeout; does not trust forwarded proxy headers; and caps
/// in-memory intake buffering for bodies of unknown length at
/// [`DEFAULT_MAX_INTAKE_BUFFER`].
///
/// # Examples
///
/// ```rust
/// use std::time::Duration;
/// use tus_protocol::{Config, Extension};
///
/// # fn main() -> Result<(), Box<dyn std::error::Error>> {
/// // `try_with_extension` returns an error instead of panicking when the
/// // current build cannot service the extension (see [`Config::with_extension`]
/// // for the panicking counterpart and when each is appropriate).
/// let config = Config::new()
///     .with_base_path("/uploads")
///     .with_max_size(100 * 1024 * 1024)
///     .with_expiration(Duration::from_secs(24 * 60 * 60))
///     .try_with_extension(Extension::Concatenation)?;
///
/// assert_eq!(config.base_path(), "/uploads");
/// assert!(config.has_extension(Extension::Concatenation));
/// # Ok(())
/// # }
/// ```
#[derive(Debug, Clone, PartialEq, Eq)]
#[non_exhaustive]
pub struct Config {
    /// Maximum upload size in bytes. None means unlimited.
    max_size: Option<u64>,

    /// Enabled TUS extensions.
    extensions: HashSet<Extension>,

    /// Supported checksum algorithms (only relevant if Checksum extension is enabled).
    checksum_algorithms: HashSet<ChecksumAlgorithm>,

    /// Default expiration duration for uploads. None means uploads don't expire.
    expiration: Option<Duration>,

    /// Lock acquisition timeout.
    lock_timeout: Duration,

    /// Base path for the TUS endpoint (e.g., "/files").
    base_path: String,

    /// Base URL for absolute Location headers (e.g., "http://localhost:8080").
    /// If set, Location headers will be absolute URLs.
    base_url: Option<String>,

    /// Whether to respect forwarded headers (X-Forwarded-Host, X-Forwarded-Proto).
    respect_forwarded_headers: bool,

    /// Maximum chunk size per PATCH request. None means unlimited.
    max_chunk_size: Option<u64>,

    /// Maximum bytes buffered in memory while accepting a request body whose
    /// length is not known in advance (chunked transfer encoding / no
    /// `Content-Length`, e.g. checksum trailers). `None` disables the cap
    /// (unbounded intake buffering). Bodies with a known `Content-Length`
    /// stream in constant memory and are not affected.
    max_intake_buffer: Option<u64>,

    /// Whether to disable download (GET) endpoint.
    disable_download: bool,

    /// Whether to allow standard empty Creation requests.
    allow_empty_creation: bool,
}

impl Default for Config {
    fn default() -> Self {
        let mut extensions = HashSet::new();
        extensions.insert(Extension::Creation);
        extensions.insert(Extension::Termination);
        extensions.insert(Extension::CreationDeferLength);

        Self {
            max_size: None,
            extensions,
            checksum_algorithms: HashSet::new(),
            expiration: None,
            lock_timeout: Duration::from_secs(30),
            base_path: "/files".to_string(),
            base_url: None,
            respect_forwarded_headers: false,
            max_chunk_size: None,
            max_intake_buffer: Some(DEFAULT_MAX_INTAKE_BUFFER),
            disable_download: false,
            allow_empty_creation: true,
        }
    }
}

impl Config {
    /// Creates a new configuration with default settings.
    pub fn new() -> Self {
        Self::default()
    }

    /// Creates a configuration with all extensions enabled, including the
    /// non-standard `ConcatenationUnfinished`.
    ///
    /// Note: advertising `concatenation-unfinished` means final uploads with
    /// incomplete partials are accepted (the extension's defining behavior),
    /// which supersedes the core concatenation requirement that partials must
    /// be complete. If you want strict core-concatenation semantics, use
    /// [`Config::default()`] plus explicit `.with_extension(...)` calls
    /// and don't enable `ConcatenationUnfinished`.
    #[must_use]
    pub fn all_extensions() -> Self {
        Self {
            extensions: Extension::supported().iter().copied().collect(),
            checksum_algorithms: [
                ChecksumAlgorithm::Sha1,
                ChecksumAlgorithm::Sha256,
                ChecksumAlgorithm::Md5,
                ChecksumAlgorithm::Crc32,
            ]
            .into_iter()
            .collect(),
            ..Self::default()
        }
    }

    /// Sets the maximum upload size.
    #[must_use]
    pub fn with_max_size(mut self, size: u64) -> Self {
        self.max_size = Some(size);
        self
    }

    /// Adds an extension to the enabled set.
    ///
    /// When enabling the Checksum extension, default algorithms (sha1) are automatically
    /// added if no algorithms are already configured. Use `with_checksum()` to add
    /// additional algorithms.
    ///
    /// # Panics
    ///
    /// Panics when enabling [`Extension::Checksum`] or
    /// [`Extension::ChecksumTrailer`] without the `checksum` Cargo feature:
    /// the extension would be advertised but no algorithm could ever verify a
    /// request. Enable the `checksum` feature of `tus-protocol` to use these
    /// extensions.
    ///
    /// Because whether the `checksum` feature is enabled is often decided by
    /// Cargo feature unification rather than by the code calling this method,
    /// prefer [`Config::try_with_extension`] when the extension set is not
    /// statically known to be serviceable by the current build.
    #[must_use]
    pub fn with_extension(self, ext: Extension) -> Self {
        self.try_with_extension(ext).unwrap_or_else(|err| {
            panic!("{err}");
        })
    }

    /// Adds an extension to the enabled set, returning an error instead of
    /// panicking when the current build cannot service it.
    ///
    /// Enabling [`Extension::Checksum`] or [`Extension::ChecksumTrailer`]
    /// without the `checksum` Cargo feature returns
    /// [`Error::ExtensionNotSupported`]: the extension would be advertised but
    /// no algorithm could ever verify a request. This is the fallible
    /// counterpart to [`Config::with_extension`]; use it when whether the
    /// `checksum` feature is enabled is not under this call site's control.
    ///
    /// When enabling the Checksum extension, default algorithms (sha1) are
    /// automatically added if no algorithms are already configured.
    ///
    /// # Errors
    ///
    /// Returns [`Error::ExtensionNotSupported`] when enabling a checksum
    /// extension without the `checksum` Cargo feature.
    pub fn try_with_extension(mut self, ext: Extension) -> Result<Self, Error> {
        #[cfg(not(feature = "checksum"))]
        if matches!(ext, Extension::Checksum | Extension::ChecksumTrailer) {
            return Err(Error::ExtensionNotSupported(format!(
                "{}: enable the `checksum` feature of tus-protocol",
                ext.as_str()
            )));
        }

        self.extensions.insert(ext);
        // When enabling Checksum extension, ensure at least sha1 is available
        #[cfg(feature = "checksum")]
        if matches!(ext, Extension::Checksum | Extension::ChecksumTrailer) {
            self.extensions.insert(Extension::Checksum);
            if self.checksum_algorithms.is_empty() {
                self.checksum_algorithms.insert(ChecksumAlgorithm::Sha1);
            }
        }
        Ok(self)
    }

    /// Removes an extension from the enabled set.
    #[must_use]
    pub fn without_extension(mut self, ext: Extension) -> Self {
        self.extensions.remove(&ext);
        self
    }

    /// Sets the expiration duration for uploads.
    #[must_use]
    pub fn with_expiration(mut self, duration: Duration) -> Self {
        self.expiration = Some(duration);
        self.extensions.insert(Extension::Expiration);
        self
    }

    /// Sets the lock acquisition timeout.
    #[must_use]
    pub fn with_lock_timeout(mut self, timeout: Duration) -> Self {
        self.lock_timeout = timeout;
        self
    }

    /// Sets the base path for the TUS endpoint.
    #[must_use]
    pub fn with_base_path(mut self, path: impl Into<String>) -> Self {
        self.base_path = path.into();
        self
    }

    /// Sets the base URL for absolute Location headers.
    ///
    /// When set, Location headers will include the full URL (e.g., "http://localhost:8080/files/abc").
    /// The base URL should not include a trailing slash.
    #[must_use]
    pub fn with_base_url(mut self, url: impl Into<String>) -> Self {
        self.base_url = Some(url.into());
        self
    }

    /// Trusts the `X-Forwarded-Host` and `X-Forwarded-Proto` request headers
    /// when building absolute `Location` URLs.
    ///
    /// Disabled by default, and it should stay disabled unless a trusted
    /// reverse proxy sits in front of the server. These headers are
    /// client-supplied: if untrusted requests reach the server directly, an
    /// attacker can set `X-Forwarded-Host` to an arbitrary value and poison the
    /// `Location` URL returned from Creation (a classic host-header injection /
    /// URL-poisoning vector). Only enable this when a proxy you control
    /// **overwrites** (not appends to) `X-Forwarded-Host` / `X-Forwarded-Proto`
    /// on every inbound request, so clients cannot forge them. Setting
    /// [`with_base_url`](Self::with_base_url) instead pins the origin explicitly
    /// and takes precedence over forwarded headers.
    #[must_use]
    pub fn with_respect_forwarded_headers(mut self) -> Self {
        self.respect_forwarded_headers = true;
        self
    }

    /// Sets the maximum chunk size per PATCH request.
    #[must_use]
    pub fn with_max_chunk_size(mut self, size: u64) -> Self {
        self.max_chunk_size = Some(size);
        self
    }

    /// Sets the maximum bytes buffered in memory for a request body whose
    /// length is not known in advance (chunked transfer encoding / no
    /// `Content-Length`).
    ///
    /// Bodies with a known `Content-Length` stream to storage in constant
    /// memory and are unaffected. A body without `Content-Length` (chunked
    /// transfer encoding, including checksum-trailer uploads) must be buffered
    /// to discover its size before it can be committed, so this bounds that
    /// buffer. Exceeding it fails the request with `413 Payload Too Large`.
    ///
    /// Defaults to [`DEFAULT_MAX_INTAKE_BUFFER`]. Raise it for deployments that
    /// accept large chunked uploads, or call
    /// [`without_intake_buffer_limit`](Self::without_intake_buffer_limit) to
    /// remove the cap entirely.
    ///
    /// Note the distinction between the two edge cases: `size == 0` *disables*
    /// buffering support, so any non-empty unknown-length body is rejected with
    /// `413` on its first chunk, whereas
    /// [`without_intake_buffer_limit`](Self::without_intake_buffer_limit) leaves
    /// the buffer *unbounded*.
    #[must_use]
    pub fn with_max_intake_buffer(mut self, size: u64) -> Self {
        self.max_intake_buffer = Some(size);
        self
    }

    /// Removes the in-memory intake-buffer cap, allowing a body of unknown
    /// length (chunked transfer / no `Content-Length`) to be buffered without
    /// bound.
    ///
    /// This re-enables the memory-exhaustion exposure that
    /// [`with_max_intake_buffer`](Self::with_max_intake_buffer) guards against;
    /// only use it when an upstream layer already bounds request body size.
    #[must_use]
    pub fn without_intake_buffer_limit(mut self) -> Self {
        self.max_intake_buffer = None;
        self
    }

    /// Disables the download endpoint.
    #[must_use]
    pub fn without_download(mut self) -> Self {
        self.disable_download = true;
        self
    }

    /// Adds a checksum algorithm to the supported set and enables the
    /// Checksum extension.
    ///
    /// SHA-1 is always added alongside the given algorithm because the tus
    /// specification requires servers supporting the Checksum extension to
    /// accept SHA-1. Trailer checksums are a separate extension; enable them
    /// explicitly with `with_extension(Extension::ChecksumTrailer)`.
    #[cfg(feature = "checksum")]
    #[must_use]
    pub fn with_checksum(mut self, algorithm: ChecksumAlgorithm) -> Self {
        self.checksum_algorithms.insert(algorithm);
        self.checksum_algorithms.insert(ChecksumAlgorithm::Sha1);
        self.extensions.insert(Extension::Checksum);
        self
    }

    /// Rejects empty creation requests.
    ///
    /// The tus Creation extension uses an empty `POST` with `Upload-Length` as
    /// its standard creation example, and it is accepted by default. Calling
    /// this is an opt-in non-compliant mode for deployments that only want
    /// Creation-With-Upload requests to create new resources.
    #[must_use]
    pub fn without_empty_creation(mut self) -> Self {
        self.allow_empty_creation = false;
        self
    }

    /// Checks if an extension is enabled.
    pub fn has_extension(&self, ext: Extension) -> bool {
        self.extensions.contains(&ext)
    }

    /// Returns the configured maximum upload size in bytes, if any.
    pub fn max_size(&self) -> Option<u64> {
        self.max_size
    }

    /// Returns the supported checksum algorithms, sorted by name.
    ///
    /// The internal collection type is deliberately not exposed so it can
    /// change without breaking callers.
    pub fn checksum_algorithms(&self) -> Vec<ChecksumAlgorithm> {
        let mut algorithms: Vec<_> = self.checksum_algorithms.iter().copied().collect();
        algorithms.sort_by_key(|algorithm| algorithm.as_str());
        algorithms
    }

    /// Returns whether a checksum algorithm is supported.
    pub fn supports_checksum_algorithm(&self, algorithm: ChecksumAlgorithm) -> bool {
        self.checksum_algorithms.contains(&algorithm)
    }

    /// Returns the configured expiration duration, if any.
    pub fn expiration(&self) -> Option<Duration> {
        self.expiration
    }

    /// Returns the lock acquisition timeout.
    pub fn lock_timeout(&self) -> Duration {
        self.lock_timeout
    }

    /// Returns the base path for TUS endpoints.
    pub fn base_path(&self) -> &str {
        &self.base_path
    }

    /// Returns the configured base URL for absolute Location headers.
    pub fn base_url(&self) -> Option<&str> {
        self.base_url.as_deref()
    }

    /// Returns whether forwarded proxy headers should be trusted.
    pub fn respects_forwarded_headers(&self) -> bool {
        self.respect_forwarded_headers
    }

    /// Returns the maximum PATCH chunk size, if configured.
    pub fn max_chunk_size(&self) -> Option<u64> {
        self.max_chunk_size
    }

    /// Returns the in-memory intake-buffer cap for bodies of unknown length,
    /// if any. `None` means intake buffering is unbounded.
    pub fn max_intake_buffer(&self) -> Option<u64> {
        self.max_intake_buffer
    }

    /// Returns whether the download endpoint is disabled.
    pub fn is_download_disabled(&self) -> bool {
        self.disable_download
    }

    /// Returns whether empty creation requests are allowed.
    pub fn allows_empty_creation(&self) -> bool {
        self.allow_empty_creation
    }

    /// Returns the extensions as a comma-separated string for the Tus-Extension header.
    pub fn extensions_string(&self) -> String {
        let mut exts: Vec<_> = self.extensions.iter().map(|e| e.as_str()).collect();
        exts.sort();
        exts.join(",")
    }

    /// Returns the checksum algorithms as a comma-separated string.
    pub fn checksum_algorithms_string(&self) -> String {
        self.checksum_algorithms()
            .iter()
            .map(|a| a.as_str())
            .collect::<Vec<_>>()
            .join(",")
    }

    /// Builds the full URL for an upload.
    ///
    /// The upload id is percent-encoded as a path segment so externally
    /// seeded ids with reserved characters still yield a usable Location.
    ///
    /// Uses the following priority for base URL:
    /// 1. Config's base_url if set
    /// 2. Request's base URL (from scheme + host) if provided
    /// 3. Falls back to relative path
    pub fn upload_url(&self, upload_id: &str, request_base_url: Option<&str>) -> String {
        let upload_id = percent_encoding::utf8_percent_encode(upload_id, PATH_SEGMENT_ENCODE);
        let base = self.base_url.as_deref().or(request_base_url);
        match base {
            Some(base) => format!("{}{}/{}", base, self.base_path, upload_id),
            None => format!("{}/{}", self.base_path, upload_id),
        }
    }
}

/// TUS protocol extensions.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum Extension {
    /// Creation extension - allows creating new uploads via POST.
    Creation,
    /// Creation with upload - allows including data in the initial POST request.
    CreationWithUpload,
    /// Creation with deferred length - allows uploads without knowing size upfront.
    CreationDeferLength,
    /// Termination extension - allows deleting uploads via DELETE.
    Termination,
    /// Expiration extension - uploads can expire and be cleaned up.
    Expiration,
    /// Concatenation extension - allows merging partial uploads.
    Concatenation,
    /// Concatenation-Unfinished extension - allows creating final uploads before all partials are complete.
    ConcatenationUnfinished,
    /// Checksum extension - allows verifying chunk integrity.
    Checksum,
    /// Checksum trailer extension - allows checksums to be sent as HTTP trailers.
    ChecksumTrailer,
}

impl Extension {
    /// Returns the extension name as used in the Tus-Extension header.
    pub fn as_str(self) -> &'static str {
        match self {
            Extension::Creation => "creation",
            Extension::CreationWithUpload => "creation-with-upload",
            Extension::CreationDeferLength => "creation-defer-length",
            Extension::Termination => "termination",
            Extension::Expiration => "expiration",
            Extension::Concatenation => "concatenation",
            Extension::ConcatenationUnfinished => "concatenation-unfinished",
            Extension::Checksum => "checksum",
            Extension::ChecksumTrailer => "checksum-trailer",
        }
    }

    /// Returns all extensions supported by this build.
    pub fn supported() -> &'static [Extension] {
        &[
            Extension::Creation,
            Extension::CreationWithUpload,
            Extension::CreationDeferLength,
            Extension::Termination,
            Extension::Expiration,
            Extension::Concatenation,
            Extension::ConcatenationUnfinished,
            #[cfg(feature = "checksum")]
            Extension::Checksum,
            #[cfg(feature = "checksum")]
            Extension::ChecksumTrailer,
        ]
    }
}

impl std::fmt::Display for Extension {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl std::str::FromStr for Extension {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "creation" => Ok(Extension::Creation),
            "creation-with-upload" => Ok(Extension::CreationWithUpload),
            "creation-defer-length" => Ok(Extension::CreationDeferLength),
            "termination" => Ok(Extension::Termination),
            "expiration" => Ok(Extension::Expiration),
            "concatenation" => Ok(Extension::Concatenation),
            "concatenation-unfinished" => Ok(Extension::ConcatenationUnfinished),
            "checksum" => Ok(Extension::Checksum),
            "checksum-trailer" => Ok(Extension::ChecksumTrailer),
            other => Err(Error::ExtensionNotSupported(other.to_string())),
        }
    }
}

/// Checksum algorithms supported by the checksum extension.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
#[non_exhaustive]
pub enum ChecksumAlgorithm {
    /// SHA-1 hash.
    Sha1,
    /// SHA-256 hash.
    Sha256,
    /// MD5 hash.
    Md5,
    /// CRC32 checksum.
    Crc32,
}

impl ChecksumAlgorithm {
    /// Returns the algorithm name as used in the Tus-Checksum-Algorithm header.
    pub fn as_str(self) -> &'static str {
        match self {
            ChecksumAlgorithm::Sha1 => "sha1",
            ChecksumAlgorithm::Sha256 => "sha256",
            ChecksumAlgorithm::Md5 => "md5",
            ChecksumAlgorithm::Crc32 => "crc32",
        }
    }
}

impl std::fmt::Display for ChecksumAlgorithm {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.write_str(self.as_str())
    }
}

impl std::str::FromStr for ChecksumAlgorithm {
    type Err = Error;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s.to_lowercase().as_str() {
            "sha1" => Ok(ChecksumAlgorithm::Sha1),
            "sha256" => Ok(ChecksumAlgorithm::Sha256),
            "md5" => Ok(ChecksumAlgorithm::Md5),
            "crc32" => Ok(ChecksumAlgorithm::Crc32),
            other => Err(Error::UnsupportedChecksum(other.to_string())),
        }
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_supported_extensions_match_build_features() {
        let extensions = Extension::supported();

        assert!(extensions.contains(&Extension::Creation));
        assert!(extensions.contains(&Extension::ConcatenationUnfinished));

        #[cfg(feature = "checksum")]
        {
            assert!(extensions.contains(&Extension::Checksum));
            assert!(extensions.contains(&Extension::ChecksumTrailer));
        }

        #[cfg(not(feature = "checksum"))]
        {
            assert!(!extensions.contains(&Extension::Checksum));
            assert!(!extensions.contains(&Extension::ChecksumTrailer));
        }
    }

    #[test]
    fn test_default_config() {
        let config = Config::default();
        assert!(config.has_extension(Extension::Creation));
        assert!(config.has_extension(Extension::Termination));
        assert!(!config.has_extension(Extension::Checksum));
    }

    #[cfg(feature = "checksum")]
    #[test]
    fn test_builder_pattern() {
        let config = Config::new()
            .with_max_size(1024 * 1024 * 100) // 100 MB
            .with_extension(Extension::Checksum)
            .with_checksum(ChecksumAlgorithm::Sha256)
            .with_expiration(Duration::from_secs(3600))
            .with_base_path("/uploads");

        assert_eq!(config.max_size(), Some(100 * 1024 * 1024));
        assert!(config.has_extension(Extension::Checksum));
        assert!(config.has_extension(Extension::Expiration));
        assert!(config.supports_checksum_algorithm(ChecksumAlgorithm::Sha256));
        assert_eq!(config.base_path(), "/uploads");
    }

    #[cfg(not(feature = "checksum"))]
    #[test]
    #[should_panic(expected = "enable the `checksum` feature")]
    fn with_extension_panics_for_checksum_without_feature() {
        let _ = Config::new().with_extension(Extension::Checksum);
    }

    #[cfg(not(feature = "checksum"))]
    #[test]
    #[should_panic(expected = "enable the `checksum` feature")]
    fn with_extension_panics_for_checksum_trailer_without_feature() {
        let _ = Config::new().with_extension(Extension::ChecksumTrailer);
    }

    #[cfg(not(feature = "checksum"))]
    #[test]
    fn try_with_extension_errors_for_checksum_without_feature() {
        let err = Config::new()
            .try_with_extension(Extension::Checksum)
            .unwrap_err();
        assert!(matches!(err, Error::ExtensionNotSupported(_)));
    }

    #[test]
    fn try_with_extension_accepts_non_checksum_extensions() {
        let config = Config::new()
            .try_with_extension(Extension::Concatenation)
            .unwrap();
        assert!(config.has_extension(Extension::Concatenation));
    }

    #[cfg(feature = "checksum")]
    #[test]
    fn checksum_algorithms_are_returned_sorted_by_name() {
        let config = Config::new()
            .with_checksum(ChecksumAlgorithm::Sha256)
            .with_checksum(ChecksumAlgorithm::Crc32);

        let names: Vec<&str> = config
            .checksum_algorithms()
            .into_iter()
            .map(|algorithm| algorithm.as_str())
            .collect();
        assert_eq!(names, vec!["crc32", "sha1", "sha256"]);
    }

    #[test]
    fn test_extensions_string() {
        let config = Config::new()
            .with_extension(Extension::Creation)
            .with_extension(Extension::Termination);

        let exts = config.extensions_string();
        assert!(exts.contains("creation"));
        assert!(exts.contains("termination"));
    }
}