scim-server 0.4.0

A comprehensive SCIM 2.0 server library for Rust with multi-tenant support and type-safe operations
Documentation
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
//! ResourceId value object for SCIM resource identifiers.
//!
//! This module provides a type-safe wrapper around resource IDs with built-in validation.
//! Resource IDs are fundamental identifiers in SCIM and must follow specific format rules.

use crate::error::{ValidationError, ValidationResult};
use crate::resource::value_objects::value_object_trait::{SchemaConstructible, ValueObject};
use crate::schema::types::{AttributeDefinition, AttributeType};
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde_json::Value;
use std::any::Any;
use std::fmt;

/// A validated SCIM resource identifier.
///
/// ResourceId represents a unique identifier for a SCIM resource. It enforces
/// validation rules at construction time, ensuring that only valid resource IDs
/// can exist in the system.
///
/// ## Validation Rules
///
/// - Must not be empty
/// - Must be a valid string
/// - Additional format rules may be added in the future
///
/// ## Examples
///
/// ```rust
/// use scim_server::resource::value_objects::ResourceId;
///
/// fn main() -> Result<(), Box<dyn std::error::Error>> {
///     // Valid resource ID
///     let id = ResourceId::new("2819c223-7f76-453a-919d-413861904646".to_string())?;
///     println!("Resource ID: {}", id.as_str());
///
///     // Invalid resource ID - returns ValidationError
///     let invalid = ResourceId::new("".to_string());
///     assert!(invalid.is_err());
///
///     Ok(())
/// }
/// ```
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ResourceId(String);

impl ResourceId {
    /// Create a new ResourceId with validation.
    ///
    /// This is the primary constructor that enforces all validation rules.
    /// Use this method when creating ResourceId instances from untrusted input.
    ///
    /// # Arguments
    ///
    /// * `value` - The string value to validate and wrap
    ///
    /// # Returns
    ///
    /// * `Ok(ResourceId)` - If the value is valid
    /// * `Err(ValidationError)` - If the value violates validation rules
    ///
    /// # Examples
    ///
    /// ```rust
    /// use scim_server::resource::value_objects::ResourceId;
    ///
    /// fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let id = ResourceId::new("user-123".to_string())?;
    ///     let empty_id = ResourceId::new("".to_string()); // Error
    ///     assert!(empty_id.is_err());
    ///     Ok(())
    /// }
    /// ```
    pub fn new(value: String) -> ValidationResult<Self> {
        Self::validate_format(&value)?;
        Ok(Self(value))
    }

    /// Get the string representation of the ResourceId.
    ///
    /// Returns a reference to the underlying string value. This is safe
    /// because the value is guaranteed to be valid by construction.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use scim_server::resource::value_objects::ResourceId;
    ///
    /// fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let id = ResourceId::new("test-id".to_string())?;
    ///     assert_eq!(id.as_str(), "test-id");
    ///     Ok(())
    /// }
    /// ```
    pub fn as_str(&self) -> &str {
        &self.0
    }

    /// Get the owned string value of the ResourceId.
    ///
    /// Consumes the ResourceId and returns the underlying string.
    /// Use this when you need to transfer ownership of the string value.
    ///
    /// # Examples
    ///
    /// ```rust
    /// use scim_server::resource::value_objects::ResourceId;
    ///
    /// fn main() -> Result<(), Box<dyn std::error::Error>> {
    ///     let id = ResourceId::new("test-id".to_string())?;
    ///     let owned_string = id.into_string();
    ///     assert_eq!(owned_string, "test-id");
    ///     Ok(())
    /// }
    /// ```
    pub fn into_string(self) -> String {
        self.0
    }

    /// Validate the format of a resource ID string.
    ///
    /// This function contains the core validation logic moved from SchemaRegistry.
    /// It enforces all the rules that define a valid resource ID.
    ///
    /// # Arguments
    ///
    /// * `value` - The string to validate
    ///
    /// # Returns
    ///
    /// * `Ok(())` - If the value is valid
    /// * `Err(ValidationError)` - If the value violates any rules
    fn validate_format(value: &str) -> ValidationResult<()> {
        // Check if id is empty
        if value.is_empty() {
            return Err(ValidationError::EmptyId);
        }

        // TODO: Add more sophisticated ID format validation if needed
        // For now, we accept any non-empty string as a valid ID
        // Future enhancements might include:
        // - UUID format validation
        // - Character set restrictions
        // - Length limits

        Ok(())
    }
}

impl fmt::Display for ResourceId {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        write!(f, "{}", self.0)
    }
}

