use std::fmt;
use serde::{Deserialize, Serialize};
use uuid::Uuid;
const GTS_TYPE_PATH_MAX_LEN: usize = 1024;
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
#[serde(try_from = "String", into = "String")]
pub struct GtsTypePath(String);
impl GtsTypePath {
pub fn new(raw: impl Into<String>) -> Result<Self, String> {
let raw = raw.into();
let s = raw.trim().to_lowercase();
if s.is_empty() {
return Err("GTS type path must not be empty".to_owned());
}
if s.len() > GTS_TYPE_PATH_MAX_LEN {
return Err("GTS type path exceeds maximum length".to_owned());
}
if gts::GtsID::new(&s).is_err() {
return Err("Invalid GTS type path format".to_owned());
}
Ok(Self(s))
}
#[must_use]
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for GtsTypePath {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl From<GtsTypePath> for String {
fn from(p: GtsTypePath) -> Self {
p.0
}
}
impl TryFrom<String> for GtsTypePath {
type Error = String;
fn try_from(s: String) -> Result<Self, Self::Error> {
Self::new(s)
}
}
impl AsRef<str> for GtsTypePath {
fn as_ref(&self) -> &str {
&self.0
}
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ResourceGroupType {
pub code: String,
pub can_be_root: bool,
pub allowed_parent_types: Vec<String>,
pub allowed_membership_types: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata_schema: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateTypeRequest {
pub code: String,
pub can_be_root: bool,
#[serde(default)]
pub allowed_parent_types: Vec<String>,
#[serde(default)]
pub allowed_membership_types: Vec<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata_schema: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateTypeRequest {
pub can_be_root: bool,
pub allowed_parent_types: Vec<String>,
pub allowed_membership_types: Vec<String>,
pub metadata_schema: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GroupHierarchy {
#[serde(skip_serializing_if = "Option::is_none")]
pub parent_id: Option<Uuid>,
pub tenant_id: Uuid,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GroupHierarchyWithDepth {
#[serde(skip_serializing_if = "Option::is_none")]
pub parent_id: Option<Uuid>,
pub tenant_id: Uuid,
pub depth: i32,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ResourceGroup {
pub id: Uuid,
#[serde(rename = "type")]
pub code: String,
pub name: String,
pub hierarchy: GroupHierarchy,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ResourceGroupWithDepth {
pub id: Uuid,
#[serde(rename = "type")]
pub code: String,
pub name: String,
pub hierarchy: GroupHierarchyWithDepth,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct CreateGroupRequest {
#[serde(skip_serializing_if = "Option::is_none")]
pub id: Option<Uuid>,
#[serde(rename = "type")]
pub code: String,
pub name: String,
#[serde(skip_serializing_if = "Option::is_none")]
pub parent_id: Option<Uuid>,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct UpdateGroupRequest {
pub name: String,
pub parent_id: Option<Uuid>,
pub metadata: Option<serde_json::Value>,
}
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct ResourceGroupMembership {
pub group_id: Uuid,
pub resource_type: String,
pub resource_id: String,
}
#[cfg(test)]
#[path = "models_tests.rs"]
mod models_tests;