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
//! Ranges for specifying downloads
use std::{
    fmt,
    ops::{Range, RangeFrom, RangeFull, RangeInclusive, RangeTo, RangeToInclusive},
};

use crate::errors::CondowError;

/// An inclusive range which can not have a length of 0.
///
/// A replacement for [RangeInclusive] with some sugar.
///
///
/// ## Examples
///
/// ```
/// # use condow_core::InclusiveRange;
///
/// let range1: InclusiveRange = (10..=20).into();
/// let range2 = InclusiveRange(10, 20);
///
/// assert_eq!(range1, range2);
/// ```
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct InclusiveRange(pub u64, pub u64);

impl InclusiveRange {
    /// Returns the index of the first item within the range
    ///
    /// ```
    /// use condow_core::InclusiveRange;
    ///
    /// let range: InclusiveRange = (4..=9).into();
    ///
    /// assert_eq!(range.start(), 4);
    /// ```
    pub fn start(&self) -> u64 {
        self.0
    }

    /// Returns the index of the last item within the range
    ///
    /// ```
    /// use condow_core::InclusiveRange;
    ///
    /// let range: InclusiveRange = (4..=9).into();
    ///
    /// assert_eq!(range.end_incl(), 9);
    /// ```
    pub fn end_incl(&self) -> u64 {
        self.1
    }

    pub fn validate(&self) -> Result<(), CondowError> {
        if self.end_incl() < self.start() {
            Err(CondowError::new_invalid_range(format!(
                "End must not be smaller than start: {}",
                self
            )))
        } else {
            Ok(())
        }
    }

    /// Returns the length of the range
    ///
    /// ```
    /// use condow_core::InclusiveRange;
    ///
    /// let range: InclusiveRange = (4..=9).into();
    ///
    /// assert_eq!(range.len(), 6);
    /// ```
    #[allow(clippy::len_without_is_empty)]
    #[inline]
    pub fn len(&self) -> u64 {
        if self.1 < self.0 {
            return 0;
        }

        self.1 - self.0 + 1
    }

    pub fn to_std_range(self) -> RangeInclusive<u64> {
        self.0..=self.1
    }

    #[cfg(test)]
    pub fn to_std_range_usize(self) -> RangeInclusive<usize> {
        self.0 as usize..=self.1 as usize
    }

    pub fn to_std_range_excl(self) -> Range<u64> {
        self.0..self.1 + 1
    }

    #[inline]
    pub fn advance(&mut self, by: u64) {
        self.0 += by
    }

    /// Returns a value for an  `HTTP-Range` header with bytes as the unit
    ///
    /// ```
    /// use condow_core::InclusiveRange;
    ///
    /// let range: InclusiveRange = (4..=9).into();
    ///
    /// assert_eq!(range.http_bytes_range_value(), "bytes=4-9");
    /// ```
    pub fn http_bytes_range_value(&self) -> String {
        format!("bytes={}-{}", self.0, self.1)
    }
}

impl fmt::Display for InclusiveRange {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "[{}..{}]", self.0, self.1)
    }
}

impl From<RangeInclusive<u64>> for InclusiveRange {
    fn from(ri: RangeInclusive<u64>) -> Self {
        Self(*ri.start(), *ri.end())
    }
}

impl From<RangeToInclusive<u64>> for InclusiveRange {
    fn from(ri: RangeToInclusive<u64>) -> Self {
        Self(0, ri.end)
    }
}
impl From<InclusiveRange> for RangeInclusive<u64> {
    fn from(ir: InclusiveRange) -> Self {
        ir.to_std_range()
    }
}

impl From<InclusiveRange> for Range<u64> {
    fn from(ir: InclusiveRange) -> Self {
        ir.to_std_range_excl()
    }
}

/// A range defined by an offset and a length.
#[derive(Debug, Copy, Clone, PartialEq, Eq)]
pub struct OffsetRange(pub u64, pub u64);

impl OffsetRange {
    pub fn new(offset: u64, len: u64) -> Self {
        Self(offset, len)
    }

    /// Returns the index of the first item within the range
    ///
    /// ```
    /// use condow_core::OffsetRange;
    ///
    /// let range = OffsetRange::new(4, 6);
    ///
    /// assert_eq!(range.start(), 4);
    /// ```
    pub fn start(&self) -> u64 {
        self.0
    }

