libertyparse 0.3.0

Liberty cell library parser
Documentation
//! A direction provider implementation for the interaction
//! with the netlist database.
//!
//! This mod is only enabled with the `direction_provider`
//! feature.

use super::*;
use std::collections::HashMap;
use arcstr::Substr;

pub struct LibDirectionProvider<'i> {
    pub cell_pin: HashMap<(&'i str, &'i str), netlistdb::Direction>
}

impl Liberty {
    pub fn to_direction_provider(&self) -> LibDirectionProvider {
        // deduplicate same logic cell in different library.
        let cells = self.libs.iter()
            .flat_map(|(_, lib)|
                      lib.cells.iter()
                      .map(|(name, cell)| (name, cell)))
            .collect::<HashMap<_, _>>();
        // build the pin mapping
        use netlistdb::Direction::*;
        let cell_pin = cells.iter()
            .flat_map(|(cell_name, cell)| {
                cell.pins.iter().map(|(pin_name, pin)| {
                    ((cell_name.as_str(), pin_name.as_str()),
                     match pin.direction {
                         PinDirection::I => I,
                         PinDirection::O => O,
                         _ => Unknown
                     })
                })
            })
            .collect();
        LibDirectionProvider { cell_pin }
    }
}

impl<'i> netlistdb::DirectionProvider for LibDirectionProvider<'i> {
    #[inline]
    fn direction_of(
        &self,
        macro_name: &Substr, pin_name: &Substr,
        pin_idx: Option<isize>
    ) -> netlistdb::Direction {
        use netlistdb::Direction::*;
        if pin_idx.is_some() {
            return Unknown
        }
        match self.cell_pin.get(
            &(macro_name.as_str(), pin_name.as_str())
        ) {
            Some(v) => *v,
            None => Unknown
        }
    }
}