qiniu_upload_token/
file_type.rs

1use duplicate::duplicate;
2use serde::{Deserialize, Serialize};
3use std::fmt::{self, Display};
4
5/// 文件存储类型
6#[derive(Copy, Clone, Debug, Eq, PartialEq, Default, Serialize, Deserialize)]
7#[serde(from = "u8", into = "u8")]
8#[non_exhaustive]
9pub enum FileType {
10    /// 标准存储
11    #[default]
12    Standard,
13
14    /// 低频存储
15    InfrequentAccess,
16
17    /// 归档存储
18    Archive,
19
20    /// 深度归档存储
21    DeepArchive,
22
23    /// 归档直读存储
24    ArchiveIR,
25
26    /// 其他存储类型
27    Other(u8),
28}
29
30impl Display for FileType {
31    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
32        usize::from(*self).fmt(f)
33    }
34}
35
36#[duplicate(
37    ty;
38    [u8];
39    [u16];
40    [u32];
41    [u64];
42    [usize];
43    [i8];
44    [i16];
45    [i32];
46    [i64];
47    [isize];
48)]
49impl From<FileType> for ty {
50    #[inline]
51    fn from(file_type: FileType) -> Self {
52        match file_type {
53            FileType::Standard => 0,
54            FileType::InfrequentAccess => 1,
55            FileType::Archive => 2,
56            FileType::DeepArchive => 3,
57            FileType::ArchiveIR => 4,
58            #[allow(clippy::unnecessary_cast)]
59            FileType::Other(ft) => ft as ty,
60        }
61    }
62}
63
64#[duplicate(
65    ty;
66    [u8];
67    [u16];
68    [u32];
69    [u64];
70)]
71impl From<ty> for FileType {
72    fn from(value: ty) -> Self {
73        #[allow(clippy::unnecessary_cast)]
74        match value as u8 {
75            0 => Self::Standard,
76            1 => Self::InfrequentAccess,
77            2 => Self::Archive,
78            3 => Self::DeepArchive,
79            4 => Self::ArchiveIR,
80            ft => Self::Other(ft),
81        }
82    }
83}
84
85#[cfg(test)]
86mod tests {
87    use super::*;
88
89    #[test]
90    fn test_file_type_serialization_and_deserialization() -> anyhow::Result<()> {
91        assert_eq!(&serde_json::to_string(&FileType::Standard)?, "0");
92        assert_eq!(&serde_json::to_string(&FileType::Other(5))?, "5");
93        assert_eq!(serde_json::from_str::<FileType>("0")?, FileType::Standard);
94        assert_eq!(serde_json::from_str::<FileType>("5")?, FileType::Other(5));
95        Ok(())
96    }
97}