use rust_decimal::Decimal;
use scir::ParamValue;
use serde::{Deserialize, Serialize};
use std::collections::HashMap;
use std::hash::{Hash, Hasher};
use substrate::schematic::ExportsNestedData;
use crate::arcstr;
use crate::arcstr::ArcStr;
use crate::block::Block;
use crate::io::{Array, InOut, Signal, TwoTerminalIo};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct RawInstance {
pub cell: ArcStr,
pub ports: Vec<ArcStr>,
pub params: HashMap<ArcStr, ParamValue>,
}
impl Hash for RawInstance {
fn hash<H: Hasher>(&self, state: &mut H) {
self.cell.hash(state);
self.ports.hash(state);
self.params.iter().collect::<Vec<_>>().hash(state);
}
}
impl RawInstance {
#[inline]
pub fn with_params(
cell: ArcStr,
ports: Vec<ArcStr>,
params: impl Into<HashMap<ArcStr, ParamValue>>,
) -> Self {
Self {
cell,
ports,
params: params.into(),
}
}
#[inline]
pub fn new(cell: ArcStr, ports: Vec<ArcStr>) -> Self {
Self {
cell,
ports,
params: HashMap::new(),
}
}
}
impl Block for RawInstance {
type Io = InOut<Array<Signal>>;
fn id() -> ArcStr {
arcstr::literal!("raw_instance")
}
fn name(&self) -> ArcStr {
arcstr::format!("raw_instance_{}", self.cell)
}
fn io(&self) -> Self::Io {
InOut(Array::new(self.ports.len(), Default::default()))
}
}
impl ExportsNestedData for RawInstance {
type NestedData = ();
}
#[derive(Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct Resistor {
value: Decimal,
}
impl Resistor {
#[inline]
pub fn new(value: impl Into<Decimal>) -> Self {
Self {
value: value.into(),
}
}
#[inline]
pub fn value(&self) -> Decimal {
self.value
}
}
impl Block for Resistor {
type Io = TwoTerminalIo;
fn id() -> ArcStr {
arcstr::literal!("ideal_resistor")
}
fn name(&self) -> ArcStr {
arcstr::format!("ideal_resistor_{}", self.value)
}
fn io(&self) -> Self::Io {
Default::default()
}
}
impl ExportsNestedData for Resistor {
type NestedData = ();
}
#[derive(Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct Capacitor {
value: Decimal,
}
impl Capacitor {
#[inline]
pub fn new(value: impl Into<Decimal>) -> Self {
Self {
value: value.into(),
}
}
#[inline]
pub fn value(&self) -> Decimal {
self.value
}
}
impl Block for Capacitor {
type Io = TwoTerminalIo;
fn id() -> ArcStr {
arcstr::literal!("capacitor")
}
fn name(&self) -> ArcStr {
arcstr::format!("capacitor_{}", self.value)
}
fn io(&self) -> Self::Io {
Default::default()
}
}
impl ExportsNestedData for Capacitor {
type NestedData = ();
}
#[derive(Clone, Copy, Hash, PartialEq, Eq, Serialize, Deserialize)]
pub struct DcVsource {
value: Decimal,
}
impl DcVsource {
#[inline]
pub fn new(value: impl Into<Decimal>) -> Self {
Self {
value: value.into(),
}
}
#[inline]
pub fn value(&self) -> Decimal {
self.value
}
}
impl Block for DcVsource {
type Io = TwoTerminalIo;
fn id() -> ArcStr {
arcstr::literal!("vsource")
}
fn name(&self) -> ArcStr {
arcstr::format!("vsource_{}", self.value)
}
fn io(&self) -> Self::Io {
Default::default()
}
}
impl ExportsNestedData for DcVsource {
type NestedData = ();
}