Skip to main content

libtrace2power/
netlist.rs

1// Copyright (c) 2024-2026 Antmicro <www.antmicro.com>
2// SPDX-License-Identifier: Apache-2.0
3
4// Structs for deserializing yosys netlists
5
6use serde::Deserialize;
7use std::collections::HashMap;
8
9#[derive(Debug, Deserialize)]
10pub struct Module {
11    pub cells: HashMap<String, Cell>,
12}
13
14#[derive(Debug, Deserialize)]
15pub struct Cell {
16    #[serde(rename = "type")]
17    pub type_name: String,
18}
19
20#[derive(Deserialize)]
21pub struct Netlist {
22    pub modules: HashMap<String, Module>,
23}
24
25impl Cell {
26    pub fn get_module<'n>(&self, netlist: &'n Netlist) -> Option<&'n Module> {
27        netlist.modules.get(&self.type_name)
28    }
29}
30
31#[derive(Copy, Clone)]
32pub enum ModuleLookupError {
33    CellNotFound,
34    ModuleUndefined,
35}
36
37impl Module {
38    pub fn get_module_of_cell<'s>(
39        &self,
40        netlist: &'s Netlist,
41        cell_name: &str,
42    ) -> Result<&'s Self, ModuleLookupError> {
43        let cell = self
44            .cells
45            .get(cell_name)
46            .ok_or(ModuleLookupError::CellNotFound)?;
47        let module = cell
48            .get_module(netlist)
49            .ok_or(ModuleLookupError::ModuleUndefined)?;
50        Ok(module)
51    }
52}