reda-lib 0.1.0

Liberty file library
Documentation
use std::{collections::HashMap, str::FromStr};
use crate::ast::Ast;
use crate::error::LibError;
use super::{Cell, Group, Library, Pin, PinDirection};

pub trait FromGroup: Sized {
    type Error: Sized;
    fn from_group(group: Group) -> Result<Self, Self::Error>;
}

pub trait IntoGroup: Sized {
    type Error: Sized;
    fn into_group(self) -> Result<Group, Self::Error>;
}

impl Library {
    pub fn from_ast(ast: Ast) -> Result<Self, LibError> {
        let group = Group::from_ast(ast)?;
        Self::from_group(group)
    }
}

impl FromGroup for Library {
    type Error = LibError;
    fn  from_group(group: Group) -> Result<Self, LibError> {
        let mut group = group;
        let name = group.expect_name()?;
        let (cell_groups, groups) = group.groups.into_iter().partition(|g| g.kind == "cell");
        
        // collect cells
        let mut cells = Vec::new();
        for cell_group in cell_groups {
            cells.push(Cell::from_group(cell_group)?);
        }

        Ok(Self {
            name,
            attrs: group.attrs,
            groups,
            cells,
        })
    }
}

impl FromGroup for Cell {
    type Error = LibError;
    fn from_group(group: Group) -> Result<Self, LibError> {
        let mut group = group;
        let name = group.expect_name()?;
        let (pin_groups, groups) = group.groups.into_iter().partition(|g| g.kind == "pin");

        // collect cells
        let mut pins = Vec::new();
        for cell_group in pin_groups {
            pins.push(Pin::from_group(cell_group)?);
        }

        Ok(Self {
            name,
            pins,
            attrs: group.attrs,
            groups,
        })
    }
}

impl FromGroup for Pin {
    type Error = LibError;
    fn from_group(group: Group) -> Result<Self, LibError> {
        let mut group = group;
        let name = group.expect_name()?;

        let direction = match group.expect_ident_attr_opt("direction")? {
            Some(s) => PinDirection::from_str(&s)?,
            None => PinDirection::default(),
        };
        let function = group.expect_string_attr_opt("function")?;
    
        Ok(Self {
            name,
            direction,
            function,
            attrs: group.attrs,
            groups: group.groups,
        })
    }
}

impl FromStr for PinDirection {
    type Err = LibError;
    fn from_str(s: &str) -> Result<Self, Self::Err> {
        match s {
            "input" => Ok(Self::Input),
            "output" => Ok(Self::Output),
            "inout" => Ok(Self::Inout),
            "internal" => Ok(Self::Internal),
            s => Err(LibError::UnsupportDirection(s.to_string()))
        }   
    }
}

impl Group {
    pub fn from_ast(ast: Ast) -> Result<Self, LibError> {
        match ast {
            Ast::Group { kind, params, items } => {
                let mut attrs = HashMap::new();
                let mut groups = Vec::new();
                
                for item_ast in items {
                    if let Ast::Attr { key, value } = item_ast {
                        attrs.insert(key, value);
                    } else {
                        groups.push(Group::from_ast(item_ast)?);
                    }
                }

                Ok(Self {
                    kind, 
                    params,
                    attrs,
                    groups
                })
            }
            Ast::Attr { key:_, value:_ } => Err(LibError::ExceptGroup),
        }
    }
}

#[cfg(test)]
mod test {
    use super::*;

    #[test]
    fn test_pin_into_group() {
        let mut pin = Pin::new("my_pin");
        pin.groups.push(Group::new("gtype", vec!["gname".to_string()]));
        // let group = pin.into_group();
        // assert_eq!(group.type_, "pin");
        // assert_eq!(group.name, "my_pin");
        // assert_eq!(group.groups.len(), 1);
    }

    #[test]
    fn test_pin_from_group() {
        let mut group = Group::new("pin", vec!["a".to_string()]);
        group.groups.push(Group::new("gtype", vec!["gname".to_string()]));
        let pin = Pin::from_group(group).unwrap();
        assert_eq!(pin.name, "a");
        assert_eq!(pin.groups.len(), 1);
    }

    #[test]
    fn test_cell_into_group() {
        let mut cell = Cell::new("my_cell");
        cell.groups.push(Group::new("gtype", vec!["gname".to_string()]));
        cell.pins.push(Pin::new("a"));
        cell.pins.push(Pin::new("b"));
        // let group = cell.into_group();
        // assert_eq!(group.type_, "cell");
        // assert_eq!(group.name, "my_cell");
        // assert_eq!(group.groups.len(), 3);
    }

    #[test]
    fn test_cell_from_group() {
        let mut group = Group::new("cell", vec!["AND2".to_string()]);
        group.groups.push(Group::new("gtype", vec!["gname".to_string()]));
        group.groups.push(Group::new("pin", vec!["a".to_string()]));
        group.groups.push(Group::new("pin", vec!["b".to_string()]));
        let cell = Cell::from_group(group).unwrap();
        assert_eq!(cell.name, "AND2");
        assert_eq!(cell.groups.len(), 1);
        assert_eq!(cell.pins.len(), 2);
    }

    #[test]
    fn test_library_into_group() {
        let mut lib = Library::new("my_lib");
        lib.groups.push(Group::new("gtype", vec!["gname".to_string()]));
        lib.cells.push(Cell::new("AND2"));
        lib.cells.push(Cell::new("NAND2"));
        // let group = lib.into_group();
        // assert_eq!(group.type_, "library");
        // assert_eq!(group.name, "my_lib");
        // assert_eq!(group.groups.len(), 3);
    }

    #[test]
    fn test_lib_from_group() {
        let mut group = Group::new("library", vec!["mylib".to_string()]);
        group.groups.push(Group::new("gtype", vec!["gname".to_string()]));
        let mut cell = Group::new("cell", vec!["AND2".to_string()]);
        cell.groups.push(Group::new("pin", vec!["a".to_string()]));
        cell.groups.push(Group::new("pin", vec!["b".to_string()]));
        group.groups.push(cell);
        let lib = Library::from_group(group).unwrap();
        assert_eq!(lib.name, "mylib");
        assert_eq!(lib.groups.len(), 1);
        assert_eq!(lib.cells.len(), 1);
        let converted_cell = lib.cell("AND2").unwrap();
        assert_eq!(converted_cell.name, "AND2");
        assert_eq!(converted_cell.groups.len(), 0);
        assert_eq!(converted_cell.pins.len(), 2);
    }
}