65 lines
1.5 KiB
Rust
65 lines
1.5 KiB
Rust
use super::contextualize::Contextualize;
|
|
use super::{
|
|
id::{IdError, validate_id},
|
|
user::UserId,
|
|
};
|
|
|
|
#[derive(Clone, Debug, Eq, Hash, PartialEq)]
|
|
pub struct OrganizationId(String);
|
|
|
|
impl OrganizationId {
|
|
pub const MAX_BYTES: usize = 128;
|
|
|
|
pub fn new(value: impl Into<String>) -> Result<Self, IdError> {
|
|
let value = value.into();
|
|
validate_id(&value, Self::MAX_BYTES, IdError::Organization)?;
|
|
Ok(Self(value))
|
|
}
|
|
|
|
pub fn as_str(&self) -> &str {
|
|
&self.0
|
|
}
|
|
}
|
|
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct Organization {
|
|
pub id: OrganizationId,
|
|
pub name: String,
|
|
}
|
|
|
|
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
|
|
pub enum OrganizationRole {
|
|
Owner,
|
|
Admin,
|
|
Member,
|
|
}
|
|
|
|
/// Links a user to an organization with a role in that organization.
|
|
#[derive(Clone, Debug, Eq, PartialEq)]
|
|
pub struct OrganizationMembership {
|
|
pub organization_id: OrganizationId,
|
|
pub user_id: UserId,
|
|
pub role: OrganizationRole,
|
|
}
|
|
|
|
impl Contextualize for Organization {
|
|
fn render_context(&self) -> String {
|
|
format!("Organization: {}", self.name)
|
|
}
|
|
}
|
|
|
|
impl Contextualize for OrganizationMembership {
|
|
fn render_context(&self) -> String {
|
|
let role = match self.role {
|
|
OrganizationRole::Owner => "owner",
|
|
OrganizationRole::Admin => "admin",
|
|
OrganizationRole::Member => "member",
|
|
};
|
|
format!(
|
|
"User {} is a {role} of organization {}",
|
|
self.user_id.as_str(),
|
|
self.organization_id.as_str()
|
|
)
|
|
}
|
|
}
|