use std::collections::BTreeMap;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use crate::model::entities::{
AuthFlow, Client, ClientScope, Credential, Group, IdentityProvider, ProtocolMapper, Realm,
Role, User,
};
use crate::model::kind::EntityKind;
use crate::version::Version;
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct BackendInfo {
#[serde(default)]
pub backend: String,
pub detected_version: Version,
#[serde(default)]
pub extra: BTreeMap<String, Value>,
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct ExtractScope {
pub realm: String,
}
impl ExtractScope {
pub fn new(realm: impl Into<String>) -> Self {
Self {
realm: realm.into(),
}
}
}
#[derive(Debug, Clone, PartialEq, Eq, Default, Serialize, Deserialize)]
pub struct VolatileSection(
pub BTreeMap<EntityKind, BTreeMap<String, BTreeMap<String, Value>>>,
);
impl VolatileSection {
pub fn new() -> Self {
Self::default()
}
pub fn is_empty(&self) -> bool {
self.0.is_empty()
}
pub fn insert(
&mut self,
entity: EntityKind,
row_id: impl Into<String>,
path: impl Into<String>,
value: Value,
) {
self.0
.entry(entity)
.or_default()
.entry(row_id.into())
.or_default()
.insert(path.into(), value);
}
pub fn get(&self, entity: EntityKind, row_id: &str, path: &str) -> Option<&Value> {
self.0
.get(&entity)
.and_then(|by_id| by_id.get(row_id))
.and_then(|by_path| by_path.get(path))
}
pub fn row_ids<'a>(&'a self, entity: &'a EntityKind) -> impl Iterator<Item = &'a String> {
self.0.get(entity).into_iter().flat_map(|m| m.keys())
}
}
#[derive(Debug, Clone, PartialEq, Default, Serialize, Deserialize)]
pub struct CanonicalRealm {
#[serde(default)]
pub backend: BackendInfo,
pub realm: Realm,
#[serde(default)]
pub clients: Vec<Client>,
#[serde(default)]
pub client_scopes: Vec<ClientScope>,
#[serde(default)]
pub groups: Vec<Group>,
#[serde(default)]
pub roles: Vec<Role>,
#[serde(default)]
pub users: Vec<User>,
#[serde(default)]
pub credentials: Vec<Credential>,
#[serde(default)]
pub protocol_mappers: Vec<ProtocolMapper>,
#[serde(default)]
pub auth_flows: Vec<AuthFlow>,
#[serde(default)]
pub identity_providers: Vec<IdentityProvider>,
#[serde(default)]
pub volatile: VolatileSection,
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn envelope_has_default_empty_state() {
let realm = CanonicalRealm::default();
assert!(realm.clients.is_empty());
assert!(realm.volatile.is_empty());
}
#[test]
fn volatile_section_roundtrips_and_is_ordered() {
let mut section = VolatileSection::new();
section.insert(
EntityKind::User,
"u1",
"native.last_login",
serde_json::json!("2026-01-01"),
);
section.insert(
EntityKind::User,
"u2",
"native.last_login",
serde_json::json!("2026-01-02"),
);
assert_eq!(
section
.get(EntityKind::User, "u1", "native.last_login")
.unwrap(),
&serde_json::json!("2026-01-01")
);
assert_eq!(
section.row_ids(&EntityKind::User).collect::<Vec<_>>(),
vec![&"u1".to_string(), &"u2".to_string()]
);
let json = serde_json::to_value(§ion).unwrap();
assert!(json.is_object());
let back: VolatileSection = serde_json::from_value(json).unwrap();
assert_eq!(back, section);
}
#[test]
fn backend_info_defaults_to_unknown_backend() {
let info = BackendInfo::default();
assert_eq!(info.backend, "");
assert_eq!(info.detected_version, Version::default());
}
}