1use std::collections::HashMap;
7use std::sync::{Arc, RwLock};
8
9#[cfg(test)]
10use crate::component::LifecyclePhase;
11use crate::component::{ComponentId, ComponentInstance, Context, LifecycleManager, Node};
12
13pub type TreeResult<T> = Result<T, TreeError>;
15
16#[derive(Debug, thiserror::Error)]
18pub enum TreeError {
19 #[error("Component not found: {0}")]
21 ComponentNotFound(ComponentId),
22
23 #[error("Component already exists: {0}")]
25 ComponentAlreadyExists(ComponentId),
26
27 #[error("Failed to access component: {0}")]
29 LockError(String),
30
31 #[error("Component lifecycle error: {0}")]
33 LifecycleError(#[from] crate::component::ComponentError),
34
35 #[error("Invalid parent-child relationship: {0}")]
37 InvalidRelationship(String),
38}
39
40pub type SharedComponentInstance = Arc<RwLock<ComponentInstance>>;
42
43pub struct ComponentTree {
47 components: RwLock<HashMap<ComponentId, SharedComponentInstance>>,
49
50 lifecycle_managers: RwLock<HashMap<ComponentId, Arc<RwLock<LifecycleManager>>>>,
52
53 children: RwLock<HashMap<ComponentId, Vec<ComponentId>>>,
55
56 parents: RwLock<HashMap<ComponentId, ComponentId>>,
58
59 root: RwLock<Option<ComponentId>>,
61
62 context: Context,
64}
65
66impl ComponentTree {
67 pub fn new(context: Context) -> Self {
69 Self {
70 components: RwLock::new(HashMap::new()),
71 lifecycle_managers: RwLock::new(HashMap::new()),
72 children: RwLock::new(HashMap::new()),
73 parents: RwLock::new(HashMap::new()),
74 root: RwLock::new(None),
75 context,
76 }
77 }
78
79 pub fn set_root(&self, component_id: ComponentId) -> TreeResult<()> {
81 if !self.has_component(component_id) {
83 return Err(TreeError::ComponentNotFound(component_id));
84 }
85
86 let mut root = self
88 .root
89 .write()
90 .map_err(|_| TreeError::LockError("Failed to lock root component".to_string()))?;
91
92 *root = Some(component_id);
93
94 Ok(())
95 }
96
97 pub fn root_id(&self) -> TreeResult<Option<ComponentId>> {
99 let root = self
100 .root
101 .read()
102 .map_err(|_| TreeError::LockError("Failed to read root component".to_string()))?;
103
104 Ok(*root)
105 }
106
107 pub fn add_component(&self, component: ComponentInstance) -> TreeResult<ComponentId> {
109 let id = component.id();
110
111 if self.has_component(id) {
113 return Err(TreeError::ComponentAlreadyExists(id));
114 }
115
116 let lifecycle = LifecycleManager::new(component.clone(), self.context.clone());
118
119 {
121 let mut components = self
122 .components
123 .write()
124 .map_err(|_| TreeError::LockError("Failed to lock components map".to_string()))?;
125
126 components.insert(id, Arc::new(RwLock::new(component)));
127 }
128
129 {
130 let mut lifecycle_managers = self.lifecycle_managers.write().map_err(|_| {
131 TreeError::LockError("Failed to lock lifecycle managers map".to_string())
132 })?;
133
134 lifecycle_managers.insert(id, Arc::new(RwLock::new(lifecycle)));
135 }
136
137 {
138 let mut children = self
139 .children
140 .write()
141 .map_err(|_| TreeError::LockError("Failed to lock children map".to_string()))?;
142
143 children.insert(id, Vec::new());
144 }
145
146 Ok(id)
147 }
148
149 pub fn remove_component(&self, id: ComponentId) -> TreeResult<()> {
151 if !self.has_component(id) {
153 return Err(TreeError::ComponentNotFound(id));
154 }
155
156 let children: Vec<ComponentId> = {
158 let children_map = self
159 .children
160 .read()
161 .map_err(|_| TreeError::LockError("Failed to read children map".to_string()))?;
162
163 children_map.get(&id).cloned().unwrap_or_default()
164 };
165
166 for child_id in children {
168 self.remove_component(child_id)?;
169 }
170
171 {
173 let parent_id_option = {
174 let parents = self
175 .parents
176 .read()
177 .map_err(|_| TreeError::LockError("Failed to read parents map".to_string()))?;
178
179 parents.get(&id).cloned()
180 };
181
182 if let Some(parent_id) = parent_id_option {
183 let mut children_map = self.children.write().map_err(|_| {
184 TreeError::LockError("Failed to write children map".to_string())
185 })?;
186
187 if let Some(siblings) = children_map.get_mut(&parent_id) {
188 if let Some(index) = siblings.iter().position(|&c| c == id) {
189 siblings.remove(index);
190 }
191 }
192 }
193 }
194
195 {
197 let mut components = self
198 .components
199 .write()
200 .map_err(|_| TreeError::LockError("Failed to lock components map".to_string()))?;
201
202 components.remove(&id);
203 }
204
205 {
206 let mut lifecycle_managers = self.lifecycle_managers.write().map_err(|_| {
207 TreeError::LockError("Failed to lock lifecycle managers map".to_string())
208 })?;
209
210 lifecycle_managers.remove(&id);
211 }
212
213 {
214 let mut children = self
215 .children
216 .write()
217 .map_err(|_| TreeError::LockError("Failed to lock children map".to_string()))?;
218
219 children.remove(&id);
220 }
221
222 {
223 let mut parents = self
224 .parents
225 .write()
226 .map_err(|_| TreeError::LockError("Failed to lock parents map".to_string()))?;
227
228 parents.remove(&id);
229 }
230
231 {
233 let mut root = self
234 .root
235 .write()
236 .map_err(|_| TreeError::LockError("Failed to lock root component".to_string()))?;
237
238 if let Some(root_id) = *root {
239 if root_id == id {
240 *root = None;
241 }
242 }
243 }
244
245 Ok(())
246 }
247
248 pub fn add_child(&self, parent_id: ComponentId, child_id: ComponentId) -> TreeResult<()> {
250 if !self.has_component(parent_id) {
252 return Err(TreeError::ComponentNotFound(parent_id));
253 }
254
255 if !self.has_component(child_id) {
256 return Err(TreeError::ComponentNotFound(child_id));
257 }
258
259 {
261 let parents = self
262 .parents
263 .read()
264 .map_err(|_| TreeError::LockError("Failed to read parents map".to_string()))?;
265
266 if let Some(existing_parent) = parents.get(&child_id) {
267 if *existing_parent != parent_id {
268 return Err(TreeError::InvalidRelationship(format!(
269 "Component {} already has a parent {}",
270 child_id.id(),
271 existing_parent.id()
272 )));
273 }
274
275 return Ok(());
277 }
278 }
279
280 {
282 let mut children_map = self
283 .children
284 .write()
285 .map_err(|_| TreeError::LockError("Failed to write children map".to_string()))?;
286
287 if let Some(children) = children_map.get_mut(&parent_id) {
288 if !children.contains(&child_id) {
289 children.push(child_id);
290 }
291 }
292 }
293
294 {
296 let mut parents = self
297 .parents
298 .write()
299 .map_err(|_| TreeError::LockError("Failed to write parents map".to_string()))?;
300
301 parents.insert(child_id, parent_id);
302 }
303
304 Ok(())
305 }
306
307 pub fn remove_child(&self, parent_id: ComponentId, child_id: ComponentId) -> TreeResult<()> {
309 if !self.has_component(parent_id) {
311 return Err(TreeError::ComponentNotFound(parent_id));
312 }
313
314 if !self.has_component(child_id) {
315 return Err(TreeError::ComponentNotFound(child_id));
316 }
317
318 {
320 let mut children_map = self
321 .children
322 .write()
323 .map_err(|_| TreeError::LockError("Failed to write children map".to_string()))?;
324
325 if let Some(children) = children_map.get_mut(&parent_id) {
326 if let Some(index) = children.iter().position(|&c| c == child_id) {
327 children.remove(index);
328 }
329 }
330 }
331
332 {
334 let mut parents = self
335 .parents
336 .write()
337 .map_err(|_| TreeError::LockError("Failed to write parents map".to_string()))?;
338
339 parents.remove(&child_id);
340 }
341
342 Ok(())
343 }
344
345 pub fn get_component(&self, id: ComponentId) -> TreeResult<SharedComponentInstance> {
347 let components = self
348 .components
349 .read()
350 .map_err(|_| TreeError::LockError("Failed to read components map".to_string()))?;
351
352 if let Some(component) = components.get(&id) {
353 Ok(component.clone())
354 } else {
355 Err(TreeError::ComponentNotFound(id))
356 }
357 }
358
359 pub fn get_lifecycle_manager(
361 &self,
362 id: ComponentId,
363 ) -> TreeResult<Arc<RwLock<LifecycleManager>>> {
364 let lifecycle_managers = self.lifecycle_managers.read().map_err(|_| {
365 TreeError::LockError("Failed to read lifecycle managers map".to_string())
366 })?;
367
368 if let Some(manager) = lifecycle_managers.get(&id) {
369 Ok(manager.clone())
370 } else {
371 Err(TreeError::ComponentNotFound(id))
372 }
373 }
374
375 pub fn initialize_component(&self, id: ComponentId) -> TreeResult<()> {
377 let lifecycle_manager = self.get_lifecycle_manager(id)?;
378 let mut manager = lifecycle_manager
379 .write()
380 .map_err(|_| TreeError::LockError("Failed to lock lifecycle manager".to_string()))?;
381
382 manager.initialize().map_err(TreeError::LifecycleError)?;
383
384 Ok(())
385 }
386
387 pub fn mount_component(&self, id: ComponentId) -> TreeResult<()> {
389 let lifecycle_manager = self.get_lifecycle_manager(id)?;
390 let mut manager = lifecycle_manager
391 .write()
392 .map_err(|_| TreeError::LockError("Failed to lock lifecycle manager".to_string()))?;
393
394 manager.mount().map_err(TreeError::LifecycleError)?;
395
396 Ok(())
397 }
398
399 pub fn unmount_component(&self, id: ComponentId) -> TreeResult<()> {
401 let lifecycle_manager = self.get_lifecycle_manager(id)?;
402 let mut manager = lifecycle_manager
403 .write()
404 .map_err(|_| TreeError::LockError("Failed to lock lifecycle manager".to_string()))?;
405
406 manager.unmount().map_err(TreeError::LifecycleError)?;
407
408 Ok(())
409 }
410
411 pub fn render_component(&self, id: ComponentId) -> TreeResult<Vec<Node>> {
413 let lifecycle_manager = self.get_lifecycle_manager(id)?;
414 let manager = lifecycle_manager
415 .read()
416 .map_err(|_| TreeError::LockError("Failed to read lifecycle manager".to_string()))?;
417
418 manager.render().map_err(TreeError::LifecycleError)
419 }
420
421 pub fn has_component(&self, id: ComponentId) -> bool {
423 let components = match self.components.read() {
424 Ok(c) => c,
425 Err(_) => return false,
426 };
427
428 components.contains_key(&id)
429 }
430
431 pub fn get_children(&self, id: ComponentId) -> TreeResult<Vec<ComponentId>> {
433 let children_map = self
434 .children
435 .read()
436 .map_err(|_| TreeError::LockError("Failed to read children map".to_string()))?;
437
438 if let Some(children) = children_map.get(&id) {
439 Ok(children.clone())
440 } else {
441 Ok(Vec::new())
443 }
444 }
445
446 pub fn get_parent(&self, id: ComponentId) -> TreeResult<Option<ComponentId>> {
448 let parents = self
449 .parents
450 .read()
451 .map_err(|_| TreeError::LockError("Failed to read parents map".to_string()))?;
452
453 Ok(parents.get(&id).cloned())
454 }
455
456 pub fn mount_component_tree(&self, id: ComponentId) -> TreeResult<()> {
458 self.mount_component(id)?;
460
461 let children = self.get_children(id)?;
463 for child_id in children {
464 self.mount_component_tree(child_id)?;
465 }
466
467 Ok(())
468 }
469
470 pub fn unmount_component_tree(&self, id: ComponentId) -> TreeResult<()> {
472 let children = self.get_children(id)?;
474 for child_id in children {
475 self.unmount_component_tree(child_id)?;
476 }
477
478 self.unmount_component(id)?;
480
481 Ok(())
482 }
483
484 pub fn update_component<P: crate::component::Props + 'static>(
486 &self,
487 id: ComponentId,
488 props: P,
489 ) -> TreeResult<()> {
490 let lifecycle_manager = self.get_lifecycle_manager(id)?;
491 let mut manager = lifecycle_manager
492 .write()
493 .map_err(|_| TreeError::LockError("Failed to lock lifecycle manager".to_string()))?;
494
495 let boxed_props = Box::new(props);
497
498 manager
499 .update(boxed_props)
500 .map_err(TreeError::LifecycleError)?;
501
502 Ok(())
503 }
504
505 pub fn update_component_tree<P: crate::component::Props + Clone + 'static>(
507 &self,
508 id: ComponentId,
509 props: P,
510 ) -> TreeResult<()> {
511 self.update_component(id, props.clone())?;
513
514 let _children = self.get_children(id)?;
516
517 let _nodes = self.render_component(id)?;
523
524 Ok(())
529 }
530
531 pub fn get_all_components(&self) -> TreeResult<Vec<ComponentId>> {
533 let components = self
534 .components
535 .read()
536 .map_err(|_| TreeError::LockError("Failed to read components map".to_string()))?;
537
538 Ok(components.keys().cloned().collect())
539 }
540
541 pub fn get_components_to_update(&self) -> TreeResult<Vec<ComponentId>> {
543 Ok(Vec::new())
547 }
548
549 pub fn detect_state_changes(&self, id: ComponentId) -> TreeResult<bool> {
551 let component = self.get_component(id)?;
552 let _component = component
553 .read()
554 .map_err(|_| TreeError::LockError("Failed to read component".to_string()))?;
555
556 Ok(false)
560 }
561
562 pub fn batch_update_components(&self, ids: &[ComponentId]) -> TreeResult<usize> {
564 for id in ids {
565 if !self.has_component(*id) {
566 return Err(TreeError::ComponentNotFound(*id));
567 }
568 }
569
570 let mut updated_count = 0;
573
574 for &id in ids {
575 let lifecycle_manager = self.get_lifecycle_manager(id)?;
577 let _manager = lifecycle_manager.read().map_err(|_| {
578 TreeError::LockError("Failed to lock lifecycle manager".to_string())
579 })?;
580
581 updated_count += 1;
584 }
585
586 Ok(updated_count)
587 }
588}
589
590impl std::fmt::Debug for ComponentTree {
592 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
593 f.debug_struct("ComponentTree")
594 .field(
595 "component_count",
596 &self.components.try_read().map(|c| c.len()).unwrap_or(0),
597 )
598 .field("root", &self.root.try_read().ok())
599 .finish()
600 }
601}
602
603#[cfg(test)]
604mod tests {
605 use super::*;
606 use crate::component::{Component, ComponentError, Context, Node};
607
608 struct TestComponent {
610 id: ComponentId,
611 #[allow(dead_code)]
612 context: Context,
613 name: String,
614 }
615
616 #[derive(Clone)]
618 struct TestProps {
619 name: String,
620 }
621
622 impl Component for TestComponent {
625 type Props = TestProps;
626
627 fn component_id(&self) -> ComponentId {
628 self.id
629 }
630
631 fn create(props: Self::Props, context: Context) -> Self {
632 Self {
633 id: ComponentId::new(),
634 context,
635 name: props.name,
636 }
637 }
638
639 fn update(&mut self, props: Self::Props) -> Result<(), ComponentError> {
640 self.name = props.name;
641 Ok(())
642 }
643
644 fn render(&self) -> Result<Vec<Node>, ComponentError> {
645 Ok(vec![])
646 }
647
648 fn as_any(&self) -> &dyn std::any::Any {
649 self
650 }
651
652 fn as_any_mut(&mut self) -> &mut dyn std::any::Any {
653 self
654 }
655 }
656
657 fn create_test_component(name: &str, context: Context) -> ComponentInstance {
658 let props = TestProps {
659 name: name.to_string(),
660 };
661
662 let component = TestComponent::create(props.clone(), context.clone());
663
664 ComponentInstance::new(component, props)
665 }
666
667 #[test]
668 fn test_component_tree_basic() {
669 let context = Context::new();
671 let tree = ComponentTree::new(context.clone());
672
673 let root_component = create_test_component("root", context.clone());
675 let root_id = tree.add_component(root_component).unwrap();
676
677 tree.set_root(root_id).unwrap();
679
680 assert_eq!(tree.root_id().unwrap(), Some(root_id));
682
683 let child_component = create_test_component("child", context.clone());
685 let child_id = tree.add_component(child_component).unwrap();
686
687 tree.add_child(root_id, child_id).unwrap();
689
690 let children = tree.get_children(root_id).unwrap();
692 assert_eq!(children.len(), 1);
693 assert_eq!(children[0], child_id);
694
695 let parent = tree.get_parent(child_id).unwrap();
696 assert_eq!(parent, Some(root_id));
697 }
698
699 #[test]
700 fn test_component_tree_lifecycle() {
701 let context = Context::new();
703 let tree = ComponentTree::new(context.clone());
704
705 let root_component = create_test_component("root", context.clone());
707 let root_id = tree.add_component(root_component).unwrap();
708 tree.set_root(root_id).unwrap();
709
710 let child1_component = create_test_component("child1", context.clone());
712 let child1_id = tree.add_component(child1_component).unwrap();
713
714 let child2_component = create_test_component("child2", context.clone());
715 let child2_id = tree.add_component(child2_component).unwrap();
716
717 tree.add_child(root_id, child1_id).unwrap();
719 tree.add_child(root_id, child2_id).unwrap();
720
721 tree.mount_component_tree(root_id).unwrap();
723
724 let root_lifecycle = tree.get_lifecycle_manager(root_id).unwrap();
726 let root_phase = {
727 let manager = root_lifecycle.read().unwrap();
728 manager.current_phase()
729 };
730 assert_eq!(root_phase, LifecyclePhase::Mounted);
731
732 let child1_lifecycle = tree.get_lifecycle_manager(child1_id).unwrap();
733 let child1_phase = {
734 let manager = child1_lifecycle.read().unwrap();
735 manager.current_phase()
736 };
737 assert_eq!(child1_phase, LifecyclePhase::Mounted);
738 }
739
740 #[test]
741 fn test_component_tree_removal() {
742 let context = Context::new();
744 let tree = ComponentTree::new(context.clone());
745
746 let root_component = create_test_component("root", context.clone());
748 let root_id = tree.add_component(root_component).unwrap();
749 tree.set_root(root_id).unwrap();
750
751 let child1_component = create_test_component("child1", context.clone());
753 let child1_id = tree.add_component(child1_component).unwrap();
754
755 let child2_component = create_test_component("child2", context.clone());
756 let child2_id = tree.add_component(child2_component).unwrap();
757
758 tree.add_child(root_id, child1_id).unwrap();
760 tree.add_child(root_id, child2_id).unwrap();
761
762 tree.remove_child(root_id, child1_id).unwrap();
764
765 let children = tree.get_children(root_id).unwrap();
767 assert_eq!(children.len(), 1);
768 assert_eq!(children[0], child2_id);
769
770 let parent = tree.get_parent(child1_id).unwrap();
772 assert_eq!(parent, None);
773
774 assert!(tree.has_component(child1_id));
776
777 tree.remove_component(child1_id).unwrap();
779
780 assert!(!tree.has_component(child1_id));
782
783 tree.remove_component(root_id).unwrap();
785
786 assert!(!tree.has_component(root_id));
788 assert!(!tree.has_component(child2_id));
789
790 assert_eq!(tree.root_id().unwrap(), None);
792 }
793
794 #[test]
795 fn test_dirty_component_updates() {
796 let context = Context::new();
798 let tree = ComponentTree::new(context.clone());
799
800 let root_component = create_test_component("root", context.clone());
802 let root_id = tree.add_component(root_component).unwrap();
803 tree.set_root(root_id).unwrap();
804
805 let child1_component = create_test_component("child1", context.clone());
806 let child1_id = tree.add_component(child1_component).unwrap();
807
808 let child2_component = create_test_component("child2", context.clone());
809 let child2_id = tree.add_component(child2_component).unwrap();
810
811 tree.add_child(root_id, child1_id).unwrap();
813 tree.add_child(root_id, child2_id).unwrap();
814
815 tree.mount_component_tree(root_id).unwrap();
817
818 let dirty_components = vec![root_id, child1_id];
821
822 let updated = tree.batch_update_components(&dirty_components).unwrap();
824
825 assert_eq!(updated, 2);
827 }
828}