Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtbuilds committed Sep 15, 2024
1 parent 3c43b54 commit 752d418
Show file tree
Hide file tree
Showing 7 changed files with 71 additions and 69 deletions.
1 change: 1 addition & 0 deletions Cargo.lock

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

8 changes: 0 additions & 8 deletions libninja/src/rust/codegen.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,11 +70,3 @@ pub fn serde_rename(value: &str, ident: &Ident) -> TokenStream {
TokenStream::new()
}
}

pub fn serde_rename2(value: &str, ident: &Ident) -> Option<TokenStream> {
if ident.0 != value {
Some(quote!(#[serde(rename = #value)]))
} else {
None
}
}
61 changes: 4 additions & 57 deletions libninja/src/rust/lower_hir.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,15 +4,15 @@ use convert_case::Casing;
use proc_macro2::TokenStream;
use quote::{quote, ToTokens};

use crate::rust::codegen::ToRustType;
use hir::{HirField, HirSpec, NewType, Record, Struct};
use ln_core::{ConfigFlags, PackageConfig};
use mir::{import, Field, File, Ident, Import, Variant, Visibility};
use mir::{import, Field, File, Ident, Import, Visibility};
use mir::{DateSerialization, DecimalSerialization, IntegerSerialization, Ty};
use mir_rust::ToRustCode;
use mir_rust::{derives_to_tokens, lower_enum};
use mir_rust::{sanitize_filename, RustExtra, ToRustIdent};

use crate::rust::codegen::{serde_rename2, ToRustType};

pub trait FieldExt {
fn decorators(&self, name: &str, config: &ConfigFlags) -> Vec<TokenStream>;
}
Expand Down Expand Up @@ -304,46 +304,6 @@ pub fn create_sumtype_struct(
}
}

fn create_enum_struct(e: &hir::Enum, derives: &Vec<String>) -> TokenStream {
let variants = e
.variants
.iter()
.map(|s| {
let ident = if let Some(a) = &s.alias {
a.to_rust_struct()
} else {
let mut s = s.value.clone();
if !s.is_empty() && s.chars().next().unwrap().is_numeric() {
s = format!("{}{}", e.name, s);
}
s.to_rust_struct()
};
let rename = serde_rename2(&s.value, &ident);
Variant {
ident,
doc: None,
value: None,
extra: RustExtra {
attributes: rename.into_iter().collect(),
},
}
})
.collect();
let derives = derives_to_tokens(derives);
let derives = quote! { #[derive(Debug, Serialize, Deserialize #derives)] };
mir::Enum {
name: e.name.to_rust_struct(),
doc: e.doc.clone(),
variants,
vis: Visibility::Public,
methods: Vec::new(),
extra: RustExtra {
attributes: vec![derives],
},
}
.to_rust_code()
}

pub fn create_newtype_struct(
schema: &NewType,
spec: &HirSpec,
Expand Down Expand Up @@ -381,24 +341,11 @@ pub fn create_struct(record: &Record, config: &PackageConfig, hir: &HirSpec) ->
match record {
Record::Struct(s) => create_sumtype_struct(s, &config.config, hir, &config.derives),
Record::NewType(nt) => create_newtype_struct(nt, hir, &config.derives),
Record::Enum(en) => create_enum_struct(en, &config.derives),
Record::Enum(en) => lower_enum(en, &config.derives).to_rust_code(),
Record::TypeAlias(name, field) => create_typealias(name, field),
}
}

pub fn derives_to_tokens(derives: &Vec<String>) -> TokenStream {
derives
.iter()
.map(|d| {
if let Ok(d) = d.trim().parse::<TokenStream>() {
quote! { , #d }
} else {
return TokenStream::new();
}
})
.collect()
}

#[cfg(test)]
mod tests {
use hir::HirField;
Expand Down
4 changes: 2 additions & 2 deletions libninja/src/rust/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,14 +9,14 @@ use regex::Captures;
use hir::{HirSpec, Operation};
use hir::{Language, Location, Parameter};
use ln_core::PackageConfig;
use mir::{Class, Field, FnArg2, Function, Ident, Visibility};
use mir::Doc;
use mir::Ty;
use mir::{Class, Field, FnArg2, Function, Ident, Visibility};
use mir_rust::{ToRustCode, ToRustIdent};

use crate::rust::codegen::ToRustType;

use super::lower_hir::derives_to_tokens;
use mir_rust::derives_to_tokens;

pub fn assign_inputs_to_request(inputs: &[Parameter]) -> TokenStream {
let params_except_path: Vec<&Parameter> = inputs
Expand Down
1 change: 1 addition & 0 deletions mir_rust/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,3 +17,4 @@ syn = "2.0.48"
convert_case = "0.6.0"
regex = "1.10.3"
prettyplease = "0.2.16"
libninja_hir = { path = "../hir" }
43 changes: 41 additions & 2 deletions mir_rust/src/enum.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,47 @@
use crate::{RustExtra, ToRustCode};
use mir::{Enum, Variant};
use crate::{derives_to_tokens, serde_rename2, RustExtra, ToRustCode, ToRustIdent};
use mir::{Enum, Variant, Visibility};
use proc_macro2::TokenStream;
use quote::quote;

pub fn lower_enum(e: &hir::Enum, derives: &[String]) -> Enum<TokenStream, RustExtra> {
let variants = e
.variants
.iter()
.map(|s| {
let ident = if let Some(a) = &s.alias {
a.to_rust_struct()
} else {
let mut s = s.value.clone();
if !s.is_empty() && s.chars().next().unwrap().is_numeric() {
s = format!("{}{}", e.name, s);
}
s.to_rust_struct()
};
let rename = serde_rename2(&s.value, &ident);
Variant {
ident,
doc: None,
value: None,
extra: RustExtra {
attributes: rename.into_iter().collect(),
},
}
})
.collect();
let derives = derives_to_tokens(derives);
let derives = quote! { #[derive(Debug, Serialize, Deserialize #derives)] };
Enum {
name: e.name.to_rust_struct(),
doc: e.doc.clone(),
variants,
vis: Visibility::Public,
methods: Vec::new(),
extra: RustExtra {
attributes: vec![derives],
},
}
}

impl ToRustCode for Enum<TokenStream, RustExtra> {
fn to_rust_code(self) -> TokenStream {
let Enum {
Expand Down
22 changes: 22 additions & 0 deletions mir_rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,28 @@ mod r#enum;
mod file;
mod function;
mod import;
pub use r#enum::lower_enum;

pub fn serde_rename2(value: &str, ident: &Ident) -> Option<TokenStream> {
if ident.0 != value {
Some(quote!(#[serde(rename = #value)]))
} else {
None
}
}

pub fn derives_to_tokens(derives: &[String]) -> TokenStream {
derives
.iter()
.map(|d| {
if let Ok(d) = d.trim().parse::<TokenStream>() {
quote! { , #d }
} else {
return TokenStream::new();
}
})
.collect()
}

#[derive(Debug, Clone)]
pub struct RustExtra {
Expand Down

0 comments on commit 752d418

Please sign in to comment.