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
use std::{
    fs::{self},
    io,
};

use strum_macros::Display;

use serde::{Deserialize, Serialize};

#[allow(dead_code)]
#[derive(
    Debug, Eq, PartialEq, PartialOrd, Ord, Copy, Clone, Hash, Serialize, Deserialize, Display,
)]
#[strum(serialize_all = "lowercase")]
pub enum XvcFileType {
    RecordOnly,
    File,
    Directory,
    Symlink,
    Hardlink,
    Reflink,
}

impl Default for XvcFileType {
    fn default() -> Self {
        Self::RecordOnly
    }
}

impl From<fs::Metadata> for XvcFileType {
    fn from(md: fs::Metadata) -> Self {
        let ft = md.file_type();
        if ft.is_dir() {
            Self::Directory
        } else if ft.is_file() {
            Self::File
        } else if ft.is_symlink() {
            Self::Symlink
        } else {
            Self::RecordOnly
        }
    }
}

impl From<&fs::Metadata> for XvcFileType {
    fn from(md: &fs::Metadata) -> Self {
        let ft = md.file_type();
        if ft.is_dir() {
            Self::Directory
        } else if ft.is_file() {
            Self::File
        } else if ft.is_symlink() {
            Self::Symlink
        } else {
            Self::RecordOnly
        }
    }
}

impl From<io::Result<fs::Metadata>> for XvcFileType {
    fn from(r_md: io::Result<fs::Metadata>) -> Self {
        match r_md {
            Err(_) => Self::RecordOnly,
            Ok(md) => {
                let ft = md.file_type();
                if ft.is_dir() {
                    Self::Directory
                } else if ft.is_file() {
                    Self::File
                } else if ft.is_symlink() {
                    Self::Symlink
                } else {
                    Self::RecordOnly
                }
            }
        }
    }
}