async_zip/spec/
attribute.rs

1// Copyright (c) 2022 Harry [Majored] [hello@majored.pw]
2// MIT License (https://github.com/Majored/rs-async-zip/blob/main/LICENSE)
3
4use crate::error::{Result, ZipError};
5
6/// An attribute host compatibility supported by this crate.
7#[non_exhaustive]
8#[derive(Debug, Clone, Copy, PartialEq, Eq)]
9pub enum AttributeCompatibility {
10    Unix,
11}
12
13impl TryFrom<u16> for AttributeCompatibility {
14    type Error = ZipError;
15
16    // Convert a u16 stored with little endianness into a supported attribute host compatibility.
17    // https://github.com/Majored/rs-async-zip/blob/main/SPECIFICATION.md#4422
18    fn try_from(value: u16) -> Result<Self> {
19        match value {
20            3 => Ok(AttributeCompatibility::Unix),
21            _ => Err(ZipError::AttributeCompatibilityNotSupported(value)),
22        }
23    }
24}
25
26impl From<&AttributeCompatibility> for u16 {
27    // Convert a supported attribute host compatibility into its relevant u16 stored with little endianness.
28    // https://github.com/Majored/rs-async-zip/blob/main/SPECIFICATION.md#4422
29    fn from(compatibility: &AttributeCompatibility) -> Self {
30        match compatibility {
31            AttributeCompatibility::Unix => 3,
32        }
33    }
34}
35
36impl From<AttributeCompatibility> for u16 {
37    // Convert a supported attribute host compatibility into its relevant u16 stored with little endianness.
38    fn from(compatibility: AttributeCompatibility) -> Self {
39        (&compatibility).into()
40    }
41}