diff --git a/src/entity.rs b/src/entity.rs index 047b3de..7f35ffa 100644 --- a/src/entity.rs +++ b/src/entity.rs @@ -13,8 +13,11 @@ 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 @@ -22,8 +25,29 @@ 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 Permission for S +where + S: Into<&'static str>, +{ + fn name(self) -> &'static str { + self.into() + } +} + +impl Relation for S +where + S: Into<&'static str>, +{ + fn name(self) -> &'static str { + self.into() + } } pub trait Caveat { @@ -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!() } } diff --git a/tests/example.rs b/tests/example.rs index efd4a7e..381fb27 100644 --- a/tests/example.rs +++ b/tests/example.rs @@ -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); @@ -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; @@ -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; }