Skip to content

Commit

Permalink
add serde support
Browse files Browse the repository at this point in the history
  • Loading branch information
zao111222333 committed May 15, 2024
1 parent b53fb8c commit 0ee679b
Show file tree
Hide file tree
Showing 39 changed files with 247 additions and 84 deletions.
2 changes: 1 addition & 1 deletion .vscode/settings.json
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
{
"window.zoomLevel": 2,
"window.zoomLevel": 1,
"[rust]": {
"editor.defaultFormatter": "rust-lang.rust-analyzer",
"editor.formatOnSave": true
Expand Down
19 changes: 10 additions & 9 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ license = "MIT"
edition = "2021"
authors = ["Junzhuo <[email protected]>"]
keywords = [
"eda",
"eda",
"liberty",
"database",
"parser",
Expand Down Expand Up @@ -42,10 +42,11 @@ rustdoc-args = ["--cfg", "docsrs", "--html-in-header", "docs/header.html" ]
default = []

[dependencies]
liberty-macros = "0.4.7"
# liberty-macros = { path = "macros" }
# liberty-macros = "0.4.7"
liberty-macros = { path = "macros" }
log = { version = "0.4", features = ["std", "serde"] }
ordered-float = { version = "4.0", default-features = false }
serde = { version = "1.0", features = ["derive"] }
ordered-float = { version = "4.0", features = ["serde"] }
nom = "7.1"
thiserror = "1.0"
lazy_static = "1.4.0"
Expand All @@ -57,11 +58,11 @@ prettytable-rs = "^0.10"
itertools = "0.10.5"
ryu = "1.0"
itoa = "1.0"
uom = { version = "0.36.0", default-features = false, features = ["f64","si", "std","serde"] }
biodivine-lib-bdd = "0.5"
# biodivine-lib-bdd = {path = "../../biodivine-lib-bdd"}
mut_set = "0.3.4"
mut_set_derive = "0.3.4"
uom = { version = "0.36.0", default-features = false, features = ["f64", "si", "std", "serde"] }
# biodivine-lib-bdd = "0.5"
biodivine-lib-bdd = {path = "../../biodivine-lib-bdd" , features = ["serde"]}
mut_set = "0.3.5"
mut_set_derive = "0.3.5"
# mut_set = {path = "../mut_set"}
# mut_set_derive = {path = "../mut_set/derive"}
duplicate = "1.0.0"
Expand Down
1 change: 1 addition & 0 deletions macros/src/attribute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -428,6 +428,7 @@ fn main() {
use syn::{parse_str, Data};
let input = r#"
#[derive(liberty_macros::Group)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Foo {
#[liberty(undefined)]
undefined: bool,
Expand Down
126 changes: 64 additions & 62 deletions macros/src/group.rs
Original file line number Diff line number Diff line change
Expand Up @@ -383,11 +383,12 @@ pub(crate) fn inner(
_ => {
let i = Ident::new(&format!("{}Name", ident), Span::call_site());
let mut s: DeriveInput = parse_quote! {
#[doc(hidden)]
#[derive(Debug,Clone)]
pub struct #i{
}
};
#[doc(hidden)]
#[derive(Debug,Clone)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct #i{
}
};
let s_fileds = fields_of_input(&mut s);
let mut _name = quote!();
let mut _set_name = quote!();
Expand Down Expand Up @@ -423,69 +424,70 @@ pub(crate) fn inner(
}
};
let impl_group = quote! {
#named_group_impl
#name_sturct
#[doc(hidden)]
#[derive(Default,Debug,Clone)]
pub struct #comments_ident{
pub #comments_self: crate::ast::AttriComment,
pub #comments_undefined_bgn: crate::ast::AttriComment,
pub #comments_undefined_end: crate::ast::AttriComment,
#attri_comments
}
#[doc(hidden)]
impl crate::ast::GroupAttri for #ident {
type Name=#name_ident;
type Comments=#comments_ident;
#name_func
fn fmt_liberty<T: std::fmt::Write>(&self, key: &str, f: &mut crate::ast::CodeFormatter<'_, T>) -> std::fmt::Result {
use std::fmt::Write;
use itertools::Itertools;
#write_title
f.indent(1);
#write_fields
if !self.#undefined_name.is_empty(){
<crate::ast::AttriComment as crate::ast::Format>::liberty(&self.#comments_name.#comments_undefined_bgn, "", f)?;
crate::ast::liberty_attr_list(&self.#undefined_name,f)?;
<crate::ast::AttriComment as crate::ast::Format>::liberty(&self.#comments_name.#comments_undefined_end, "", f)?;
#named_group_impl
#name_sturct
#[doc(hidden)]
#[derive(Default,Debug,Clone)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct #comments_ident{
pub #comments_self: crate::ast::AttriComment,
pub #comments_undefined_bgn: crate::ast::AttriComment,
pub #comments_undefined_end: crate::ast::AttriComment,
#attri_comments
}
f.dedent(1);
write!(f, "\n}}")
}
fn nom_parse<'a>(
i: &'a str, line_num: &mut usize
) -> nom::IResult<&'a str, Result<Self,crate::ast::IdError>, nom::error::Error<&'a str>> {
let (mut input,title) = crate::ast::parser::title(i,line_num)?;
let mut res = Self::default();
res.#comments_name.#comments_undefined_bgn.push("Undefined attributes from here".to_string());
res.#comments_name.#comments_undefined_end.push("Undefined attributes end here".to_string());
loop {
match crate::ast::parser::key(input){
Err(nom::Err::Error(_)) => {
(input,_) = crate::ast::parser::end_group(input)?;
#link_self
#change_id_return
},
Err(e) => return Err(e),
Ok((_input,key)) => {
input = _input;
match key {
#parser_arms
_ => {
let undefined: crate::ast::AttriValue;
(input,undefined) = crate::ast::parser::undefine(input,line_num)?;
res.#undefined_name.push((key.to_owned(), undefined));
let n: usize;
(input,n) = crate::ast::parser::comment_space_newline(input)?;
*line_num+=n;
#[doc(hidden)]
impl crate::ast::GroupAttri for #ident {
type Name=#name_ident;
type Comments=#comments_ident;
#name_func
fn fmt_liberty<T: std::fmt::Write>(&self, key: &str, f: &mut crate::ast::CodeFormatter<'_, T>) -> std::fmt::Result {
use std::fmt::Write;
use itertools::Itertools;
#write_title
f.indent(1);
#write_fields
if !self.#undefined_name.is_empty(){
<crate::ast::AttriComment as crate::ast::Format>::liberty(&self.#comments_name.#comments_undefined_bgn, "", f)?;
crate::ast::liberty_attr_list(&self.#undefined_name,f)?;
<crate::ast::AttriComment as crate::ast::Format>::liberty(&self.#comments_name.#comments_undefined_end, "", f)?;
}
f.dedent(1);
write!(f, "\n}}")
}
fn nom_parse<'a>(
i: &'a str, line_num: &mut usize
) -> nom::IResult<&'a str, Result<Self,crate::ast::IdError>, nom::error::Error<&'a str>> {
let (mut input,title) = crate::ast::parser::title(i,line_num)?;
let mut res = Self::default();
res.#comments_name.#comments_undefined_bgn.push("Undefined attributes from here".to_string());
res.#comments_name.#comments_undefined_end.push("Undefined attributes end here".to_string());
loop {
match crate::ast::parser::key(input){
Err(nom::Err::Error(_)) => {
(input,_) = crate::ast::parser::end_group(input)?;
#link_self
#change_id_return
},
Err(e) => return Err(e),
Ok((_input,key)) => {
input = _input;
match key {
#parser_arms
_ => {
let undefined: crate::ast::AttriValue;
(input,undefined) = crate::ast::parser::undefine(input,line_num)?;
res.#undefined_name.push((key.to_owned(), undefined));
let n: usize;
(input,n) = crate::ast::parser::comment_space_newline(input)?;
*line_num+=n;
},
}
}
}
}
}
}
}
}
};
};
Ok(quote! {
#impl_group
})
Expand Down
5 changes: 4 additions & 1 deletion src/ast/mod.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//!
//! This crate implement `liberty` data structre in Rust.
//! `liberty` data structre ast
//!
mod fmt;
Expand Down Expand Up @@ -27,6 +27,7 @@ pub type ComplexWrapper = Vec<Vec<String>>;
/// }
/// ```
#[derive(Debug, Clone, Default)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct GroupWrapper {
/// title
pub title: Vec<String>,
Expand All @@ -37,6 +38,7 @@ pub struct GroupWrapper {
pub type AttributeList = Vec<(String, AttriValue)>;
/// AttriValue for undefined_attribute/serialization
#[derive(Debug, Clone)]
#[derive(serde::Serialize, serde::Deserialize)]
pub enum AttriValue {
///
Simple(SimpleWrapper),
Expand Down Expand Up @@ -70,6 +72,7 @@ impl PartialEq for LinkError {

// /// Reference: https://rustcc.cn/article?id=ac75148b-6eb0-4249-b36d-0a14875b736e
// #[derive(Debug, Clone)]
// #[derive(serde::Serialize, serde::Deserialize)]
// pub struct LinkedGroup<LinkTo>
// where
// LinkTo: HashedGroup + GroupAttri,
Expand Down
4 changes: 4 additions & 0 deletions src/bundle.rs
Original file line number Diff line number Diff line change
@@ -1,2 +1,6 @@
/// <script>
/// IFRAME('https://zao111222333.github.io/liberty-db/2020.09/reference_manual.html');
/// </script>
#[derive(Debug, Clone)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Bundle {}
4 changes: 4 additions & 0 deletions src/bus.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
/// <script>
/// IFRAME('https://zao111222333.github.io/liberty-db/2020.09/reference_manual.html');
/// </script>
#[derive(Debug, Clone)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Bus {
pub group_name: String,
}
9 changes: 9 additions & 0 deletions src/ccsn.rs
Original file line number Diff line number Diff line change
Expand Up @@ -32,12 +32,16 @@ use crate::{
/// <a name ="reference_link" href="
/// https://zao111222333.github.io/liberty-db/2020.09/reference_manual.html?field=null&bgn=283.21&end=283.43
/// ">Reference-Definition</a>
/// <script>
/// IFRAME('https://zao111222333.github.io/liberty-db/2020.09/reference_manual.html');
/// </script>
#[derive(Debug, Default, Clone)]
#[derive(liberty_macros::Group)]
#[mut_set_derive::item(
sort,
macro(derive(Debug, Clone,Default);)
)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct CCSNStage {
#[liberty(name)]
#[id]
Expand Down Expand Up @@ -218,6 +222,7 @@ impl GroupFn for CCSNStage {
#[derive(Hash, PartialEq, Eq)]
#[derive(Ord, PartialOrd, Default)]
#[derive(strum_macros::EnumString, strum_macros::EnumIter, strum_macros::Display)]
#[derive(serde::Serialize, serde::Deserialize)]
pub enum StageType {
/// pull_up,in which the output voltage of the channel-connecting block is always pulled up (rising);
#[strum(serialize = "pull_up")]
Expand Down Expand Up @@ -249,12 +254,16 @@ impl SimpleAttri for StageType {}
/// <a name ="reference_link" href="
/// https://zao111222333.github.io/liberty-db/2020.09/reference_manual.html?field=null&bgn=316.5&end=316.31
/// ">Reference-Definition</a>
/// <script>
/// IFRAME('https://zao111222333.github.io/liberty-db/2020.09/reference_manual.html');
/// </script>
#[derive(Debug, Default, Clone)]
#[derive(liberty_macros::Group)]
#[mut_set_derive::item(
sort,
macro(derive(Debug, Clone,Default);)
)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct ReceiverCapacitance {
#[id]
#[liberty(name)]
Expand Down
15 changes: 9 additions & 6 deletions src/cell/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ use crate::{
sort,
macro(derive(Debug, Clone,Default);)
)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct LeakagePower {
#[id]
#[liberty(name)]
Expand Down Expand Up @@ -61,6 +62,7 @@ impl GroupFn for LeakagePower {}
sort,
macro(derive(Debug, Clone,Default);)
)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Statetable {
#[id]
#[liberty(name)]
Expand Down Expand Up @@ -108,13 +110,9 @@ impl NamedGroup for Statetable {
}
}

// #[derive(Debug, Default, Clone, Hash, Eq, PartialEq)]
// pub struct StatetableId {
// pub input_npde: Vec<String>,
// pub internal_node: Vec<String>,
// }

/// StateTable Table
#[derive(Default, Debug, Clone)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Table {
pub v: Vec<String>,
}
Expand Down Expand Up @@ -198,12 +196,16 @@ fn statetable_test() {
/// <a name ="reference_link" href="
/// https://zao111222333.github.io/liberty-db/2020.09/reference_manual.html?field=null&bgn=209.3&end=209.6
/// ">Reference-Definition</a>
/// <script>
/// IFRAME('https://zao111222333.github.io/liberty-db/2020.09/reference_manual.html');
/// </script>
#[derive(Debug, Default, Clone)]
#[derive(liberty_macros::Group)]
#[mut_set_derive::item(
sort,
macro(derive(Debug, Clone,Default);)
)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct PgPin {
#[liberty(name)]
#[id]
Expand Down Expand Up @@ -285,6 +287,7 @@ impl GroupFn for PgPin {}
#[derive(Hash, PartialEq, Eq)]
#[derive(Ord, PartialOrd, Default)]
#[derive(strum_macros::EnumString, strum_macros::EnumIter, strum_macros::Display)]
#[derive(serde::Serialize, serde::Deserialize)]
pub enum PgType {
/// primary_power
#[strum(serialize = "primary_power")]
Expand Down
1 change: 1 addition & 0 deletions src/cell/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ pub use items::*;
sort,
macro(derive(Debug, Clone, Default);)
)]
#[derive(serde::Serialize, serde::Deserialize)]
pub struct Cell {
#[id]
#[liberty(name)]
Expand Down
Loading

0 comments on commit 0ee679b

Please sign in to comment.