impl Serialize for ResourceId {
    fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
    where
        S: Serializer,
    {
        self.0.serialize(serializer)
    }
}

impl<'de> Deserialize<'de> for ResourceId {
    fn deserialize<D>(deserializer: D) -> Result<Self, D::Error>
    where
        D: Deserializer<'de>,
    {
        let value = String::deserialize(deserializer)?;
        Self::new(value).map_err(serde::de::Error::custom)
    }
}

/// Convert from String to ResourceId with validation.
impl TryFrom<String> for ResourceId {
    type Error = ValidationError;

    fn try_from(value: String) -> ValidationResult<Self> {
        Self::new(value)
    }
}

/// Convert from &str to ResourceId with validation.
impl TryFrom<&str> for ResourceId {
    type Error = ValidationError;

    fn try_from(value: &str) -> ValidationResult<Self> {
        Self::new(value.to_string())
    }
}

impl ValueObject for ResourceId {
    fn attribute_type(&self) -> AttributeType {
        AttributeType::String
    }

    fn attribute_name(&self) -> &str {
        "id"
    }

    fn to_json(&self) -> ValidationResult<Value> {
        Ok(Value::String(self.0.clone()))
    }

    fn validate_against_schema(&self, definition: &AttributeDefinition) -> ValidationResult<()> {
        if definition.data_type != AttributeType::String {
            return Err(ValidationError::InvalidAttributeType {
                attribute: definition.name.clone(),
                expected: "string".to_string(),
                actual: format!("{:?}", definition.data_type),
            });
        }

        if definition.name != "id" {
            return Err(ValidationError::InvalidAttributeName {
                actual: definition.name.clone(),
                expected: "id".to_string(),
            });
        }

        Ok(())
    }

    fn as_json_value(&self) -> Value {
        Value::String(self.0.clone())
    }

    fn supports_definition(&self, definition: &AttributeDefinition) -> bool {
        definition.data_type == AttributeType::String && definition.name == "id"
    }

    fn clone_boxed(&self) -> Box<dyn ValueObject> {
        Box::new(self.clone())
    }

    fn as_any(&self) -> &dyn Any {
        self
    }
}

impl SchemaConstructible for ResourceId {
    fn from_schema_and_value(
        definition: &AttributeDefinition,
        value: &Value,
    ) -> ValidationResult<Self> {
        if definition.name != "id" || definition.data_type != AttributeType::String {
            return Err(ValidationError::UnsupportedAttributeType {
                attribute: definition.name.clone(),
                type_name: format!("{:?}", definition.data_type),
            });
        }

        if let Some(id_str) = value.as_str() {
            Self::new(id_str.to_string())
        } else {
            Err(ValidationError::InvalidAttributeType {
                attribute: definition.name.clone(),
                expected: "string".to_string(),
                actual: "non-string".to_string(),
            })
        }
    }

    fn can_construct_from(definition: &AttributeDefinition) -> bool {
        definition.name == "id" && definition.data_type == AttributeType::String
    }

