term_rustdoc/tree/nodes/
unions.rs

1use crate::tree::{
2    impls::show::{show_ids, DocTree, Show},
3    DImpl, IDMap, IDs, SliceToIds, ID,
4};
5use rustdoc_types::Union;
6
7#[derive(serde::Serialize, serde::Deserialize, Clone)]
8pub struct DUnion {
9    pub id: ID,
10    pub fields: IDs,
11    pub impls: DImpl,
12}
13impl DUnion {
14    pub fn new(id: ID, item: &Union, map: &IDMap) -> Self {
15        DUnion {
16            id,
17            fields: item.fields.to_ids(),
18            impls: DImpl::new(&item.impls, map),
19        }
20    }
21
22    /// External items need external crates compiled to know details,
23    /// and the ID here is for PathMap, not IndexMap.
24    pub fn new_external(id: ID) -> Self {
25        let (fields, impls) = Default::default();
26        DUnion { id, fields, impls }
27    }
28
29    pub fn fields_tree(&self, map: &IDMap) -> DocTree {
30        let mut root = node!(Union: map, &self.id);
31        names_node!(@iter self map root
32            fields Field
33        );
34        root
35    }
36}
37
38impl Show for DUnion {
39    fn show(&self) -> DocTree {
40        format!("[union] {}", self.id).show().with_leaves([
41            "Fields".show().with_leaves(show_ids(&self.fields)),
42            self.impls.show(),
43        ])
44    }
45
46    fn show_prettier(&self, map: &IDMap) -> DocTree {
47        let fields = names_node!(@single
48            self map NoFields,
49            Fields fields Field
50        );
51        node!(Union: map, &self.id).with_leaves([fields, self.impls.show_prettier(map)])
52    }
53}