dia_files/permissions/impls/
raw_permission.rs

1/*
2==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--==--
3
4Dia-Files
5
6Copyright (C) 2019-2024  Anonymous
7
8There are several releases over multiple years,
9they are listed as ranges, such as: "2019-2024".
10
11This program is free software: you can redistribute it and/or modify
12it under the terms of the GNU Lesser General Public License as published by
13the Free Software Foundation, either version 3 of the License, or
14(at your option) any later version.
15
16This program is distributed in the hope that it will be useful,
17but WITHOUT ANY WARRANTY; without even the implied warranty of
18MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19GNU Lesser General Public License for more details.
20
21You should have received a copy of the GNU Lesser General Public License
22along with this program.  If not, see <https://www.gnu.org/licenses/>.
23
24::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--::--
25*/
26
27#[cfg(unix)]
28use {
29    std::io::{Error, ErrorKind},
30    crate::Result,
31    super::super::{FilePermissions, Permissions, RawPermission},
32};
33
34#[cfg(test)]
35mod tests;
36
37#[cfg(unix)]
38macro_rules! impl_from_file_permissions_for_raw_permission { ($($file_permissions: ty,)+) => {
39    $(
40        #[cfg(unix)]
41        #[doc(cfg(unix))]
42        impl From<$file_permissions> for RawPermission {
43
44            fn from(file_permissions: $file_permissions) -> Self {
45                ((file_permissions.user as RawPermission) << 6)
46                    | ((file_permissions.group as RawPermission) << 3)
47                    | (file_permissions.others as RawPermission)
48            }
49
50        }
51    )+
52}}
53
54#[cfg(unix)]
55impl_from_file_permissions_for_raw_permission! { &FilePermissions, FilePermissions, }
56
57#[cfg(unix)]
58#[doc(cfg(unix))]
59impl TryFrom<RawPermission> for Permissions {
60
61    type Error = Error;
62
63    fn try_from(raw_permission: RawPermission) -> Result<Self> {
64        if raw_permission == Permissions::None as RawPermission {
65            Ok(Permissions::None)
66        } else if raw_permission == Permissions::Read as RawPermission {
67            Ok(Permissions::Read)
68        } else if raw_permission == Permissions::Write as RawPermission {
69            Ok(Permissions::Write)
70        } else if raw_permission == Permissions::Execute as RawPermission {
71            Ok(Permissions::Execute)
72        } else if raw_permission == Permissions::ReadWrite as RawPermission {
73            Ok(Permissions::ReadWrite)
74        } else if raw_permission == Permissions::ReadExecute as RawPermission {
75            Ok(Permissions::ReadExecute)
76        } else if raw_permission == Permissions::WriteExecute as RawPermission {
77            Ok(Permissions::WriteExecute)
78        } else if raw_permission == Permissions::ReadWriteExecute as RawPermission {
79            Ok(Permissions::ReadWriteExecute)
80        } else {
81            Err(Error::new(ErrorKind::Other, __!("Unknown permissions: {}", raw_permission)))
82        }
83    }
84
85}
86
87#[cfg(unix)]
88#[doc(cfg(unix))]
89impl TryFrom<&RawPermission> for Permissions {
90
91    type Error = Error;
92
93    fn try_from(raw_permission: &RawPermission) -> Result<Self> {
94        Self::try_from(*raw_permission)
95    }
96
97}
98
99#[cfg(unix)]
100#[doc(cfg(unix))]
101impl TryFrom<&RawPermission> for FilePermissions {
102
103    type Error = Error;
104
105    fn try_from(r: &RawPermission) -> Result<Self> {
106        let r = *r;
107        Ok(Self::new(
108            Permissions::try_from(r >> 6)?,
109            Permissions::try_from((r >> 3) & 0b_000_111)?,
110            Permissions::try_from(r & 0b_000_000_111)?,
111        ))
112    }
113
114}
115
116#[cfg(unix)]
117#[doc(cfg(unix))]
118impl TryFrom<RawPermission> for FilePermissions {
119
120    type Error = Error;
121
122    fn try_from(r: RawPermission) -> Result<Self> {
123        Self::try_from(&r)
124    }
125
126}