Skip to content

Commit

Permalink
feat(pedm): fix ID deserializing
Browse files Browse the repository at this point in the history
  • Loading branch information
kbouchard-dev committed Aug 12, 2024
1 parent 5923ae6 commit 7d30119
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 5 deletions.
2 changes: 1 addition & 1 deletion crates/devolutions-pedm-shared/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,6 @@ pin-project = { version = "1.1.5", optional = true }

[features]
pedm_client = ["hyper", "serde_json", "tokio", "win-api-wrappers", "policy", "serde", "devolutions-pedm-client-http", "tower", "pin-project", "anyhow"]
policy = ["regex", "schemars", "serde", "glob"]
policy = ["regex", "schemars", "serde", "glob", "anyhow"]
build = []
desktop = ["windows-registry", "win-api-wrappers", "anyhow"]
30 changes: 26 additions & 4 deletions crates/devolutions-pedm-shared/src/policy.rs
Original file line number Diff line number Diff line change
Expand Up @@ -324,12 +324,32 @@ pub struct ElevationConfigurations {

#[repr(transparent)]
#[derive(Serialize, Deserialize, JsonSchema, Debug, PartialEq, Eq, Hash, Clone)]
#[validate(regex = "ID_PATTERN")]
pub struct Id(String);
#[serde(try_from = "String")]
pub struct Id {
id: String,
}

impl Id {
pub fn try_new(id: String) -> anyhow::Result<Self> {
let pat = Regex::new(ID_PATTERN)?;

pat.is_match(&id)
.then(|| Id { id })
.ok_or_else(|| anyhow::anyhow!("Invalid ID"))
}
}

impl TryFrom<String> for Id {
type Error = anyhow::Error;

fn try_from(value: String) -> Result<Self, Self::Error> {
Id::try_new(value)
}
}

impl fmt::Display for Id {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
self.0.fmt(f)
self.id.fmt(f)
}
}

Expand Down Expand Up @@ -359,7 +379,9 @@ impl Identifiable for Profile {
impl Default for Profile {
fn default() -> Self {
Self {
id: Id("".to_owned()),
id: Id {
id: "default".to_owned(),
},
name: "Unnamed profile".to_owned(),
elevation_method: ElevationMethod::LocalAdmin,
elevation_settings: ElevationConfigurations::default(),
Expand Down

0 comments on commit 7d30119

Please sign in to comment.