Skip to content

Commit

Permalink
sort and dedup
Browse files Browse the repository at this point in the history
  • Loading branch information
StuartHarris committed Nov 22, 2024
1 parent 1c948a5 commit 3782c34
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 23 deletions.
15 changes: 13 additions & 2 deletions crux_cli/src/codegen/data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use std::collections::HashMap;
use std::{cmp::Ordering, collections::HashMap, hash::Hash};

use rustdoc_types::{Crate, Id, Item, ItemSummary};
use serde::Serialize;
Expand Down Expand Up @@ -64,7 +64,18 @@ impl Node {
}
}

impl std::hash::Hash for Node {
impl Ord for Node {
fn cmp(&self, other: &Self) -> Ordering {
self.id.0.cmp(&other.id.0)
}
}

impl PartialOrd for Node {
fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
Some(self.cmp(other))
}
}
impl Hash for Node {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.id.hash(state);
}
Expand Down
49 changes: 28 additions & 21 deletions crux_cli/src/codegen/generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,8 @@ pub(crate) fn generate(edges: &[(Node, Node)], data: &Data) {
let Some(item) = &from.item else {
continue;
};
let mut container = match &item.inner {

let container = match &item.inner {
ItemEnum::Struct(s) => {
let val = match &s.kind {
StructKind::Unit => ContainerFormat::UnitStruct,
Expand All @@ -44,6 +45,8 @@ pub(crate) fn generate(edges: &[(Node, Node)], data: &Data) {
.entry(name)
.or_insert(ContainerFormat::Enum(Default::default()))
}
// ItemEnum::StructField(_) => (),
// ItemEnum::Variant(variant) => Source::Variant(name, variant.into()),
_ => continue,
};

Expand All @@ -54,7 +57,7 @@ pub(crate) fn generate(edges: &[(Node, Node)], data: &Data) {
continue;
};
match &item.inner {
ItemEnum::StructField(t) => match &mut container {
ItemEnum::StructField(t) => match container {
ContainerFormat::Struct(ref mut v) => {
v.push(Named {
name: name.to_string(),
Expand All @@ -63,25 +66,32 @@ pub(crate) fn generate(edges: &[(Node, Node)], data: &Data) {
}
ContainerFormat::UnitStruct => (),
ContainerFormat::NewTypeStruct(_format) => (),
ContainerFormat::TupleStruct(ref mut _v) => {
ContainerFormat::TupleStruct(ref mut v) => {
println!("{:?}", v);
// v.push(Format::Tuple((data, t).into()));
}
ContainerFormat::Enum(_btree_map) => (),
},
ItemEnum::Variant(t) => {
let ContainerFormat::Enum(ref mut v) = &mut container else {
continue;
};
let value = Named {
name: name.to_string(),
value: t.into(),
};
if v.values().find(|v| v.name == name).is_none() {
v.insert(variant_index, value);
} else {
variant_index -= 1;
ItemEnum::Variant(t) => match container {
ContainerFormat::Enum(ref mut v) => {
let value = Named {
name: name.to_string(),
value: t.into(),
};
if v.values().find(|v| v.name == name).is_none() {
v.insert(variant_index, value);
} else {
variant_index -= 1;
}
}
}
ContainerFormat::UnitStruct => todo!(),
ContainerFormat::NewTypeStruct(_format) => todo!(),
ContainerFormat::TupleStruct(ref mut v) => {
println!("{:?}", v);
// v.push(Format::Tuple((data, t).into()));
}
ContainerFormat::Struct(_vec) => todo!(),
},
_ => continue,
}
// println!("{:?} \n-> {:?}\n", item, to);
Expand Down Expand Up @@ -181,11 +191,8 @@ impl From<&Variant> for VariantFormat {
fn from(value: &Variant) -> Self {
match &value.kind {
VariantKind::Plain => VariantFormat::Unit,
VariantKind::Tuple(_vec) => VariantFormat::Tuple(vec![]),
VariantKind::Struct {
fields: _,
has_stripped_fields: _,
} => todo!(),
VariantKind::Tuple(_) => VariantFormat::Tuple(Default::default()),
VariantKind::Struct { .. } => VariantFormat::Struct(Default::default()),
}
}
}
Expand Down
2 changes: 2 additions & 0 deletions crux_cli/src/codegen/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -299,6 +299,8 @@ pub fn parse(data: &Data) -> Result<Vec<(Node, Node)>> {
let mut all = Vec::new();
all.extend(prog.field);
all.extend(prog.variant);
all.sort_by(|a, b| (&a.0, &a.1).cmp(&(&b.0, &b.1)));
all.dedup();

Ok(all)
}
Expand Down

0 comments on commit 3782c34

Please sign in to comment.