Skip to content

Commit

Permalink
parse in logic prog
Browse files Browse the repository at this point in the history
  • Loading branch information
StuartHarris committed Nov 28, 2024
1 parent 00930cf commit c2ef303
Show file tree
Hide file tree
Showing 8 changed files with 268 additions and 10,326 deletions.
69 changes: 2 additions & 67 deletions crux_cli/src/codegen/data.rs
Original file line number Diff line number Diff line change
@@ -1,80 +1,15 @@
use std::{collections::HashMap, hash::Hash};
use std::hash::Hash;

use rustdoc_types::{Crate, Id, Item, ItemSummary};
use rustdoc_types::{Id, Item, ItemSummary};
use serde::{Deserialize, Serialize};

pub struct Data {
pub crate_: Crate,
pub nodes_by_id: HashMap<Id, Node>,
}

#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Node {
pub id: Id,
pub item: Option<Item>,
pub summary: Option<ItemSummary>,
}

#[derive(Debug, Clone, Copy, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)]
pub enum Edge {
AssociatedItem,
AssociatedType,
Type,
Field,
Variant,
TraitApp,
TraitEffect,
}

impl Data {
pub fn new(crate_: Crate) -> Self {
let mut nodes_by_id = HashMap::new();

// items
for (id, item) in &crate_.index {
nodes_by_id
.entry(*id)
.or_insert_with(|| Node::new(*id))
.item = Some(item.clone());
}

// paths
for (id, path) in &crate_.paths {
nodes_by_id
.entry(*id)
.or_insert_with(|| Node::new(*id))
.summary = Some(path.clone());
}

Self {
crate_,
nodes_by_id,
}
}

pub fn node_by_id(&self, id: &Id) -> Option<&Node> {
let node = self
.nodes_by_id
.get(id)
.expect("node should exist for all items and paths");
let skip = match &node.item {
Some(x) => x.attrs.contains(&"#[serde(skip)]".to_string()),
_ => false,
};
(!skip).then_some(node)
}
}

impl Node {
pub fn new(id: Id) -> Self {
Self {
id,
item: None,
summary: None,
}
}
}

impl Hash for Node {
fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
self.id.hash(state);
Expand Down
Loading

0 comments on commit c2ef303

Please sign in to comment.