    /// Returns the index of the first item after the range
    ///
    /// ```
    /// use condow_core::OffsetRange;
    ///
    /// let range = OffsetRange::new(4, 6);
    ///
    /// assert_eq!(range.end_excl(), 10);
    /// ```
    pub fn end_excl(&self) -> u64 {
        self.0 + self.1
    }

    #[allow(clippy::len_without_is_empty)]
    /// Returns the length of the range
    ///
    /// ```
    /// use condow_core::OffsetRange;
    ///
    /// let range = OffsetRange::new(4, 6);
    ///
    /// assert_eq!(range.len(), 6);
    /// ```
    pub fn len(&self) -> u64 {
        self.1
    }
}

impl fmt::Display for OffsetRange {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}({})", self.0, self.1)
    }
}

/// A closed range
///
/// A closed range has a "defined end".
/// This does not require [Condow](crate::Condow) to do a size request.
/// [Condow](crate::Condow) can be configured to do a size request anyways
/// which allows to adjust the end of the range so that the whole range
/// is part of the file. This is the default behaviour. If this
/// behaviour is disabled, it is up to the caller to ensure a valid
/// range which does not exceed the end of the file is supplied.
///
/// Naming
///
/// variants where the last index is included in the range are suffixed with
/// "Inclusive". Those missing the suffix do not include the last index which
/// makes them exlusive. This is the same convention as with the stdlib.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum ClosedRange {
    /// From including the first value to excluding the second value
    FromTo(u64, u64),
    /// From including the first value to including the second value
    FromToInclusive(u64, u64),
    /// From the beginning to excluding the value
    To(u64),
    /// From the beginning to including the value
    ToInclusive(u64),
}

impl ClosedRange {
    /// Validates the range.
    ///
    /// Fails with an error containig the reason if the range is invalid.
    ///
    /// Ranges with only 1 parameter are always valid.
    ///
    /// ## Examples
    ///
    /// [ClosedRange::FromTo]:
    ///
    /// ```
    /// use condow_core::ClosedRange;
    ///
    /// let range = ClosedRange::FromTo(0,1);
    /// assert!(range.validate().is_ok());
    ///
    /// let range = ClosedRange::FromTo(1,1);
    /// assert!(range.validate().is_ok());
    ///
    /// let range = ClosedRange::FromTo(1,0);
    /// assert!(range.validate().is_err());
    /// ```
    ///
    /// [ClosedRange::FromToInclusive]:
    ///
    /// ```
    /// use condow_core::ClosedRange;
    ///
    /// let range = ClosedRange::FromToInclusive(0,1);
    /// assert!(range.validate().is_ok());
    ///
    /// let range = ClosedRange::FromToInclusive(1,1);
    /// assert!(range.validate().is_ok());
    ///
    /// let range = ClosedRange::FromToInclusive(1,0);
    /// assert!(range.validate().is_err());
    /// ```
    pub fn validate(&self) -> Result<(), CondowError> {
        match self {
            Self::FromTo(a, b) => {
                if b < a {
                    Err(CondowError::new_invalid_range(format!(
                        "FromTo: 'to'({b}) must not be lesser than 'from'({a})"
                    )))
                } else {
                    Ok(())
                }
            }
            Self::FromToInclusive(a, b) => {
                if b < a {
                    Err(CondowError::new_invalid_range(format!(
                        "FromToInclusive: 'to'({b}) must not be lesser than 'from'({a})"
                    )))
                } else {
                    Ok(())
                }
            }
            _ => Ok(()),
        }
    }

    pub fn is_empty(&self) -> bool {
        self.len() == 0 // efficient enough since not on the hot path
    }

    pub fn len(&self) -> u64 {
        match self {
            Self::FromTo(a, b) => b - a,
            Self::FromToInclusive(a, b) => b - a + 1,
            Self::To(last_excl) => *last_excl,
            Self::ToInclusive(last_incl) => last_incl + 1,
        }
    }

    pub fn sanitized(self) -> Option<Self> {
        match self {
            Self::FromTo(a, b) => {
                if b <= a {
                    return None;
                }
            }
            Self::FromToInclusive(a, b) => {
                if b < a {
                    return None;
                }
            }
            Self::To(0) => return None,
            Self::To(_) => {}
            Self::ToInclusive(_) => {}
        }

        Some(self)
    }

