Skip to main content

windows_erg/security/
target.rs

1//! Security target helpers.
2
3use crate::Result;
4
5use super::SecurityDescriptor;
6use super::backends;
7
8/// Permission target reference.
9#[derive(Debug, Clone, PartialEq, Eq)]
10pub enum PermissionTarget {
11    /// File or directory path.
12    FilePath(String),
13    /// Registry key path.
14    RegistryPath(String),
15}
16
17impl PermissionTarget {
18    /// Build a file target.
19    pub fn file(path: impl Into<String>) -> Self {
20        PermissionTarget::FilePath(path.into())
21    }
22
23    /// Build a registry target.
24    pub fn registry(path: impl Into<String>) -> Self {
25        PermissionTarget::RegistryPath(path.into())
26    }
27
28    /// Read current descriptor for target.
29    pub fn read_descriptor(&self) -> Result<SecurityDescriptor> {
30        match self {
31            PermissionTarget::FilePath(path) => backends::file::read_descriptor(path),
32            PermissionTarget::RegistryPath(path) => backends::registry::read_descriptor(path),
33        }
34    }
35
36    /// Write descriptor to target.
37    pub fn write_descriptor(&self, descriptor: &SecurityDescriptor) -> Result<()> {
38        match self {
39            PermissionTarget::FilePath(path) => backends::file::write_descriptor(path, descriptor),
40            PermissionTarget::RegistryPath(path) => {
41                backends::registry::write_descriptor(path, descriptor)
42            }
43        }
44    }
45}