cream_core/schema.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249
use serde::{Deserialize, Serialize};
use crate::{declare_resource_type, declare_schema, meta::Meta, reference::Reference};
declare_schema!(SchemaSchema = "urn:ietf:params:scim:schemas:core:2.0:Schema");
declare_resource_type!(SchemaResourceType = "Schema");
/// A SCIM schema
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Schema {
/// ["urn:ietf:params:scim:schemas:core:2.0:Schema"]
#[serde(skip_deserializing)]
pub schemas: [SchemaSchema; 1],
/// The unique identifier for the schema.
pub id: String,
/// The name of the schema.
pub name: String,
/// A human-readable description of the schema.
pub description: String,
/// A list of attributes that form the schema.
pub attributes: Vec<Attribute>,
/// Metadata about the schema.
#[serde(skip_deserializing)]
pub meta: Meta<SchemaResourceType>,
}
impl Schema {
/// Adds the location metadata to this schema.
pub fn locate(&mut self) {
self.meta.location = Some(Reference::new_relative(&format!("/Schemas/{}", self.id)));
}
}
/// A single attribute of a SCIM schema.
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct Attribute {
/// The name of the attribute.
pub name: String,
/// The data type of the attribute.
#[serde(rename = "type")]
pub type_: Type,
/// Whether the attribute is multi-valued.
#[serde(default)]
pub multi_valued: bool,
/// A human-readable description of the attribute.
#[serde(default)]
pub description: String,
/// Whether the attribute is required.
#[serde(default)]
pub required: bool,
/// A list of canonical values for the attribute.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub canonical_values: Option<Vec<String>>,
/// Whether the attribute is case-sensitive.
#[serde(default)]
pub case_exact: bool,
/// The mutability of the attribute.
#[serde(default)]
pub mutability: Mutability,
/// When the attribute is returned.
#[serde(default)]
pub returned: Returned,
/// The uniqueness of the attribute.
#[serde(default)]
pub uniqueness: Uniqueness,
/// If this attribute is a reference, the types of resources it references.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub reference_types: Option<Vec<String>>,
/// If this attribute is a complex type, the sub-attributes that form it.
#[serde(default, skip_serializing_if = "Option::is_none")]
pub sub_attributes: Option<Vec<Attribute>>,
}
impl Attribute {
/// Construct a new attribute.
pub fn new(name: String, type_: Type) -> Self {
Self {
name,
type_,
multi_valued: false,
description: "".into(),
required: false,
canonical_values: None,
case_exact: false,
mutability: Mutability::ReadWrite,
returned: Returned::Default,
uniqueness: Uniqueness::None,
reference_types: None,
sub_attributes: None,
}
}
/// Set whether the attribute is multi-valued.
pub fn multi_valued(mut self) -> Self {
self.multi_valued = true;
self
}
/// Set whether the attribute is required.
pub fn required(mut self) -> Self {
self.required = true;
self
}
/// Set whether the attribute is case-sensitive.
pub fn case_exact(mut self) -> Self {
self.case_exact = true;
self
}
/// Set the sub-attributes of this attribute.
pub fn sub_attributes(mut self, sub_attributes: Vec<Attribute>) -> Self {
self.sub_attributes = Some(sub_attributes);
self
}
/// Set this attribute as immutable
pub fn immutable(mut self) -> Self {
self.mutability = Mutability::Immutable;
self
}
/// Set this attribute as read-only
pub fn read_only(mut self) -> Self {
self.mutability = Mutability::ReadOnly;
self
}
/// Set this attribute as write-only
pub fn write_only(mut self) -> Self {
self.mutability = Mutability::WriteOnly;
self
}
/// Set this attribute as always returned
pub fn always_returned(mut self) -> Self {
self.returned = Returned::Always;
self
}
/// Set this attribute as never returned
pub fn never_returned(mut self) -> Self {
self.returned = Returned::Never;
self
}
/// Set this attribute as returned on request
pub fn returned_on_request(mut self) -> Self {
self.returned = Returned::Request;
self
}
/// Set this attribute as unique on this server
pub fn unique(mut self) -> Self {
self.uniqueness = Uniqueness::Server;
self
}
/// Set this attribute as globally unique
pub fn globally_unique(mut self) -> Self {
self.uniqueness = Uniqueness::Global;
self
}
/// Set the types of resources this attribute can reference.
pub fn reference_types(mut self, reference_types: Vec<String>) -> Self {
self.reference_types = Some(reference_types);
self
}
}
/// The mutability of an attribute.
#[allow(unused)]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub enum Mutability {
/// Clients can only read this attribute.
ReadOnly,
/// Clients can read and write this attribute (default).
ReadWrite,
/// Clients can set this value on creation, but not update it.
Immutable,
/// Clients can write this attribute, but not read it.
WriteOnly,
}
impl Default for Mutability {
fn default() -> Self {
Self::ReadWrite
}
}
/// When an attribute is returned.
#[allow(unused)]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub enum Returned {
/// The attribute is always returned.
Always,
/// The attribute is never returned.
Never,
/// The attribute is returned by default (default).
Default,
/// The attribute is returned on request.
Request,
}
impl Default for Returned {
fn default() -> Self {
Self::Default
}
}
/// The uniqueness of an attribute.
#[allow(unused)]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub enum Uniqueness {
/// The attribute is not unique (default).
None,
/// The attribute is unique on this server.
Server,
/// The attribute is globally unique.
Global,
}
impl Default for Uniqueness {
fn default() -> Self {
Self::None
}
}
/// The data type of an attribute.
#[allow(unused)]
#[derive(Serialize, Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub enum Type {
/// A string.
String,
/// A boolean.
Boolean,
/// A decimal number.
Decimal,
/// An integer.
Integer,
/// A date and time.
DateTime,
/// Binary data (encoded in base64).
Binary,
/// A reference to another resource or external URL.
Reference,
/// A complex type with sub-attributes.
Complex,
}
impl Default for Type {
fn default() -> Self {
Self::String
}
}