Skip to main content

zarrs_codec/
bytes_representation.rs

1use derive_more::Display;
2
3/// The decoded representation of `bytes`.
4#[derive(Copy, Clone, Eq, PartialEq, Debug, Display)]
5pub enum BytesRepresentation {
6    /// The output size is fixed.
7    #[display("fixed size: {_0}")]
8    FixedSize(u64),
9    /// The output size is bounded.
10    #[display("bounded size: {_0}")]
11    BoundedSize(u64),
12    /// The output size is unbounded/indeterminate.
13    #[display("unbounded size")]
14    UnboundedSize,
15}
16
17impl BytesRepresentation {
18    /// Return the fixed or bounded size of the bytes representations, or [`None`] if the size is unbounded.
19    #[must_use]
20    pub const fn size(&self) -> Option<u64> {
21        match self {
22            Self::FixedSize(size) | Self::BoundedSize(size) => Some(*size),
23            Self::UnboundedSize => None,
24        }
25    }
26}
27
28#[cfg(test)]
29mod tests {
30    use super::*;
31
32    #[test]
33    fn bytes_representation() {
34        let bytes_representation_fixed = BytesRepresentation::FixedSize(10);
35        assert_eq!(bytes_representation_fixed.size(), Some(10));
36        assert_eq!(
37            bytes_representation_fixed,
38            bytes_representation_fixed.clone()
39        );
40        let bytes_representation_bounded = BytesRepresentation::BoundedSize(10);
41        assert_eq!(bytes_representation_bounded.size(), Some(10));
42        assert_ne!(bytes_representation_fixed, bytes_representation_bounded);
43        let bytes_representation_unbounded = BytesRepresentation::UnboundedSize;
44        assert_eq!(bytes_representation_unbounded.size(), None);
45    }
46}