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
// Copyright (C) 2019 O.S. Systems Sofware LTDA
//
// SPDX-License-Identifier: Apache-2.0

use serde::{de, Deserialize, Deserializer};

/// How many `ChunkSize` blocks must be copied from the source file to
/// the target. The default value of -1 means all possible bytes
/// until the end of the file.
#[derive(PartialEq, Debug, Clone)]
pub enum Count {
    All,
    Limited(isize),
}

impl Default for Count {
    fn default() -> Self {
        Count::All
    }
}

impl<'de> Deserialize<'de> for Count {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        match isize::deserialize(deserializer)? {
            -1 => Ok(Count::All),
            n if n >= 0 => Ok(Count::Limited(n)),
            n => Err(de::Error::custom(format!("Invalid count: {}", n))),
        }
    }
}

impl std::iter::Iterator for Count {
    type Item = isize;

    fn next(&mut self) -> Option<Self::Item> {
        match self {
            Count::All => Some(0),
            Count::Limited(ref mut n) => match n {
                0 => None,
                n => {
                    *n -= 1;
                    Some(*n)
                }
            },
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;
    use pretty_assertions::assert_eq;
    use serde_json::json;

    #[derive(Debug, PartialEq, Deserialize)]
    struct Payload {
        #[serde(default)]
        count: Count,
    }

    #[test]
    fn deserialize() {
        assert_eq!(
            serde_json::from_value::<Payload>(json!({ "count": 0 })).unwrap(),
            Payload { count: Count::Limited(0) }
        );
    }

    #[test]
    fn default() {
        assert_eq!(
            serde_json::from_value::<Payload>(json!({})).unwrap(),
            Payload { count: Count::All }
        );
    }

    #[test]
    fn validation_of_minimal() {
        assert!(serde_json::from_value::<Payload>(json!({ "count": -2 })).is_err());
    }
}