From 752d4187814d8898a6562d9f498d390b821cd543 Mon Sep 17 00:00:00 2001 From: Kurt Wolf Date: Sun, 15 Sep 2024 01:18:39 -0700 Subject: [PATCH] checkpoint --- Cargo.lock | 1 + libninja/src/rust/codegen.rs | 8 ----- libninja/src/rust/lower_hir.rs | 61 +++------------------------------- libninja/src/rust/request.rs | 4 +-- mir_rust/Cargo.toml | 1 + mir_rust/src/enum.rs | 43 ++++++++++++++++++++++-- mir_rust/src/lib.rs | 22 ++++++++++++ 7 files changed, 71 insertions(+), 69 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index d53252d..7654362 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -735,6 +735,7 @@ name = "libninja_mir_rust" version = "0.1.0" dependencies = [ "convert_case", + "libninja_hir", "libninja_mir", "prettyplease", "proc-macro2", diff --git a/libninja/src/rust/codegen.rs b/libninja/src/rust/codegen.rs index b786105..d4df2af 100644 --- a/libninja/src/rust/codegen.rs +++ b/libninja/src/rust/codegen.rs @@ -70,11 +70,3 @@ pub fn serde_rename(value: &str, ident: &Ident) -> TokenStream { TokenStream::new() } } - -pub fn serde_rename2(value: &str, ident: &Ident) -> Option { - if ident.0 != value { - Some(quote!(#[serde(rename = #value)])) - } else { - None - } -} diff --git a/libninja/src/rust/lower_hir.rs b/libninja/src/rust/lower_hir.rs index 5830afa..5f01967 100644 --- a/libninja/src/rust/lower_hir.rs +++ b/libninja/src/rust/lower_hir.rs @@ -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; } @@ -304,46 +304,6 @@ pub fn create_sumtype_struct( } } -fn create_enum_struct(e: &hir::Enum, derives: &Vec) -> 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, @@ -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) -> TokenStream { - derives - .iter() - .map(|d| { - if let Ok(d) = d.trim().parse::() { - quote! { , #d } - } else { - return TokenStream::new(); - } - }) - .collect() -} - #[cfg(test)] mod tests { use hir::HirField; diff --git a/libninja/src/rust/request.rs b/libninja/src/rust/request.rs index da43555..2d2eed5 100644 --- a/libninja/src/rust/request.rs +++ b/libninja/src/rust/request.rs @@ -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 diff --git a/mir_rust/Cargo.toml b/mir_rust/Cargo.toml index 23cb030..cd468b4 100644 --- a/mir_rust/Cargo.toml +++ b/mir_rust/Cargo.toml @@ -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" } \ No newline at end of file diff --git a/mir_rust/src/enum.rs b/mir_rust/src/enum.rs index 874198b..5fe2bbe 100644 --- a/mir_rust/src/enum.rs +++ b/mir_rust/src/enum.rs @@ -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 { + 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 { fn to_rust_code(self) -> TokenStream { let Enum { diff --git a/mir_rust/src/lib.rs b/mir_rust/src/lib.rs index 0cd6eb4..8a5ffec 100644 --- a/mir_rust/src/lib.rs +++ b/mir_rust/src/lib.rs @@ -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 { + 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::() { + quote! { , #d } + } else { + return TokenStream::new(); + } + }) + .collect() +} #[derive(Debug, Clone)] pub struct RustExtra {