Skip to content

Commit

Permalink
fix(libscoop|config): support named use_isolated_path
Browse files Browse the repository at this point in the history
Signed-off-by: Chawye Hsu <su+git@chawyehsu.com>
  • Loading branch information
chawyehsu committed May 15, 2024
1 parent 694a263 commit 5e9a181
Showing 1 changed file with 33 additions and 2 deletions.
35 changes: 33 additions & 2 deletions crates/libscoop/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use std::collections::HashMap;
use std::io::Read;
use std::ops::Deref;
use std::path::{Path, PathBuf};
use std::str::FromStr;

use crate::error::{Error, Fallible};
use crate::internal;
Expand Down Expand Up @@ -132,7 +133,7 @@ pub struct ConfigInner {
show_manifest: Option<bool>,

#[serde(skip_serializing_if = "Option::is_none")]
use_isolated_path: Option<bool>,
use_isolated_path: Option<IsolatedPath>,

#[serde(alias = "msiextract_use_lessmsi")]
#[serde(skip_serializing_if = "Option::is_none")]
Expand Down Expand Up @@ -200,6 +201,36 @@ pub struct PrivateHosts {
headers: String,
}

#[derive(Clone, Debug, Deserialize, Serialize)]
#[serde(untagged)]
pub enum IsolatedPath {
/// boolean type of `use_isolated_path`
Boolean(bool),

/// string type of `use_isolated_path` indicating the environment variable name
Name(String),
}

impl FromStr for IsolatedPath {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let s = s.to_ascii_lowercase();

// `=` is not a valid character in environment variable names
// ref: https://learn.microsoft.com/en-us/windows/win32/procthread/environment-variables
if s.contains("=") {
return Err(Error::ConfigValueInvalid(s));
}

match s.as_str() {
"true" => Ok(IsolatedPath::Boolean(true)),
"false" => Ok(IsolatedPath::Boolean(false)),
_ => Ok(IsolatedPath::Name(s)),
}
}
}

impl Config {
/// Initialize the config with default values.
///
Expand Down Expand Up @@ -284,7 +315,7 @@ impl Config {
}
"use_isolated_path" => match is_unset {
true => self.inner.use_isolated_path = None,
false => match value.parse::<bool>() {
false => match value.parse::<IsolatedPath>() {
Ok(value) => self.inner.use_isolated_path = Some(value),
Err(_) => return Err(Error::ConfigValueInvalid(value.to_owned())),
},
Expand Down

0 comments on commit 5e9a181

Please sign in to comment.