Skip to content

Commit

Permalink
add from static str for strum integration
Browse files Browse the repository at this point in the history
  • Loading branch information
Lur1an committed Jul 18, 2024
1 parent afcdf95 commit 0e103b9
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 25 deletions.
30 changes: 27 additions & 3 deletions src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,17 +13,41 @@ pub trait Entity: 'static {
fn object_type() -> &'static str;
}

/// Represents possible relations to an `Entity`
/// Easiest way to implement is implementing `Into<&'static str>` for your enum, which can builder
/// achieved with `strum::IntoStaticStr`
pub trait Relation {
fn name(&self) -> &'static str;
fn name(self) -> &'static str;
}

/// A resource is any `Entity` that also has `Permissions` associated
pub trait Resource: Entity {
type Permissions: Permission;
}

/// Represents possible permissions on a `Resource`
/// Easiest way to implement is implementing `Into<&'static str>` for your enum, which can builder
/// achieved with `strum::IntoStaticStr`
pub trait Permission {
fn name(&self) -> &'static str;
fn name(self) -> &'static str;
}

impl<S> Permission for S
where
S: Into<&'static str>,
{
fn name(self) -> &'static str {
self.into()
}
}

impl<S> Relation for S
where
S: Into<&'static str>,
{
fn name(self) -> &'static str {
self.into()
}
}

pub trait Caveat {
Expand Down Expand Up @@ -60,7 +84,7 @@ impl Caveat for NoCaveat {
pub struct NoRelations;

impl Relation for NoRelations {
fn name(&self) -> &'static str {
fn name(self) -> &'static str {
unreachable!()
}
}
Expand Down
28 changes: 6 additions & 22 deletions tests/example.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,8 @@ use pretty_assertions::assert_eq;
use spicedb_rust::spicedb::{
relationship_update, subject_reference_raw, wildcard_relationship_update, SubjectReference,
};
use spicedb_rust::{
Actor, Entity, NoRelations, Permission, Relation, RelationshipOperation, Resource,
SpiceDBClient,
};
use spicedb_rust::{Actor, Entity, NoRelations, RelationshipOperation, Resource, SpiceDBClient};
use strum::IntoStaticStr;
use uuid::Uuid;

struct User(Uuid);
Expand Down Expand Up @@ -33,20 +31,13 @@ impl Actor for User {

struct Document;

#[derive(IntoStaticStr)]
#[strum(serialize_all = "snake_case")]
pub enum DocumentPermission {
Read,
Write,
}

impl Permission for DocumentPermission {
fn name(&self) -> &'static str {
match self {
DocumentPermission::Read => "read",
DocumentPermission::Write => "write",
}
}
}

impl Entity for Document {
type Relations = DocumentRelation;
type Id = String;
Expand All @@ -56,20 +47,13 @@ impl Entity for Document {
}
}

#[derive(IntoStaticStr)]
#[strum(serialize_all = "snake_case")]
pub enum DocumentRelation {
Reader,
Writer,
}

impl Relation for DocumentRelation {
fn name(&self) -> &'static str {
match self {
DocumentRelation::Reader => "reader",
DocumentRelation::Writer => "writer",
}
}
}

impl Resource for Document {
type Permissions = DocumentPermission;
}
Expand Down

0 comments on commit 0e103b9

Please sign in to comment.