1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
//! Netlist export.

use crate::context::Context;
use crate::schematic::conv::RawLib;
use crate::schematic::Schematic;
use scir::{Library, NetlistLibConversion};
use std::io::Write;
use std::path::Path;
use std::sync::Arc;
use substrate::schematic::schema::Schema;

/// A netlister that tracks how cells and instances are translated between SCIR and the output netlist format.
pub trait ConvertibleNetlister<S: Schema + ?Sized> {
    /// The error type returned when writing out a SCIR netlist.
    type Error: Into<substrate::error::Error>;

    /// The netlist options type.
    ///
    /// Many netlisters accept options, allowing the user to configure things like
    /// netlist indentation, naming conventions, etc. This is the type of the object
    /// that stores those options.
    type Options<'a>;

    /// Writes a netlist of a SCIR library to the provided output stream.
    fn write_scir_netlist<W: Write>(
        &self,
        lib: &Library<S>,
        out: &mut W,
        opts: Self::Options<'_>,
    ) -> Result<NetlistLibConversion, Self::Error>;

    /// Writes a netlist of a SCIR library to a file at the given path.
    ///
    /// The file and any parent directories will be created if necessary.
    fn write_scir_netlist_to_file(
        &self,
        lib: &Library<S>,
        path: impl AsRef<Path>,
        opts: Self::Options<'_>,
    ) -> substrate::error::Result<NetlistLibConversion> {
        let path = path.as_ref();
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).map_err(Arc::new)?;
        }
        let mut f = std::fs::File::create(path).map_err(Arc::new)?;
        let conv = self
            .write_scir_netlist(lib, &mut f, opts)
            .map_err(|e| e.into())?;
        Ok(conv)
    }

    /// Writes a netlist of a Substrate block to the given output stream.
    fn write_netlist<B: Schematic<S>, W: Write>(
        &self,
        ctx: &Context,
        block: B,
        out: &mut W,
        opts: Self::Options<'_>,
    ) -> substrate::error::Result<(RawLib<S>, NetlistLibConversion)> {
        let raw_lib = ctx.export_scir::<S, _>(block)?;

        let conv = self
            .write_scir_netlist(&raw_lib.scir, out, opts)
            .map_err(|e| e.into())?;
        Ok((raw_lib, conv))
    }

    /// Writes a netlist of a Substrate block to a file at the given path.
    ///
    /// The file and any parent directories will be created if necessary.
    fn write_netlist_to_file<B: Schematic<S>>(
        &self,
        ctx: &Context,
        block: B,
        path: impl AsRef<Path>,
        opts: Self::Options<'_>,
    ) -> substrate::error::Result<(RawLib<S>, NetlistLibConversion)> {
        let path = path.as_ref();
        if let Some(parent) = path.parent() {
            std::fs::create_dir_all(parent).map_err(Arc::new)?;
        }
        let mut f = std::fs::File::create(path).map_err(Arc::new)?;
        self.write_netlist(ctx, block, &mut f, opts)
    }
}