Skip to main content

surrealdb_core/iam/entities/
roles.rs

1use std::str::FromStr;
2
3use revision::revisioned;
4use serde::{Deserialize, Serialize};
5
6use crate::iam::Error;
7
8// In the future, we will allow for custom roles. For now, provide predefined
9// roles.
10#[revisioned(revision = 1)]
11#[derive(Hash, Copy, Clone, Default, Debug, Eq, PartialEq, PartialOrd, Serialize, Deserialize)]
12#[cfg_attr(feature = "arbitrary", derive(arbitrary::Arbitrary))]
13#[non_exhaustive]
14pub enum Role {
15	#[default]
16	Viewer,
17	Editor,
18	Owner,
19}
20
21impl std::fmt::Display for Role {
22	fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
23		match self {
24			Self::Viewer => write!(f, "Viewer"),
25			Self::Editor => write!(f, "Editor"),
26			Self::Owner => write!(f, "Owner"),
27		}
28	}
29}
30
31impl FromStr for Role {
32	type Err = Error;
33	fn from_str(s: &str) -> Result<Self, Self::Err> {
34		match s.to_ascii_lowercase().as_str() {
35			"viewer" => Ok(Self::Viewer),
36			"editor" => Ok(Self::Editor),
37			"owner" => Ok(Self::Owner),
38			_ => Err(Error::InvalidRole(s.to_string())),
39		}
40	}
41}