Skip to content

Commit

Permalink
enhancement: Enhance Enum types with string conversion capabilities
Browse files Browse the repository at this point in the history
For certain enum types, we need to convert them to their corresponding
strings, or be able to map a given string to the corresponding enum type.
However, many enum types currently lack these helper functions. With the
help with strum macro, we can easily reach this goal. But we also take
some special cases which are partially implemented into account. For these,
myabe we just implement `impl std::fmt::Display` for it.

Fixes #173

Signed-off-by: Alex Lyn <[email protected]>
  • Loading branch information
Apokleos committed Jul 1, 2024
1 parent da761c5 commit 2612ff9
Show file tree
Hide file tree
Showing 6 changed files with 581 additions and 12 deletions.
2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,8 @@ serde_json = "1.0.66"
quickcheck = { version = "1.0.3", optional = true }
derive_builder = "0.20.0"
getset = "0.1.1"
strum = "0.26.2"
strum_macros = "0.26.2"

[dev-dependencies]
tempfile = "3.2.0"
Expand Down
4 changes: 3 additions & 1 deletion src/distribution/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,15 @@ use derive_builder::Builder;
use getset::Getters;
use serde::{Deserialize, Serialize};
use std::fmt::{self, Display, Formatter};
use strum_macros::{Display as StrumDisplay, EnumString};
use thiserror::Error;

/// The string returned by and ErrorResponse error.
pub const ERR_REGISTRY: &str = "distribution: registry returned error";

/// Unique identifier representing error code.
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize)]
#[derive(Clone, Debug, Deserialize, Eq, PartialEq, Serialize, StrumDisplay, EnumString)]
#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
#[serde(rename_all = "SCREAMING_SNAKE_CASE")]
pub enum ErrorCode {
/// Blob unknown to registry.
Expand Down
61 changes: 60 additions & 1 deletion src/runtime/capability.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,12 @@ use serde::{
};
use std::collections::HashSet;

use strum_macros::{Display, EnumString};

/// Capabilities is a unique set of Capability values.
pub type Capabilities = HashSet<Capability>;

#[derive(Clone, Copy, Debug, Eq, Hash, PartialEq, Serialize)]
#[derive(Clone, Copy, Debug, EnumString, Eq, Display, Hash, PartialEq, Serialize)]
/// All available capabilities.
///
/// For the purpose of performing permission checks, traditional UNIX
Expand All @@ -21,6 +23,7 @@ pub type Capabilities = HashSet<Capability>;
/// Starting with kernel 2.2, Linux divides the privileges traditionally
/// associated with superuser into distinct units, known as capabilities, which
/// can be independently enabled and disabled. Capabilities are a per-thread attribute.
#[strum(serialize_all = "SCREAMING_SNAKE_CASE")]
pub enum Capability {
#[serde(rename = "CAP_AUDIT_CONTROL")]
/// Enable and disable kernel auditing; change auditing filter rules;
Expand Down Expand Up @@ -609,4 +612,60 @@ mod tests {
assert!(res.contains(&Capability::Chown));
Ok(())
}

#[test]
fn invalid_string2enum() {
let invalid_cap_str = "INVALID_CAP";
let unknown_cap = invalid_cap_str.parse::<Capability>();
assert!(unknown_cap.is_err());
}

#[test]
fn cap_enum_to_string() {
let cap = Capability::AuditControl;
assert_eq!(cap.to_string(), "AUDIT_CONTROL");

let cap = Capability::AuditRead;
assert_eq!(cap.to_string(), "AUDIT_READ");

let cap = Capability::SysAdmin;
assert_eq!(cap.to_string(), "SYS_ADMIN");
}

#[test]
fn cap_string_to_enum() {
let cap_str = "AUDIT_CONTROL";
let cap_enum: Capability = cap_str.parse().unwrap();
assert_eq!(cap_enum, Capability::AuditControl);

let cap_str = "AUDIT_READ";
let cap_enum: Capability = cap_str.parse().unwrap();
assert_eq!(cap_enum, Capability::AuditRead);

let cap_str = "SYS_ADMIN";
let cap_enum: Capability = cap_str.parse().unwrap();
assert_eq!(cap_enum, Capability::SysAdmin);
}

#[test]
fn test_serde_serialization() {
let cap = Capability::AuditControl;
let serialized = serde_json::to_string(&cap).unwrap();
assert_eq!(serialized, "\"CAP_AUDIT_CONTROL\"");

let cap = Capability::SysAdmin;
let serialized = serde_json::to_string(&cap).unwrap();
assert_eq!(serialized, "\"CAP_SYS_ADMIN\"");
}

#[test]
fn test_serde_deserialization() {
let serialized = "\"CAP_AUDIT_CONTROL\"";
let cap: Capability = serde_json::from_str(serialized).unwrap();
assert_eq!(cap, Capability::AuditControl);

let serialized = "\"CAP_SYS_ADMIN\"";
let cap: Capability = serde_json::from_str(serialized).unwrap();
assert_eq!(cap, Capability::SysAdmin);
}
}
Loading

0 comments on commit 2612ff9

Please sign in to comment.