Skip to main content

PermissionMapping

Struct PermissionMapping 

Source
pub struct PermissionMapping { /* private fields */ }
Expand description

Mapping between a permission name and its deterministic permission ID.

This type stores the relationship between:

  • the normalized permission string (trimmed and lowercased)
  • the computed 64-bit permission ID used in bitmap storage

§Purpose

This mapping enables reverse lookup from permission IDs back to their normalized string representations, which is useful for:

  • Debugging and logging
  • Administrative interfaces
  • Audit trails
  • Permission reporting

§Design Principles

  • Immutable once created; construct via From<&str>/From<String> or PermissionMapping::new(original, id)
  • Contains only the normalized string and computed ID (the original input form is not retained)
  • Validates consistency between string and ID during construction with new; validate() can be used to re-check invariants

§Examples

use webgates_core::permissions::permission_id::PermissionId;
use webgates_core::permissions::mapping::PermissionMapping;

// Create from a permission string
let mapping = PermissionMapping::from("Read:API");
assert_eq!(mapping.normalized_string(), "read:api");
assert_eq!(mapping.permission_id(), PermissionId::from("Read:API"));

// Create from components (useful for deserialization)
let id = PermissionId::from("write:file");
let mapping = PermissionMapping::new("Write:File", id).unwrap();

§Validation

The mapping validates that the provided permission ID actually corresponds to the normalized string to prevent inconsistent state.

§Construction

Prefer PermissionMapping::from(<&str|String>) when you have the permission in string form. Use PermissionMapping::new(original, id) when deserializing or when both pieces are provided and must be validated.

§Serialization

This type derives Serialize/Deserialize. The serialized shape contains normalized_string and permission_id.

Implementations§

Source§

impl PermissionMapping

Source

pub fn new( original: impl Into<String>, id: PermissionId, ) -> Result<Self, PermissionMappingError>

Creates a new mapping from a permission string and an existing ID.

This constructor validates that the provided ID actually corresponds to the normalized string so inconsistent state cannot be created by mistake.

§Arguments
  • original - The original permission string as provided
  • id - The permission ID that must correspond to the normalized form of original

Normalization is handled internally from original (trim + lowercase)

§Returns

Returns Ok(PermissionMapping) if the ID matches the normalized string, or Err(PermissionMappingError) if there’s a mismatch.

§Examples
use webgates_core::permissions::permission_id::PermissionId;
use webgates_core::permissions::mapping::PermissionMapping;

let id = PermissionId::from("read:api");
let mapping = PermissionMapping::new("Read:API", id).unwrap();
Source

pub fn normalized_string(&self) -> &str

Returns the normalized permission string.

The normalized form is trimmed and lowercased. This is the exact value used to compute the permission ID.

Source

pub fn permission_id(&self) -> PermissionId

Returns the computed permission ID.

This is the 64-bit identifier that would be stored in the permissions bitmap.

Source

pub fn id_as_u64(&self) -> u64

Returns the permission ID as a raw u64 value.

This is a convenience method for storage, diagnostics, or comparisons.

Source

pub fn matches_string(&self, permission: &str) -> bool

Checks whether this mapping corresponds to the given permission string.

The comparison uses the normalized form of the input, so it ignores case and surrounding whitespace differences.

§Examples
use webgates_core::permissions::mapping::PermissionMapping;

let mapping = PermissionMapping::from("read:api");
assert!(mapping.matches_string("READ:API"));
assert!(mapping.matches_string("  read:api  "));
assert!(!mapping.matches_string("write:api"));
Source

pub fn matches_id(&self, id: PermissionId) -> bool

Checks whether this mapping corresponds to the given permission ID.

§Examples
use webgates_core::permissions::permission_id::PermissionId;
use webgates_core::permissions::mapping::PermissionMapping;

let mapping = PermissionMapping::from("read:api");
let id = PermissionId::from("read:api");
assert!(mapping.matches_id(id));
Source

pub fn validate(&self) -> Result<(), PermissionMappingError>

Validates that this mapping is internally consistent.

This checks that the permission ID actually corresponds to the normalized string, which should always be true for properly constructed mappings.

Note: Calling this is typically only necessary when a mapping is created via serde deserialization. Constructors from strings (From<&str>/From<String>) and PermissionMapping::new(original, id) enforce the invariant at creation time.

Returns Ok(()) if consistent, or Err(PermissionMappingError) if not.

Trait Implementations§

Source§

impl Clone for PermissionMapping

Source§

fn clone(&self) -> PermissionMapping

Returns a duplicate of the value. Read more
1.0.0 (const: unstable) · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for PermissionMapping

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl<'de> Deserialize<'de> for PermissionMapping

Source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>
where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
Source§

impl Display for PermissionMapping

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl From<&str> for PermissionMapping

Source§

fn from(permission: &str) -> Self

Converts to this type from the input type.
Source§

impl From<String> for PermissionMapping

Source§

fn from(permission: String) -> Self

Converts to this type from the input type.
Source§

impl Hash for PermissionMapping

Source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · Source§

fn hash_slice<H>(data: &[Self], state: &mut H)
where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
Source§

impl PartialEq for PermissionMapping

Source§

fn eq(&self, other: &PermissionMapping) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 (const: unstable) · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Serialize for PermissionMapping

Source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>
where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
Source§

impl Eq for PermissionMapping

Source§

impl StructuralPartialEq for PermissionMapping

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T> Instrument for T

Source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
Source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
Source§

impl<T> WithSubscriber for T

Source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>
where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more
Source§

impl<T> DeserializeOwned for T
where T: for<'de> Deserialize<'de>,