diff options
author | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:41:35 +0000 |
---|---|---|
committer | Daniel Baumann <daniel.baumann@progress-linux.org> | 2024-05-04 12:41:35 +0000 |
commit | 7e5d7eea9c580ef4b41a765bde624af431942b96 (patch) | |
tree | 2c0d9ca12878fc4525650aa4e54d77a81a07cc09 /vendor/gix-sec/src/trust.rs | |
parent | Adding debian version 1.70.0+dfsg1-9. (diff) | |
download | rustc-7e5d7eea9c580ef4b41a765bde624af431942b96.tar.xz rustc-7e5d7eea9c580ef4b41a765bde624af431942b96.zip |
Merging upstream version 1.70.0+dfsg2.
Signed-off-by: Daniel Baumann <daniel.baumann@progress-linux.org>
Diffstat (limited to 'vendor/gix-sec/src/trust.rs')
-rw-r--r-- | vendor/gix-sec/src/trust.rs | 56 |
1 files changed, 56 insertions, 0 deletions
diff --git a/vendor/gix-sec/src/trust.rs b/vendor/gix-sec/src/trust.rs new file mode 100644 index 000000000..274c5b780 --- /dev/null +++ b/vendor/gix-sec/src/trust.rs @@ -0,0 +1,56 @@ +use crate::Trust; + +impl Trust { + /// Derive `Full` trust if `path` is owned by the user executing the current process, or `Reduced` trust otherwise. + pub fn from_path_ownership(path: impl AsRef<std::path::Path>) -> std::io::Result<Self> { + Ok(if crate::identity::is_path_owned_by_current_user(path.as_ref())? { + Trust::Full + } else { + Trust::Reduced + }) + } +} + +/// A trait to help creating default values based on a trust level. +pub trait DefaultForLevel { + /// Produce a default value for the given trust `level`. + fn default_for_level(level: Trust) -> Self; +} + +/// Associate instructions for how to deal with various `Trust` levels as they are encountered in the wild. +pub struct Mapping<T> { + /// The value for fully trusted resources. + pub full: T, + /// The value for resources with reduced trust. + pub reduced: T, +} + +impl<T> Default for Mapping<T> +where + T: DefaultForLevel, +{ + fn default() -> Self { + Mapping { + full: T::default_for_level(Trust::Full), + reduced: T::default_for_level(Trust::Reduced), + } + } +} + +impl<T> Mapping<T> { + /// Obtain the value for the given trust `level`. + pub fn by_level(&self, level: Trust) -> &T { + match level { + Trust::Full => &self.full, + Trust::Reduced => &self.reduced, + } + } + + /// Obtain the value for the given `level` once. + pub fn into_value_by_level(self, level: Trust) -> T { + match level { + Trust::Full => self.full, + Trust::Reduced => self.reduced, + } + } +} |