Refactor the ExprDict node (#11267)

Co-authored-by: Micha Reiser <micha@reiser.io>
This commit is contained in:
Alex Waygood 2024-05-07 12:46:10 +01:00 committed by GitHub
parent de270154a1
commit 6774f27f4b
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
52 changed files with 2425 additions and 2240 deletions

View file

@ -687,10 +687,24 @@ pub struct ExprIf<'a> {
orelse: Box<ComparableExpr<'a>>,
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ComparableDictItem<'a> {
key: Option<ComparableExpr<'a>>,
value: ComparableExpr<'a>,
}
impl<'a> From<&'a ast::DictItem> for ComparableDictItem<'a> {
fn from(ast::DictItem { key, value }: &'a ast::DictItem) -> Self {
Self {
key: key.as_ref().map(ComparableExpr::from),
value: value.into(),
}
}
}
#[derive(Debug, PartialEq, Eq, Hash)]
pub struct ExprDict<'a> {
keys: Vec<Option<ComparableExpr<'a>>>,
values: Vec<ComparableExpr<'a>>,
items: Vec<ComparableDictItem<'a>>,
}
#[derive(Debug, PartialEq, Eq, Hash)]
@ -933,16 +947,8 @@ impl<'a> From<&'a ast::Expr> for ComparableExpr<'a> {
body: body.into(),
orelse: orelse.into(),
}),
ast::Expr::Dict(ast::ExprDict {
keys,
values,
range: _,
}) => Self::Dict(ExprDict {
keys: keys
.iter()
.map(|expr| expr.as_ref().map(Into::into))
.collect(),
values: values.iter().map(Into::into).collect(),
ast::Expr::Dict(ast::ExprDict { items, range: _ }) => Self::Dict(ExprDict {
items: items.iter().map(ComparableDictItem::from).collect(),
}),
ast::Expr::Set(ast::ExprSet { elts, range: _ }) => Self::Set(ExprSet {
elts: elts.iter().map(Into::into).collect(),