use std::collections::HashMap;
use rbx_dom_weak::types::Ref;
use serde::Serialize;
use librojo::web_api::{Instance, InstanceUpdate, ReadResponse, SubscribeResponse};
use rojo_insta_ext::RedactionMap;
pub trait InternAndRedact<T> {
fn intern_and_redact(&self, redactions: &mut RedactionMap, extra: T) -> serde_yaml::Value;
}
impl<I, T> InternAndRedact<T> for I
where
I: Serialize + Internable<T>,
{
fn intern_and_redact(&self, redactions: &mut RedactionMap, extra: T) -> serde_yaml::Value {
self.intern(redactions, extra);
redactions.redacted_yaml(self)
}
}
pub trait Internable<T> {
fn intern(&self, redactions: &mut RedactionMap, extra: T);
}
impl Internable<Ref> for ReadResponse<'_> {
fn intern(&self, redactions: &mut RedactionMap, root_id: Ref) {
redactions.intern(root_id);
let root_instance = self.instances.get(&root_id).unwrap();
for &child_id in root_instance.children.iter() {
self.intern(redactions, child_id);
}
}
}
impl<'a> Internable<&'a HashMap<Ref, Instance<'_>>> for Instance<'a> {
fn intern(&self, redactions: &mut RedactionMap, other_instances: &HashMap<Ref, Instance<'_>>) {
redactions.intern(self.id);
for child_id in self.children.iter() {
let child = &other_instances[child_id];
child.intern(redactions, other_instances);
}
}
}
impl Internable<()> for SubscribeResponse<'_> {
fn intern(&self, redactions: &mut RedactionMap, _extra: ()) {
for message in &self.messages {
intern_instance_updates(redactions, &message.updated);
intern_instance_additions(redactions, &message.added);
}
}
}
fn intern_instance_updates(redactions: &mut RedactionMap, updates: &[InstanceUpdate]) {
for update in updates {
redactions.intern(update.id);
}
}
fn intern_instance_additions(
redactions: &mut RedactionMap,
additions: &HashMap<Ref, Instance<'_>>,
) {
let mut added_roots = Vec::new();
for (id, added) in additions {
let parent_id = added.parent;
let parent_redacted = redactions.get_redacted_value(parent_id);
if let Some(parent_redacted) = parent_redacted {
added_roots.push((id, parent_redacted));
}
}
added_roots.sort_unstable_by(|a, b| a.1.cmp(&b.1));
for (root_id, _redacted_id) in added_roots {
additions[root_id].intern(redactions, additions);
}
}