    fn constructor_priority() -> u8 {
        100 // High priority for exact name match
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::schema::types::{Mutability, Uniqueness};
    use serde_json;

    #[test]
    fn test_valid_resource_id() {
        let id = ResourceId::new("2819c223-7f76-453a-919d-413861904646".to_string());
        assert!(id.is_ok());

        let id = id.unwrap();
        assert_eq!(id.as_str(), "2819c223-7f76-453a-919d-413861904646");
    }

    #[test]
    fn test_empty_resource_id() {
        let result = ResourceId::new("".to_string());
        assert!(result.is_err());

        match result.unwrap_err() {
            ValidationError::EmptyId => {} // Expected
            other => panic!("Expected EmptyId error, got: {:?}", other),
        }
    }

    #[test]
    fn test_simple_string_id() {
        let id = ResourceId::new("user-123".to_string());
        assert!(id.is_ok());

        let id = id.unwrap();
        assert_eq!(id.as_str(), "user-123");
    }

    #[test]
    fn test_into_string() {
        let id = ResourceId::new("test-id".to_string()).unwrap();
        let string_value = id.into_string();
        assert_eq!(string_value, "test-id");
    }

    #[test]
    fn test_display() {
        let id = ResourceId::new("display-test".to_string()).unwrap();
        assert_eq!(format!("{}", id), "display-test");
    }

    #[test]
    fn test_serialization() {
        let id = ResourceId::new("serialize-test".to_string()).unwrap();
        let json = serde_json::to_string(&id).unwrap();
        assert_eq!(json, "\"serialize-test\"");
    }

    #[test]
    fn test_deserialization_valid() {
        let json = "\"deserialize-test\"";
        let id: ResourceId = serde_json::from_str(json).unwrap();
        assert_eq!(id.as_str(), "deserialize-test");
    }

    #[test]
    fn test_deserialization_invalid() {
        let json = "\"\""; // Empty string
        let result: Result<ResourceId, _> = serde_json::from_str(json);
        assert!(result.is_err());
    }

    #[test]
    fn test_try_from_string() {
        let result = ResourceId::try_from("try-from-test".to_string());
        assert!(result.is_ok());
        assert_eq!(result.unwrap().as_str(), "try-from-test");

        let empty_result = ResourceId::try_from("".to_string());
        assert!(empty_result.is_err());
    }

    #[test]
    fn test_try_from_str() {
        let result = ResourceId::try_from("try-from-str-test");
        assert!(result.is_ok());
        assert_eq!(result.unwrap().as_str(), "try-from-str-test");

        let empty_result = ResourceId::try_from("");
        assert!(empty_result.is_err());
    }

    #[test]
    fn test_equality() {
        let id1 = ResourceId::new("same-id".to_string()).unwrap();
        let id2 = ResourceId::new("same-id".to_string()).unwrap();
        let id3 = ResourceId::new("different-id".to_string()).unwrap();

        assert_eq!(id1, id2);
        assert_ne!(id1, id3);
    }

    #[test]
    fn test_hash() {
        use std::collections::HashMap;

        let id1 = ResourceId::new("hash-test-1".to_string()).unwrap();
        let id2 = ResourceId::new("hash-test-2".to_string()).unwrap();

        let mut map = HashMap::new();
        map.insert(id1.clone(), "value1");
        map.insert(id2.clone(), "value2");

        assert_eq!(map.get(&id1), Some(&"value1"));
        assert_eq!(map.get(&id2), Some(&"value2"));
    }

    #[test]
    fn test_clone() {
        let id = ResourceId::new("clone-test".to_string()).unwrap();
        let cloned = id.clone();

        assert_eq!(id, cloned);
        assert_eq!(id.as_str(), cloned.as_str());
    }

    #[test]
    fn test_value_object_trait() {
        let id = ResourceId::new("test-id".to_string()).unwrap();

        assert_eq!(id.attribute_type(), AttributeType::String);
        assert_eq!(id.attribute_name(), "id");
        assert_eq!(id.as_json_value(), Value::String("test-id".to_string()));

        let json_result = id.to_json().unwrap();
        assert_eq!(json_result, Value::String("test-id".to_string()));
    }

    #[test]
    fn test_schema_constructible_trait() {
        let definition = AttributeDefinition {
            name: "id".to_string(),
            data_type: AttributeType::String,
            multi_valued: false,
            required: true,
            case_exact: true,
            mutability: Mutability::ReadOnly,
            uniqueness: Uniqueness::Server,
            canonical_values: vec![],
            sub_attributes: vec![],
            returned: None,
        };

        let value = Value::String("test-id".to_string());
        let result = ResourceId::from_schema_and_value(&definition, &value);
        assert!(result.is_ok());
        assert_eq!(result.unwrap().as_str(), "test-id");

        // Test can_construct_from
        assert!(ResourceId::can_construct_from(&definition));

        // Test with wrong attribute name
        let mut wrong_def = definition.clone();
        wrong_def.name = "userName".to_string();
        assert!(!ResourceId::can_construct_from(&wrong_def));
    }

    #[test]
    fn test_validate_against_schema() {
        let id = ResourceId::new("test-id".to_string()).unwrap();

        let valid_definition = AttributeDefinition {
            name: "id".to_string(),
            data_type: AttributeType::String,
            multi_valued: false,
            required: true,
            case_exact: true,
            mutability: Mutability::ReadOnly,
            uniqueness: Uniqueness::Server,
            canonical_values: vec![],
            sub_attributes: vec![],
            returned: None,
        };

        assert!(id.validate_against_schema(&valid_definition).is_ok());

        // Test with wrong type
        let mut invalid_def = valid_definition.clone();
        invalid_def.data_type = AttributeType::Integer;
        assert!(id.validate_against_schema(&invalid_def).is_err());

        // Test with wrong name
        invalid_def.name = "userName".to_string();
        invalid_def.data_type = AttributeType::String;
        assert!(id.validate_against_schema(&invalid_def).is_err());
    }
}