Skip to content

Commit

Permalink
Init zngur autozng
Browse files Browse the repository at this point in the history
  • Loading branch information
HKalbasi committed Aug 5, 2024
1 parent 86a4568 commit 23fbc0e
Show file tree
Hide file tree
Showing 5 changed files with 267 additions and 8 deletions.
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -2,3 +2,4 @@
a.out
.vscode
compile_commands.json
zngur-autozng/doc.json
89 changes: 81 additions & 8 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@ members = [
"zngur-def",
"zngur-generator",
"zngur-parser",
"zngur-autozng",
"examples/*",
"xtask",
]
Expand Down
9 changes: 9 additions & 0 deletions zngur-autozng/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
[package]
name = "zngur-autozng"
version = "0.1.0"
edition.workspace = true
license.workspace = true

[dependencies]
serde = { version = "1.0.204", features = ["derive"] }
serde_json = "1.0.122"
175 changes: 175 additions & 0 deletions zngur-autozng/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,175 @@
use std::collections::HashMap;

use serde::{Deserialize, Serialize};
use serde_json::Value;

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
enum RustdocRustType {
BorrowedRef {
mutable: bool,
#[serde(rename = "type")]
inner: Box<RustdocRustType>,
},
RawPointer {
mutable: bool,
#[serde(rename = "type")]
inner: Box<RustdocRustType>,
},
Primitive(String),
Generic(String),
Tuple(Vec<RustdocRustType>),
Slice(Box<RustdocRustType>),
ResolvedPath {
name: String,
},
QualifiedPath {},
}

impl RustdocRustType {
fn render(&self) -> String {
match self {
RustdocRustType::BorrowedRef {
mutable: false,
inner,
} => format!("&{}", inner.render()),
RustdocRustType::BorrowedRef {
mutable: true,
inner,
} => format!("&mut {}", inner.render()),
RustdocRustType::RawPointer { .. } => todo!(),
RustdocRustType::Primitive(n) => n.clone(),
RustdocRustType::Generic(n) => n.clone(),
RustdocRustType::Tuple(_) => todo!(),
RustdocRustType::Slice(_) => todo!(),
RustdocRustType::ResolvedPath { name } => name.clone(),
RustdocRustType::QualifiedPath {} => todo!(),
}
}
}

#[derive(Debug, Serialize, Deserialize)]
struct RustdocFunctionDecl {
inputs: Vec<(String, RustdocRustType)>,
output: Option<RustdocRustType>,
#[serde(flatten)]
other_fields: Value,
}

#[derive(Debug, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
enum RustdocItemInner {
Function {
decl: RustdocFunctionDecl,
#[serde(flatten)]
other_fields: Value,
},
Struct {
impls: Vec<String>,
#[serde(flatten)]
other_fields: Value,
},
Impl {
items: Vec<String>,
#[serde(rename = "trait")]
for_trait: Option<serde_json::Value>,
#[serde(flatten)]
other_fields: Value,
},
Module {
#[serde(flatten)]
other_fields: Value,
},
StructField {
#[serde(flatten)]
other_fields: Value,
},
Import {
#[serde(flatten)]
other_fields: Value,
},
AssocType {
#[serde(flatten)]
other_fields: Value,
},
Variant {
#[serde(flatten)]
other_fields: Value,
},
TypeAlias {
#[serde(flatten)]
other_fields: Value,
},
Enum {
impls: Vec<String>,
#[serde(flatten)]
other_fields: Value,
},
}

#[derive(Debug, Serialize, Deserialize)]
struct RustdocItem {
name: Option<String>,
inner: RustdocItemInner,
#[serde(flatten)]
other_fields: Value,
}

#[derive(Serialize, Deserialize)]
struct RustdocOutput {
index: HashMap<String, RustdocItem>,
}

fn main() {
let s = std::fs::read_to_string("./doc.json").unwrap();
let d: RustdocOutput = serde_json::from_str(&s).unwrap();
for x in &d.index {
if let RustdocItemInner::Struct { impls, .. } | RustdocItemInner::Enum { impls, .. } =
&x.1.inner
{
println!("type crate::{} {{", x.1.name.as_ref().unwrap());
println!(" #heap_allocated;");
for imp in impls {
let imp = &d.index[imp];
// dbg!(imp);

if let RustdocItemInner::Impl {
items, for_trait, ..
} = &imp.inner
{
if for_trait.is_some() {
continue;
}
for item in items {
let item = &d.index[item];
if let RustdocItemInner::Function {
decl: RustdocFunctionDecl { inputs, output, .. },
..
} = &item.inner
{
print!(" fn {}(", item.name.as_deref().unwrap());
let mut first = true;
for (name, ty) in inputs {
if !first {
print!(", ");
}
first = false;
if name == "self" {
print!("self");
continue;
}
print!("{}", ty.render());
}
print!(")");
if let Some(output) = output {
print!(" -> {}", output.render());
}
println!(";");
}
}
}
}
println!("}}");
}
}
}

0 comments on commit 23fbc0e

Please sign in to comment.