libreda_db/netlist/
io.rs

1// Copyright (c) 2020-2021 Thomas Kramer.
2// SPDX-FileCopyrightText: 2022 Thomas Kramer
3//
4// SPDX-License-Identifier: AGPL-3.0-or-later
5
6//! Input and output interface definitions for netlists.
7//!
8//! Implementations for the various netlist formats are located in other crates.
9
10use crate::netlist::traits::{NetlistBase, NetlistEdit};
11use std::io::{Read, Write};
12
13/// Read a netlist from a byte stream.
14pub trait NetlistReader {
15    /// Type of error that could happen while reading a netlist.
16    type Error;
17
18    /// Read a netlist from a byte stream and populate the netlist data structure.
19    fn read_into_netlist<R: Read, N: NetlistEdit>(
20        &self,
21        reader: &mut R,
22        netlist: &mut N,
23    ) -> Result<(), Self::Error>;
24
25    /// Read a netlist from a byte stream.
26    fn read_netlist<R: Read, N: NetlistEdit + Default>(
27        &self,
28        reader: &mut R,
29    ) -> Result<N, Self::Error> {
30        let mut netlist = N::default();
31        self.read_into_netlist(reader, &mut netlist)?;
32        Ok(netlist)
33    }
34}
35
36/// Write a netlist to a byte stream.
37pub trait NetlistWriter {
38    /// Type of error that could happen while writing a netlist.
39    type Error;
40
41    /// Write the netlist data structure to a byte stream.
42    fn write_netlist<W: Write, N: NetlistBase>(
43        &self,
44        writer: &mut W,
45        netlist: &N,
46    ) -> Result<(), Self::Error>;
47}