    pub fn incl_range(self) -> Option<InclusiveRange> {
        let inclusive = match self {
            Self::FromTo(a, b) => {
                if a == b {
                    return None;
                }
                InclusiveRange(a, b - 1)
            }
            Self::FromToInclusive(a, b) => InclusiveRange(a, b),
            Self::To(b) => {
                if b == 0 {
                    return None;
                }
                InclusiveRange(0, b - 1)
            }
            Self::ToInclusive(b) => InclusiveRange(0, b),
        };

        Some(inclusive)
    }
}

impl fmt::Display for ClosedRange {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            ClosedRange::To(to) => write!(f, "[0..{to}["),
            ClosedRange::ToInclusive(to) => write!(f, "[0..{to}]"),
            ClosedRange::FromTo(from, to) => write!(f, "[{from}..{to}["),
            ClosedRange::FromToInclusive(from, to) => write!(f, "[{from}..{to}]"),
        }
    }
}

impl From<Range<u64>> for ClosedRange {
    fn from(r: Range<u64>) -> Self {
        ClosedRange::FromTo(r.start, r.end)
    }
}

impl From<RangeInclusive<u64>> for ClosedRange {
    fn from(r: RangeInclusive<u64>) -> Self {
        ClosedRange::FromToInclusive(*r.start(), *r.end())
    }
}

impl From<RangeTo<u64>> for ClosedRange {
    fn from(r: RangeTo<u64>) -> Self {
        ClosedRange::To(r.end)
    }
}

impl From<RangeToInclusive<u64>> for ClosedRange {
    fn from(r: RangeToInclusive<u64>) -> Self {
        ClosedRange::ToInclusive(r.end)
    }
}

impl From<InclusiveRange> for ClosedRange {
    fn from(r: InclusiveRange) -> Self {
        ClosedRange::FromToInclusive(r.0, r.1)
    }
}

/// An open range
///
/// An open range has no "defined end".
/// This always requires [Condow](crate::Condow) to do a size request
/// so that the download can be split into parts of known size.
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum OpenRange {
    /// Download from the specified byte to the end
    From(u64),
    /// Download the whole file
    Full,
}

impl OpenRange {
    pub fn incl_range_from_size(self, size: u64) -> Result<Option<InclusiveRange>, CondowError> {
        if size == 0 {
            return Ok(None);
        }

        let max_inclusive = size - 1;
        let inclusive = match self {
            Self::From(a) => InclusiveRange(a, max_inclusive),
            Self::Full => InclusiveRange(0, max_inclusive),
        };

        inclusive.validate()?;

        Ok(Some(inclusive))
    }
}

impl fmt::Display for OpenRange {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            OpenRange::From(from) => write!(f, "[{}..]", from),
            OpenRange::Full => write!(f, "[0..]"),
        }
    }
}

/// A range which specifies a download
///
/// Conversions for the standard Rust range syntax exist.
///
/// # Examples
///
/// ```rust
/// # use condow_core::*;
/// let dl = DownloadRange::from(..);
/// assert_eq!(dl, DownloadRange::Open(OpenRange::Full));
/// ```
///
/// ```rust
/// # use condow_core::*;
/// let dl = DownloadRange::from(3..);
/// assert_eq!(dl, DownloadRange::Open(OpenRange::From(3)));
/// ```
///
/// ```rust
/// # use condow_core::*;
/// let dl = DownloadRange::from(5..10);
/// assert_eq!(dl, DownloadRange::Closed(ClosedRange::FromTo(5,10)));
/// ```
///
/// ```rust
/// # use condow_core::*;
/// let dl = DownloadRange::from(5..=10);
/// assert_eq!(dl, DownloadRange::Closed(ClosedRange::FromToInclusive(5, 10)));
/// ```
///
/// ```rust
/// # use condow_core::*;
/// let dl = DownloadRange::from(..7);
/// assert_eq!(dl, DownloadRange::Closed(ClosedRange::To(7)));
/// ```
///
/// ```rust
/// # use condow_core::*;
/// let dl = DownloadRange::from(..=7);
/// assert_eq!(dl, DownloadRange::Closed(ClosedRange::ToInclusive(7)));
/// ```
///
/// ```rust
/// # use condow_core::*;
/// let dl = DownloadRange::from(InclusiveRange(1, 7));
/// assert_eq!(dl, DownloadRange::Closed(ClosedRange::FromToInclusive(1,7)));
/// ```
#[derive(Debug, Clone, Copy, PartialEq)]
pub enum DownloadRange {
    Open(OpenRange),
    Closed(ClosedRange),
}

