ouisync/
joint_entry.rs

1use crate::{
2    directory::EntryType,
3    error::{Error, Result},
4    file::File,
5    joint_directory::JointDirectory,
6};
7
8#[derive(Debug)]
9pub enum JointEntry {
10    File(File),
11    Directory(JointDirectory),
12}
13
14#[allow(clippy::len_without_is_empty)]
15impl JointEntry {
16    pub fn entry_type(&self) -> EntryType {
17        match self {
18            Self::File(_) => EntryType::File,
19            Self::Directory(_) => EntryType::Directory,
20        }
21    }
22
23    pub fn into_file(self) -> Result<File> {
24        match self {
25            Self::File(file) => Ok(file),
26            Self::Directory(_) => Err(Error::EntryIsDirectory),
27        }
28    }
29
30    pub fn into_directory(self) -> Result<JointDirectory> {
31        match self {
32            Self::File(_) => Err(Error::EntryIsFile),
33            Self::Directory(dirs) => Ok(dirs),
34        }
35    }
36
37    pub fn as_file_mut(&mut self) -> Result<&mut File> {
38        match self {
39            Self::File(file) => Ok(file),
40            Self::Directory(_) => Err(Error::EntryIsDirectory),
41        }
42    }
43
44    pub fn as_directory(&self) -> Result<&JointDirectory> {
45        match self {
46            Self::File(_) => Err(Error::EntryIsFile),
47            Self::Directory(dirs) => Ok(dirs),
48        }
49    }
50
51    pub fn as_directory_mut(&mut self) -> Result<&mut JointDirectory> {
52        match self {
53            Self::File(_) => Err(Error::EntryIsFile),
54            Self::Directory(dirs) => Ok(dirs),
55        }
56    }
57
58    /// Length of the entry in bytes.
59    pub fn len(&self) -> u64 {
60        match self {
61            Self::File(file) => file.len(),
62            Self::Directory(dir) => dir.len(),
63        }
64    }
65}