reda_lib/io/read/
semantic_library.rs1use crate::error::{LibErrorContext, LibError};
2use crate::model::{LibAst, LibCell, LibGroup, LibLibrary};
3
4use super::FromLibGroup;
5
6impl LibLibrary {
7 pub fn from_ast(ast: LibAst) -> Result<Self, LibError> {
8 let group = LibGroup::from_ast(ast).context(|| "semantic root ast to group")?;
9 Self::from_group(group)
10 }
11}
12
13impl FromLibGroup for LibLibrary {
14 type Error = LibError;
15 fn from_group(group: LibGroup) -> Result<Self, LibError> {
16 let mut group = group;
17 let name = group.expect_name().context(|| "group name")?;
18 let (cell_groups, groups) = group.groups.into_iter().partition(|g| g.kind == "cell");
19
20 let mut cells = Vec::new();
22 for cell_group in cell_groups {
23 cells.push(LibCell::from_group(cell_group).context(|| "semantic cell")?);
24 }
25
26 Ok(Self {
27 name,
28 attrs: group.attrs,
29 groups,
30 cells,
31 })
32 }
33}
34
35#[cfg(test)]
36mod test {
37 use super::*;
38
39
40 #[test]
41 fn test_cell_from_group() {
42 let mut group = LibGroup::new("cell", vec!["AND2".to_string()]);
43 group.groups.push(LibGroup::new("gtype", vec!["gname".to_string()]));
44 group.groups.push(LibGroup::new("pin", vec!["a".to_string()]));
45 group.groups.push(LibGroup::new("pin", vec!["b".to_string()]));
46 let cell = LibCell::from_group(group).unwrap();
47 assert_eq!(cell.name, "AND2");
48 assert_eq!(cell.groups.len(), 1);
49 assert_eq!(cell.pins.len(), 2);
50 }
51
52 #[test]
53 fn test_library_into_group() {
54 let mut lib = LibLibrary::new("my_lib");
55 lib.groups.push(LibGroup::new("gtype", vec!["gname".to_string()]));
56 lib.cells.push(LibCell::new("AND2"));
57 lib.cells.push(LibCell::new("NAND2"));
58 }
63
64 #[test]
65 fn test_lib_from_group() {
66 let mut group = LibGroup::new("library", vec!["mylib".to_string()]);
67 group.groups.push(LibGroup::new("gtype", vec!["gname".to_string()]));
68 let mut cell = LibGroup::new("cell", vec!["AND2".to_string()]);
69 cell.groups.push(LibGroup::new("pin", vec!["a".to_string()]));
70 cell.groups.push(LibGroup::new("pin", vec!["b".to_string()]));
71 group.groups.push(cell);
72 let lib = LibLibrary::from_group(group).unwrap();
73 assert_eq!(lib.name, "mylib");
74 assert_eq!(lib.groups.len(), 1);
75 assert_eq!(lib.cells.len(), 1);
76 let converted_cell = lib.cell("AND2").unwrap();
77 assert_eq!(converted_cell.name, "AND2");
78 assert_eq!(converted_cell.groups.len(), 0);
79 assert_eq!(converted_cell.pins.len(), 2);
80 }
81}