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
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
//!
//! # Spice21
//!
//! SPICE for the 21st Century
//!
//! [https://github.com/HW21/Spice21](https://github.com/HW21/Spice21)
//!

///
/// # Spice21 Macros
///
/// Note: this module's unusual location, inline in lib.rs,
/// is the best way we've found to import it
/// to the rest of Spice21.
/// Note: this must be defined *before* any uses of it.
///
#[macro_use]
pub(crate) mod macros {
    /// GetAttr-enabled struct builder.
    /// Creates structs from a list of field-definitions,
    /// adding a `getattr` method enabling by-string access.
    #[macro_export]
    macro_rules! attr {
    ( $src_name:ident, $struct_desc:literal, [
        $( ($attr_name:ident, $attr_type:ty, $default:literal, $desc:literal) ),* $(,)?
    ]) => {
        #[doc=$struct_desc]
        #[derive(Clone, Copy)]
        pub struct $src_name {
            $( #[doc=$desc]
                pub $attr_name : $attr_type ),*
        }

        impl $src_name {
            fn getattr<S: Into<String>>(&self, key: S) -> Option<f64> {
                let k: String = key.into();
                match &k as &str {
                    $( stringify!($attr_name) => Some(self.$attr_name)),*,
                    _ => None,
                }
            }
        }

        impl Default for $src_name {
            fn default() -> Self {
                Self {
                    $($attr_name : $default),*,
                }
            }
        }
    }
}

    #[cfg(test)]
    mod tests {
        use crate::assert::*;
        use crate::spresult::TestResult;

        attr!(
            SampleModel,
            "Doc-string for Sample Model",
            [
                (param1, f64, 1.1, "First Param"),
                (param22, f64, 2.2, "Parameter #2"),
                (par33, f64, 3.3, "3param"),
            ]
        );

        #[test]
        fn test1() -> TestResult {
            // Instantiate a generated struct
            let s = SampleModel {
                param1: 1.1,
                param22: 22.22,
                par33: 333.333,
            };

            // Test fields
            assert(s.param1).eq(1.1)?;
            assert(s.param22).eq(22.22)?;
            assert(s.par33).eq(333.333)?;

            // Test getattr
            assert(s.getattr("param1")).eq(Some(s.param1))?;
            assert(s.getattr("param22")).eq(Some(s.param22))?;
            assert(s.getattr("par33")).eq(Some(s.par33))?;
            assert(s.getattr("fizzbuzz")).eq(None)?;

            Ok(())
        }
    }
}

// Modules
pub mod analysis;
pub mod circuit;
pub mod comps;
pub mod proto;

// Re-exports
pub use analysis::*;
pub use proto::*;

// Crate-wide public
pub(crate) use spnum::*;
pub(crate) use spresult::*;

// Private modules
mod assert;
mod sparse21;
mod spnum;
mod spresult;
mod tests;