keypropdecode/props/implementations/
from.rs1use crate::{
2 props::{ArcDir, ArchiveProps},
3 Props,
4};
5
6#[cfg(windows)]
7use std::os::windows::prelude::*;
8use std::{fs::Metadata, path::PathBuf};
9
10use crate::error::*;
11
12const READ_ONLY: u32 = 1;
13const HIDDEN: u32 = 1 << 1;
14const SYSTEM: u32 = 1 << 2;
15const DIRECTORY: u32 = 1 << 4;
16const ARCHIVE: u32 = 1 << 5;
17const DEVICE: u32 = 1 << 6;
18const NORMAL: u32 = 1 << 7;
19const TEMPORARY: u32 = 1 << 8;
20const SPARSE: u32 = 1 << 9;
21const REPARSE: u32 = 1 << 10;
22const COMPRESSED: u32 = 1 << 11;
23const OFFLINE: u32 = 1 << 12;
24const NOT_CONTENT_INDEXED: u32 = 1 << 13;
25const ENCRYPTED: u32 = 1 << 14;
26const INTEGRITY_STREAM: u32 = 1 << 15;
27const VIRTUAL_FILE: u32 = 1 << 16;
28const NO_SCRUB_DATA: u32 = 1 << 17;
29const EXTENDED_ATTRIBUTES: u32 = 1 << 18;
30const PINNED: u32 = 1 << 19;
31const UNPINNED: u32 = 1 << 20;
32const RECALL_ON_OPEN: u32 = 1 << 21;
33const RECALL_ON_DATA_ACCESS: u32 = 1 << 22;
34impl From<u32> for Props {
35 fn from(value: u32) -> Self {
36 let mut props = Self::default();
40 if value & DIRECTORY == DIRECTORY {
41 props.element_type = ArcDir::Directory;
42 }
43 if value & ARCHIVE == ARCHIVE {
44 let mut arc_props = ArchiveProps::default();
45 if value & READ_ONLY == READ_ONLY {
46 arc_props.read_only = true;
47 }
48 if value & NORMAL == NORMAL {
49 arc_props.normal = true;
50 }
51 if value & TEMPORARY == TEMPORARY {
52 arc_props.temporary = true;
53 }
54 if value & SPARSE == SPARSE {
55 arc_props.sparse = true;
56 }
57 if value & OFFLINE == OFFLINE {
58 arc_props.offline = true;
59 }
60 props.element_type = ArcDir::Archive(arc_props);
61 }
62 if value & HIDDEN == HIDDEN {
63 props.hidden = true;
64 }
65 if value & SYSTEM == SYSTEM {
66 props.system = true;
67 }
68 if value & DEVICE == DEVICE {
69 props.device = true;
70 }
71 if value & REPARSE == REPARSE {
72 props.reparse = true;
73 }
74 if value & COMPRESSED == COMPRESSED {
75 props.compressed = true;
76 }
77 if value & NOT_CONTENT_INDEXED == NOT_CONTENT_INDEXED {
78 props.not_content_indexed = true;
79 }
80 if value & ENCRYPTED == ENCRYPTED {
81 props.encrypted = true;
82 }
83 if value & INTEGRITY_STREAM == INTEGRITY_STREAM {
84 props.integrity_stream = true;
85 }
86 if value & VIRTUAL_FILE == VIRTUAL_FILE {
87 props.virtual_file = true;
88 }
89 if value & NO_SCRUB_DATA == NO_SCRUB_DATA {
90 props.no_scrub_data = true;
91 }
92 if value & EXTENDED_ATTRIBUTES == EXTENDED_ATTRIBUTES {
93 props.extended_attributes = true;
94 }
95 if value & PINNED == PINNED {
96 props.pinned = true;
97 }
98 if value & UNPINNED == UNPINNED {
99 props.unpinned = true;
100 }
101 if value & RECALL_ON_OPEN == RECALL_ON_OPEN {
102 props.recall_on_open = true;
103 }
104 if value & RECALL_ON_DATA_ACCESS == RECALL_ON_DATA_ACCESS {
105 props.recall_on_data_access = true;
106 }
107 props
108 }
109}
110#[cfg(windows)]
111impl TryFrom<PathBuf> for Props {
112 type Error = crate::error::Error;
113 fn try_from(value: PathBuf) -> std::prelude::v1::Result<Self, Self::Error> {
114 let metadata: Metadata = match std::fs::metadata(value.clone()) {
115 Ok(obtained_metadata) => obtained_metadata,
116 Err(_) => {
117 return Err(Error {
118 kind: ErrorKind::FileNotFound,
119 })
120 }
121 };
122 Ok(Props::from(metadata.file_attributes()))
123 }
124}
125#[cfg(windows)]
126impl TryFrom<&PathBuf> for Props {
127 type Error = crate::error::Error;
128 fn try_from(value: &PathBuf) -> std::prelude::v1::Result<Self, Self::Error> {
129 let metadata: Metadata = match std::fs::metadata(value.clone()) {
130 Ok(obtained_metadata) => obtained_metadata,
131 Err(_) => {
132 return Err(Error {
133 kind: ErrorKind::FileNotFound,
134 })
135 }
136 };
137 Ok(Props::from(metadata.file_attributes()))
138 }
139}
140#[cfg(windows)]
141impl TryFrom<&str> for Props {
142 type Error = crate::error::Error;
143 fn try_from(value: &str) -> std::prelude::v1::Result<Self, Self::Error> {
144 let path = PathBuf::from(value);
145 Props::try_from(path)
146 }
147}
148impl From<Props> for u32 {
149 fn from(value: Props) -> Self {
150 let mut result = 0;
151 if let ArcDir::Archive(arc_props) = value.element_type {
152 if arc_props.read_only {
153 result += 0b1;
154 }
155 if arc_props.normal {
156 result += 1 << 7;
157 }
158 if arc_props.temporary {
159 result += 1 << 8;
160 }
161 if arc_props.sparse {
162 result += 1 << 9;
163 }
164 if arc_props.offline {
165 result += 1 << 12;
166 }
167 result += 1 << 5;
168 }
169 if value.hidden {
170 result += 1 << 1;
171 }
172 if value.system {
173 result += 1 << 2;
174 }
175 if let ArcDir::Directory = value.element_type {
176 result += 1 << 4;
177 }
178 if value.device {
179 result += 1 << 6;
180 }
181 if value.reparse {
182 result += 1 << 10;
183 }
184 if value.compressed {
185 result += 1 << 11;
186 }
187 if value.not_content_indexed {
188 result += 1 << 13;
189 }
190 if value.encrypted {
191 result += 1 << 14;
192 }
193 if value.integrity_stream {
194 result += 1 << 15;
195 }
196 if value.virtual_file {
197 result += 1 << 16;
198 }
199 if value.no_scrub_data {
200 result += 1 << 17;
201 }
202 if value.extended_attributes {
203 result += 1 << 18;
204 }
205 if value.pinned {
206 result += 1 << 19;
207 }
208 if value.unpinned {
209 result += 1 << 20;
210 }
211 if value.recall_on_open {
212 result += 1 << 21;
213 }
214 if value.recall_on_data_access {
215 result += 1 << 22;
216 }
217 result
218 }
219}