use super::core::ScimServer;
use crate::error::{ScimError, ScimResult};
use crate::resource::{ResourceHandler, ResourceProvider, ScimOperation};
use crate::schema::Schema;
use std::sync::Arc;
impl<P: ResourceProvider> ScimServer<P> {
pub fn register_resource_type(
&mut self,
resource_type: &str,
handler: ResourceHandler,
operations: Vec<ScimOperation>,
) -> Result<(), ScimError> {
self.schema_registry
.add_schema(handler.schema.clone())
.map_err(|e| ScimError::internal(format!("Failed to add schema: {}", e)))?;
self.resource_handlers
.insert(resource_type.to_string(), Arc::new(handler));
self.supported_operations
.insert(resource_type.to_string(), operations);
Ok(())
}
pub fn get_supported_resource_types(&self) -> Vec<&str> {
self.resource_handlers.keys().map(|s| s.as_str()).collect()
}
pub fn get_supported_operations(&self, resource_type: &str) -> Option<&Vec<ScimOperation>> {
self.supported_operations.get(resource_type)
}
pub(super) fn ensure_operation_supported(
&self,
resource_type: &str,
operation: &ScimOperation,
) -> ScimResult<()> {
let operations = self
.supported_operations
.get(resource_type)
.ok_or_else(|| ScimError::UnsupportedResourceType(resource_type.to_string()))?;
if !operations.contains(operation) {
return Err(ScimError::UnsupportedOperation {
resource_type: resource_type.to_string(),
operation: format!("{:?}", operation),
});
}
Ok(())
}
pub(super) fn get_handler(&self, resource_type: &str) -> ScimResult<Arc<ResourceHandler>> {
self.resource_handlers
.get(resource_type)
.cloned()
.ok_or_else(|| ScimError::UnsupportedResourceType(resource_type.to_string()))
}
pub(super) fn get_schema_for_resource_type(&self, resource_type: &str) -> ScimResult<Schema> {
let handler = self.get_handler(resource_type)?;
Ok(handler.schema.clone())
}
}