Skip to content

Commit

Permalink
fix clippy
Browse files Browse the repository at this point in the history
  • Loading branch information
zao111222333 committed Jan 1, 2025
1 parent 7ce2c47 commit 00e4cda
Show file tree
Hide file tree
Showing 13 changed files with 233 additions and 144 deletions.
56 changes: 28 additions & 28 deletions src/ast/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,17 @@
mod fmt;
pub mod parser;
use crate::{library::AttributeType, ArcStr};
use core::hash::{BuildHasher as _, Hash, Hasher as _};
use core::{fmt::Write, num::ParseIntError, str::FromStr};
use crate::{common::f64_into_hash_ord_fn, library::AttributeType, ArcStr};
use core::{
cmp::Ordering,
fmt::Write,
hash::{BuildHasher as _, Hash as _, Hasher as _},
num::ParseIntError,
str::FromStr,
};
pub use fmt::{CodeFormatter, DefaultCodeFormatter, DefaultIndentation, Indentation};
use itertools::{izip, Itertools as _};
use nom::{error::Error, IResult};
use ordered_float::NotNan;
use std::collections::HashMap;
const DEFINED_COMMENT: &str = " /* user defined attribute */";

Expand Down Expand Up @@ -83,13 +87,13 @@ pub struct GroupWrapper {
}
impl Ord for GroupWrapper {
#[inline]
fn cmp(&self, other: &Self) -> core::cmp::Ordering {
fn cmp(&self, other: &Self) -> Ordering {
self.title.cmp(&other.title)
}
}
impl PartialOrd for GroupWrapper {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<core::cmp::Ordering> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
Expand Down Expand Up @@ -135,9 +139,7 @@ impl PartialEq for SimpleDefined {
(Self::Float(l0), Self::Float(r0)) => {
l0.len() == r0.len()
&& izip!(l0, r0).all(|lr| match lr {
(Ok(l), Ok(r)) => unsafe {
NotNan::new_unchecked(*l) == NotNan::new_unchecked(*r)
},
(Ok(l), Ok(r)) => f64_into_hash_ord_fn(l) == f64_into_hash_ord_fn(r),
(Err(l), Err(r)) => l == r,
_ => false,
})
Expand All @@ -149,39 +151,37 @@ impl PartialEq for SimpleDefined {
impl Eq for SimpleDefined {}
impl PartialOrd for SimpleDefined {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<std::cmp::Ordering> {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Ord for SimpleDefined {
#[inline]
fn cmp(&self, other: &Self) -> std::cmp::Ordering {
fn cmp(&self, other: &Self) -> Ordering {
match (self, other) {
(SimpleDefined::Boolean(l), SimpleDefined::Boolean(r)) => l.cmp(r),
(SimpleDefined::String(l), SimpleDefined::String(r)) => l.cmp(r),
(SimpleDefined::Integer(l), SimpleDefined::Integer(r)) => l.cmp(r),
(SimpleDefined::Float(l), SimpleDefined::Float(r)) => match l.len().cmp(&r.len()) {
std::cmp::Ordering::Less => std::cmp::Ordering::Less,
std::cmp::Ordering::Greater => std::cmp::Ordering::Greater,
std::cmp::Ordering::Equal => l
(Self::Boolean(l), Self::Boolean(r)) => l.cmp(r),
(Self::String(l), Self::String(r)) => l.cmp(r),
(Self::Integer(l), Self::Integer(r)) => l.cmp(r),
(Self::Float(l), Self::Float(r)) => match l.len().cmp(&r.len()) {
Ordering::Less => Ordering::Less,
Ordering::Greater => Ordering::Greater,
Ordering::Equal => l
.iter()
.map(|res| match res {
Ok(f) => Ok(unsafe { NotNan::new_unchecked(*f) }),
Ok(f) => Ok(f64_into_hash_ord_fn(f)),
Err(s) => Err(s),
})
.cmp(r.iter().map(|res| match res {
Ok(f) => Ok(unsafe { NotNan::new_unchecked(*f) }),
Ok(f) => Ok(f64_into_hash_ord_fn(f)),
Err(s) => Err(s),
})),
},
(SimpleDefined::Boolean(_), _) => std::cmp::Ordering::Less,
(_, SimpleDefined::Boolean(_)) => std::cmp::Ordering::Greater,
(_, SimpleDefined::Float(_)) => std::cmp::Ordering::Less,
(SimpleDefined::Float(_), _) => std::cmp::Ordering::Greater,
(SimpleDefined::String(_), SimpleDefined::Integer(_)) => std::cmp::Ordering::Less,
(SimpleDefined::Integer(_), SimpleDefined::String(_)) => {
std::cmp::Ordering::Greater
}
(Self::Boolean(_), _)
| (_, Self::Float(_))
| (Self::String(_), Self::Integer(_)) => Ordering::Less,
(Self::Float(_), _)
| (_, Self::Boolean(_))
| (Self::Integer(_), Self::String(_)) => Ordering::Greater,
}
}
}
Expand Down
3 changes: 1 addition & 2 deletions src/ast/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ pub(crate) fn simple<'a>(
}
#[inline]
pub(crate) fn float_one(i: &str) -> IResult<&str, f64, Error<&str>> {
#[expect(clippy::string_slice, clippy::undocumented_unsafe_blocks)]
#[expect(clippy::string_slice)]
match fast_float2::parse_partial(i) {
Ok((f, pos)) => Ok((&i[pos..], f)),
Err(_) => Err(nom::Err::Error(Error::new(i, ErrorKind::Float))),
Expand All @@ -388,7 +388,6 @@ fn int_usize(i: &str) -> IResult<&str, usize, Error<&str>> {
}

#[inline]
#[expect(clippy::type_complexity)]
pub(crate) fn complex_id_vector<'a>(
i: &'a str,
line_num: &mut usize,
Expand Down
29 changes: 22 additions & 7 deletions src/cell/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,10 @@ use crate::{
join_fmt, CodeFormatter, ComplexAttri, ComplexParseError, GroupComments, GroupFn,
GroupSet, Indentation, NamedGroup, ParseScope, SimpleAttri, SimpleParseRes,
},
common::{items::WordSet, table::CompactCcsPower},
common::{
items::{NameList, WordSet},
table::CompactCcsPower,
},
expression::{logic, LogicBooleanExpression, PowerGroundBooleanExpression},
pin::Direction,
timing::items::Mode,
Expand Down Expand Up @@ -46,10 +49,14 @@ pub struct LeakagePower<C: Ctx> {
#[size = 8]
#[liberty(simple(type = Option))]
pub power_level: Option<ArcStr>,
#[id]
#[id(
borrow = "crate::common::items::RefNameList<'_>",
check_fn = "crate::common::items::namelist_borrow",
with_ref = false
)]
#[size = 64]
#[liberty(simple)]
pub related_pg_pin: WordSet,
pub related_pg_pin: NameList,
#[id(
borrow = "Option<&LogicBooleanExpression>",
check_fn = "mut_set::borrow_option!",
Expand Down Expand Up @@ -502,14 +509,22 @@ pub struct DynamicCurrent<C: Ctx> {
#[size = 80]
#[liberty(simple(type = Option))]
pub when: Option<LogicBooleanExpression>,
#[id]
#[id(
borrow = "crate::common::items::RefNameList<'_>",
check_fn = "crate::common::items::namelist_borrow",
with_ref = false
)]
#[size = 64]
#[liberty(simple)]
pub related_inputs: WordSet,
#[id]
pub related_inputs: NameList,
#[id(
borrow = "crate::common::items::RefNameList<'_>",
check_fn = "crate::common::items::namelist_borrow",
with_ref = false
)]
#[size = 64]
#[liberty(simple)]
pub related_outputs: WordSet,
pub related_outputs: NameList,
#[size = 24]
#[liberty(complex(type = Option))]
pub typical_capacitances: Option<Vec<f64>>,
Expand Down
65 changes: 60 additions & 5 deletions src/common/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use crate::{
},
ArcStr,
};
use core::fmt::{self, Write};
use core::{
fmt::{self, Write},
str::FromStr,
};
use itertools::Itertools as _;

use super::{
Expand All @@ -18,7 +21,6 @@ use super::{
crate::ast::impl_self_builder!(f64);
impl SimpleAttri for f64 {
#[inline]
#[expect(clippy::undocumented_unsafe_blocks)]
fn nom_parse<'a>(i: &'a str, scope: &mut ParseScope) -> ast::SimpleParseRes<'a, Self> {
ast::parser::simple_custom(i, &mut scope.line_num, ast::parser::float_one)
}
Expand Down Expand Up @@ -117,8 +119,7 @@ impl NameAttri for ArcStr {
impl NameAttri for NameList {
#[inline]
fn parse(v: Vec<&str>) -> Result<Self, IdError> {
let l = v.len();
match l {
match v.len() {
0 => Err(IdError::length_dismatch(1, 0, v)),
#[expect(clippy::indexing_slicing)]
1 => Ok(Self::Name(v[0].into())),
Expand All @@ -133,6 +134,60 @@ impl NameAttri for NameList {
write!(f, "{self}")
}
}
impl FromStr for NameList {
type Err = ();
#[inline]
#[expect(clippy::unwrap_in_result, clippy::unwrap_used)]
fn from_str(s: &str) -> Result<Self, Self::Err> {
let mut v: Vec<_> = s
.split(' ')
.filter_map(|_s| if _s.is_empty() { None } else { Some(ArcStr::from(_s)) })
.collect();
match v.len() {
0 => Err(()),
1 => Ok(Self::Name(v.pop().unwrap())),
_ => Ok(Self::List(WordSet { inner: v.into_iter().map(ArcStr::from).collect() })),
}
}
}
crate::ast::impl_self_builder!(NameList);
impl SimpleAttri for NameList {
#[inline]
fn nom_parse<'a>(
i: &'a str,
scope: &mut ParseScope,
) -> ast::SimpleParseRes<'a, Self::Builder> {
ast::nom_parse_from_str(i, scope)
}
#[inline]
fn is_set(&self) -> bool {
match self {
NameList::Name(s) => !s.is_empty(),
NameList::List(word_set) => word_set.is_set(),
}
}
#[inline]
fn fmt_self<T: Write, I: Indentation>(
&self,
f: &mut CodeFormatter<'_, T, I>,
) -> fmt::Result {
match self {
Self::Name(s) => {
if is_word(s) {
write!(f, "{s}")
} else {
write!(f, "\"{s}\"")
}
}
Self::List(set) => join_fmt_no_quote(
set.inner.iter().sorted(),
f,
|s, ff| if is_word(s) { write!(ff, "{s}") } else { write!(ff, "\"{s}\"") },
" ",
),
}
}
}

impl fmt::Display for NameList {
#[inline]
Expand Down Expand Up @@ -263,7 +318,7 @@ impl<const N: usize> ast::ParsingBuilder for [f64; N] {
builder
}
}
// FIXME

impl<const N: usize> ComplexAttri for [f64; N] {
#[inline]
fn parse<'a, I: Iterator<Item = &'a &'a str>>(
Expand Down
26 changes: 12 additions & 14 deletions src/common/items.rs
Original file line number Diff line number Diff line change
Expand Up @@ -187,12 +187,10 @@ impl fmt::Display for WordSet {
impl Ord for WordSet {
#[inline]
fn cmp(&self, other: &Self) -> Ordering {
if self.inner.is_subset(&other.inner) {
Ordering::Less
} else if self.inner.is_superset(&other.inner) {
Ordering::Greater
} else {
Ordering::Equal
match self.inner.len().cmp(&other.inner.len()) {
Ordering::Less => Ordering::Less,
Ordering::Greater => Ordering::Greater,
Ordering::Equal => self.inner.iter().sorted().cmp(other.inner.iter().sorted()),
}
}
}
Expand All @@ -201,14 +199,14 @@ impl Ord for WordSet {
impl PartialOrd for WordSet {
#[inline]
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
if self.inner.len() == other.inner.len() {
self.inner.iter().sorted().partial_cmp(other.inner.iter().sorted())
} else if self.inner.is_subset(&other.inner) {
Some(Ordering::Less)
} else if self.inner.is_superset(&other.inner) {
Some(Ordering::Greater)
} else {
None
match self.inner.len().cmp(&other.inner.len()) {
Ordering::Less => self.inner.is_subset(&other.inner).then_some(Ordering::Less),
Ordering::Greater => {
self.inner.is_superset(&other.inner).then_some(Ordering::Greater)
}
Ordering::Equal => {
self.inner.iter().sorted().partial_cmp(other.inner.iter().sorted())
}
}
}
}
Expand Down
4 changes: 3 additions & 1 deletion src/common/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@ pub(crate) fn parse_f64<S: AsRef<[u8]>>(s: S) -> Result<f64, fast_float2::Error>
fast_float2::parse(s)
}
#[inline]
pub(crate) fn f64_into_hash_ord_fn(val: &f64) -> NotNan<f64> {
#[expect(clippy::trivially_copy_pass_by_ref)]
pub(crate) const fn f64_into_hash_ord_fn(val: &f64) -> NotNan<f64> {
// SAFETY: convert float to hash & ord
unsafe { NotNan::new_unchecked(*val) }
}
10 changes: 2 additions & 8 deletions src/common/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -112,7 +112,6 @@ pub struct DriverWaveform<C: Ctx> {
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(bound = "C::Table: serde::Serialize + serde::de::DeserializeOwned")]
pub struct TableLookUp2D<C: Ctx> {
// TODO: unit
#[size = 8]
#[liberty(name)]
#[id(borrow = "Option<&str>", check_fn = "mut_set::borrow_option!", with_ref = false)]
Expand Down Expand Up @@ -277,7 +276,6 @@ impl SimpleAttri for VariableTypeCompactLutTemplateIndex3 {
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(bound = "C::Table: serde::Serialize + serde::de::DeserializeOwned")]
pub struct Vector3D<C: Ctx> {
// TODO: unit
#[size = 8]
#[liberty(name)]
#[id(borrow = "Option<&str>", check_fn = "mut_set::borrow_option!", with_ref = false)]
Expand Down Expand Up @@ -315,7 +313,6 @@ pub struct Vector3D<C: Ctx> {
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(bound = "C::Table: serde::Serialize + serde::de::DeserializeOwned")]
pub struct ReferenceTimeVector3D<C: Ctx> {
// TODO: unit
#[size = 8]
#[liberty(name)]
#[id(borrow = "Option<&str>", check_fn = "mut_set::borrow_option!", with_ref = false)]
Expand Down Expand Up @@ -357,7 +354,6 @@ pub struct ReferenceTimeVector3D<C: Ctx> {
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(bound = "C::Table: serde::Serialize + serde::de::DeserializeOwned")]
pub struct Vector4D<C: Ctx> {
// TODO: unit
#[size = 8]
#[liberty(name)]
#[id(borrow = "Option<&str>", check_fn = "mut_set::borrow_option!", with_ref = false)]
Expand Down Expand Up @@ -443,7 +439,6 @@ pub struct Vector4D<C: Ctx> {
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(bound = "C::Table: serde::Serialize + serde::de::DeserializeOwned")]
pub struct CompactCcsPower<C: Ctx> {
// TODO: unit
#[size = 8]
#[liberty(name)]
#[id(borrow = "Option<&str>", check_fn = "mut_set::borrow_option!", with_ref = false)]
Expand Down Expand Up @@ -701,7 +696,6 @@ impl<C: Ctx> GroupFn for CompactCcsPower<C> {}
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(bound = "C::Table: serde::Serialize + serde::de::DeserializeOwned")]
pub struct TableLookUp3D<C: Ctx> {
// TODO: unit
#[size = 8]
#[liberty(name)]
#[id(borrow = "Option<&str>", check_fn = "mut_set::borrow_option!", with_ref = false)]
Expand Down Expand Up @@ -737,7 +731,7 @@ pub struct TableLookUp3D<C: Ctx> {
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(bound = "C::Table: serde::Serialize + serde::de::DeserializeOwned")]
pub struct TableLookUp1D<C: Ctx> {
// // TODO: unit
//
// unit: (),
#[size = 8]
#[liberty(name)]
Expand Down Expand Up @@ -774,7 +768,7 @@ impl<C: Ctx> GroupFn for TableLookUp1D<C> {}
#[derive(serde::Serialize, serde::Deserialize)]
#[serde(bound = "C::Table: serde::Serialize + serde::de::DeserializeOwned")]
pub struct CompactCcsTable<C: Ctx> {
// // TODO: unit
//
// unit: (),
#[size = 8]
#[liberty(name)]
Expand Down
Loading

0 comments on commit 00e4cda

Please sign in to comment.