impl DownloadRange {
    pub fn validate(&self) -> Result<(), CondowError> {
        match self {
            DownloadRange::Open(_) => Ok(()),
            DownloadRange::Closed(r) => r.validate(),
        }
    }

    pub fn sanitized(self) -> Option<Self> {
        match self {
            DownloadRange::Open(_) => Some(self),
            DownloadRange::Closed(r) => r.sanitized().map(DownloadRange::Closed),
        }
    }

    pub fn incl_range_from_size(self, size: u64) -> Result<Option<InclusiveRange>, CondowError> {
        match self {
            DownloadRange::Open(r) => r.incl_range_from_size(size),
            DownloadRange::Closed(r) => {
                let inclusive = r.incl_range();
                if let Some(inclusive) = inclusive {
                    inclusive.validate()?
                }
                Ok(inclusive)
            }
        }
    }

    /// Returns the length in bytes for ranges where an end is defined
    pub fn len(&self) -> Option<u64> {
        match self {
            DownloadRange::Open(_) => None,
            DownloadRange::Closed(r) => Some(r.len()),
        }
    }

    pub fn is_empty(&self) -> Option<bool> {
        match self {
            DownloadRange::Open(_) => None,
            DownloadRange::Closed(r) => Some(r.is_empty()),
        }
    }
}

impl fmt::Display for DownloadRange {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        match self {
            DownloadRange::Open(open) => open.fmt(f),
            DownloadRange::Closed(closed) => closed.fmt(f),
        }
    }
}

impl From<RangeFull> for DownloadRange {
    fn from(_: RangeFull) -> Self {
        Self::Open(OpenRange::Full)
    }
}

impl From<Range<u64>> for DownloadRange {
    fn from(r: Range<u64>) -> Self {
        Self::Closed(r.into())
    }
}

impl From<RangeInclusive<u64>> for DownloadRange {
    fn from(r: RangeInclusive<u64>) -> Self {
        Self::Closed(r.into())
    }
}

impl From<RangeFrom<u64>> for DownloadRange {
    fn from(r: RangeFrom<u64>) -> Self {
        Self::Open(OpenRange::From(r.start))
    }
}

impl From<RangeTo<u64>> for DownloadRange {
    fn from(r: RangeTo<u64>) -> Self {
        Self::Closed(r.into())
    }
}

impl From<RangeToInclusive<u64>> for DownloadRange {
    fn from(r: RangeToInclusive<u64>) -> Self {
        Self::Closed(r.into())
    }
}

impl From<InclusiveRange> for DownloadRange {
    fn from(r: InclusiveRange) -> Self {
        Self::Closed(r.into())
    }
}

impl From<OffsetRange> for DownloadRange {
    fn from(r: OffsetRange) -> Self {
        Self::Closed(ClosedRange::FromTo(r.start(), r.end_excl()))
    }
}

impl From<ClosedRange> for DownloadRange {
    fn from(r: ClosedRange) -> Self {
        Self::Closed(r)
    }
}

#[test]
fn range_full() {
    let result: DownloadRange = (..).into();
    assert_eq!(result, DownloadRange::Open(OpenRange::Full));
}

#[test]
fn range() {
    let result: DownloadRange = (3..10).into();
    assert_eq!(result, DownloadRange::Closed(ClosedRange::FromTo(3, 10)));
}

#[test]
fn range_inclusive() {
    let result: DownloadRange = (3..=10).into();
    assert_eq!(
        result,
        DownloadRange::Closed(ClosedRange::FromToInclusive(3, 10))
    );
}

#[test]
fn range_from() {
    let result: DownloadRange = (3..).into();
    assert_eq!(result, DownloadRange::Open(OpenRange::From(3)));
}

#[test]
fn range_to() {
    let result: DownloadRange = (..10).into();
    assert_eq!(result, DownloadRange::Closed(ClosedRange::To(10)));
}

#[test]
fn range_to_inclusive() {
    let result: DownloadRange = (..=10).into();
    assert_eq!(result, DownloadRange::Closed(ClosedRange::ToInclusive(10)));
}