Skip to main content

Scope

Enum Scope 

Source
pub enum Scope {
    None,
    User(String),
    Tenant(String),
}
Expand description

The subject of a flag check.

Three kinds, because three are what applications actually roll out to:

  • a user, by whatever identifier they log in with;
  • a tenant, when a feature is bought or enabled per customer rather than per person;
  • nobody in particular (Scope::none), for a switch that is on or off for the whole installation.

The identifier is a String and not a generic parameter on purpose. A flag store has to write it down, an operator has to type it into a console, and a percentage rollout has to hash it — all three want text, and a type parameter here would spread through every signature in the crate to buy nothing.

use rustlavel_flags::Scope;

assert_eq!(Scope::user("ada").id(), "ada");
assert_eq!(Scope::user_id(41).id(), "41");
assert_eq!(Scope::tenant("acme").key(), "tenant:acme");
assert_eq!(Scope::none().id(), "");

Variants§

§

None

Everybody. Built with Scope::none.

§

User(String)

One logged-in user, by their authentication identifier.

§

Tenant(String)

One tenant, team, organisation — whatever the application calls it.

Implementations§

Source§

impl Scope

Source

pub fn none() -> Self

The global scope: a flag that is simply on or off, for everyone.

Named none rather than global because of how it reads at the call site — flags.active("maintenance", &Scope::none()) says “there is nobody in particular here”, which is the fact the caller has.

Source

pub fn user(id: impl Into<String>) -> Self

One user, by the identifier they log in with.

Source

pub fn user_id(id: i64) -> Self

One user, by a numeric primary key — the common case in an application whose users table is keyed by bigint.

Source

pub fn tenant(name: impl Into<String>) -> Self

One tenant, by name or by id.

Source

pub fn id(&self) -> &str

The identifier, or "" for Scope::none.

The empty string is deliberate: a resolver that does not care which kind of scope it was given can read scope.id() unconditionally, and a global check falls through whatever test it applies rather than panicking or matching some sentinel value.

Source

pub fn id_as<T: FromStr>(&self) -> Option<T>

The identifier parsed into a type: scope.id_as::<i64>().

None when the scope is global, or when the identifier is not that type — a resolver keyed on a numeric user id gets one answer for both, which is right, because it cannot serve either.

Source

pub fn kind(&self) -> &'static str

"global", "user" or "tenant" — for logs and for the store key.

Source

pub fn is_none(&self) -> bool

Whether this is the global scope.

Source

pub fn key(&self) -> String

The string a FlagStore writes an override under.

The kind is part of the key, so a user called acme and a tenant called acme are two different subjects and cannot inherit each other’s overrides. An identifier containing a colon is left alone rather than escaped — the prefix already separates the namespaces, and the only way two keys can still collide is if one subject’s identifier is literally another kind’s prefix plus another subject’s identifier, which is a collision between two identifiers the application itself issued.

Trait Implementations§

Source§

impl Clone for Scope

Source§

fn clone(&self) -> Self

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 Scope

Source§

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

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

impl Default for Scope

Source§

fn default() -> Self

Returns the “default value” for a type. Read more
Source§

impl Display for Scope

Source§

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

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

impl Eq for Scope

Source§

impl Hash for Scope

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 Ord for Scope

Source§

fn cmp(&self, other: &Self) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 (const: unstable) · Source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 (const: unstable) · Source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 (const: unstable) · Source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized,

Restrict a value to a certain interval. Read more
Source§

fn clamp_to<R>(self, range: R) -> Self
where Self: Sized, R: ClampBounds<Self>,

🔬This is a nightly-only experimental API. (clamp_to)
Restrict a value to a certain range. Read more
Source§

impl PartialEq for Scope

Source§

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

Equality operator ==. Read more
1.0.0 (const: unstable) · Source§

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

Inequality operator !=. Read more
Source§

impl PartialOrd for Scope

Source§

fn partial_cmp(&self, other: &Self) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than (for self and other) and is used by the < operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 (const: unstable) · Source§

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

Tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Source§

impl StructuralPartialEq for Scope

Auto Trait Implementations§

§

impl Freeze for Scope

§

impl RefUnwindSafe for Scope

§

impl Send for Scope

§

impl Sync for Scope

§

impl Unpin for Scope

§

impl UnsafeUnpin for Scope

§

impl UnwindSafe for Scope

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, 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> Same for T

Source§

type Output = T

Should always be Self
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 = !

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

fn try_from(value: U) -> Result<T, !>

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.