From a877fdc19e060b5d838010f2c73892466623e4f4 Mon Sep 17 00:00:00 2001 From: Kurt Wolf Date: Mon, 16 Sep 2024 19:26:46 -0700 Subject: [PATCH] checkpoint --- codegen_rust/src/client.rs | 12 ++++++------ hir/src/lib.rs | 14 +++----------- libninja/tests/regression/link_token_create.rs | 1 - mir_rust/src/class.rs | 18 ++++++++++++------ mir_rust/src/enum.rs | 15 +++++++++++---- mir_rust/src/lib.rs | 2 +- mir_rust/src/record.rs | 17 +++++++++++++++++ mir_rust/src/ty.rs | 13 +++++++++---- 8 files changed, 59 insertions(+), 33 deletions(-) diff --git a/codegen_rust/src/client.rs b/codegen_rust/src/client.rs index 07f7ece..5762c13 100644 --- a/codegen_rust/src/client.rs +++ b/codegen_rust/src/client.rs @@ -148,7 +148,7 @@ pub fn struct_Client(spec: &HirSpec, opt: &Config) -> Class { let mut instance_fields = vec![Field { name: Ident::new("client"), - ty: quote!(Cow<'static, httpclient::Client>), + ty: quote!(Cow<'static, Client>), ..Field::default() }]; if spec.has_security() { @@ -455,10 +455,10 @@ pub fn impl_Authentication(spec: &HirSpec, opt: &Config) -> TokenStream { fn static_shared_http_client(spec: &HirSpec, opt: &Config) -> TokenStream { let url = server_url(spec, opt); quote! { - static SHARED_HTTPCLIENT: OnceLock = OnceLock::new(); + static SHARED_HTTPCLIENT: OnceLock = OnceLock::new(); - pub fn default_http_client() -> httpclient::Client { - httpclient::Client::new() + pub fn default_http_client() -> Client { + Client::new() .base_url(#url) } @@ -471,11 +471,11 @@ fn static_shared_http_client(spec: &HirSpec, opt: &Config) -> TokenStream { /// .with_middleware(..) /// ); /// ``` - pub fn init_http_client(init: httpclient::Client) { + pub fn init_http_client(init: Client) { let _ = SHARED_HTTPCLIENT.set(init); } - fn shared_http_client() -> Cow<'static, httpclient::Client> { + fn shared_http_client() -> Cow<'static, Client> { Cow::Borrowed(SHARED_HTTPCLIENT.get_or_init(default_http_client)) } } diff --git a/hir/src/lib.rs b/hir/src/lib.rs index e29b70b..8c82fda 100644 --- a/hir/src/lib.rs +++ b/hir/src/lib.rs @@ -298,15 +298,9 @@ pub enum ServerStrategy { impl ServerStrategy { pub fn env_var_for_strategy(&self, service_name: &str) -> Option { match self { - ServerStrategy::BaseUrl => Some(format!( - "{}_BASE_URL", - service_name.to_case(Case::ScreamingSnake) - )), + ServerStrategy::BaseUrl => Some(format!("{}_BASE_URL", service_name.to_case(Case::ScreamingSnake))), ServerStrategy::Single(_) => None, - ServerStrategy::Env => Some(format!( - "{}_ENV", - service_name.to_case(Case::ScreamingSnake) - )), + ServerStrategy::Env => Some(format!("{}_ENV", service_name.to_case(Case::ScreamingSnake))), } } } @@ -372,9 +366,7 @@ impl HirSpec { } pub fn has_basic_auth(&self) -> bool { - self.security - .iter() - .any(|s| matches!(s, AuthStrategy::Token(_))) + self.security.iter().any(|s| matches!(s, AuthStrategy::Token(_))) } pub fn oauth2_auth(&self) -> Option<&Oauth2Auth> { diff --git a/libninja/tests/regression/link_token_create.rs b/libninja/tests/regression/link_token_create.rs index 0245aa8..8358f36 100644 --- a/libninja/tests/regression/link_token_create.rs +++ b/libninja/tests/regression/link_token_create.rs @@ -1,5 +1,4 @@ use hir::HirSpec; -use mir_rust::class::implements_default; use openapiv3::{OpenAPI, Schema}; use serde_yaml::from_str; diff --git a/mir_rust/src/class.rs b/mir_rust/src/class.rs index 7bc2356..793911e 100644 --- a/mir_rust/src/class.rs +++ b/mir_rust/src/class.rs @@ -1,4 +1,4 @@ -use crate::{derives_to_tokens, ToRustCode, ToRustIdent, ToRustType}; +use crate::{derives_to_tokens, CanDerive, ToRustCode, ToRustIdent, ToRustType}; use hir::{Config, HirField, HirSpec, Struct}; use mir::{ Class, DateSerialization, DecimalSerialization, Field, Function, Ident, IntegerSerialization, Item, Ty, Visibility, @@ -82,10 +82,6 @@ impl ToRustCode for Field { } } -pub fn implements_default(schema: &Struct, spec: &HirSpec) -> bool { - schema.fields.iter().all(|(_, f)| f.ty.implements_default(spec)) -} - fn field_attributes(f: &HirField, name: &str, config: &Config) -> Vec { let mut attributes = Vec::new(); let rust_ident = name.to_rust_ident(); @@ -212,7 +208,7 @@ fn ref_target(s: &Struct) -> Option { } pub fn make_class(s: &Struct, config: &Config, spec: &HirSpec) -> Class { - let default = implements_default(s, spec).then(|| { + let default = s.implements_default(spec).then(|| { quote! { , Default } }); let derives = derives_to_tokens(&config.derives); @@ -264,3 +260,13 @@ pub fn make_class(s: &Struct, config: &Config, spec: &HirSpec) -> Class bool { + self.fields.iter().all(|(_, f)| f.ty.implements_default(spec)) + } + + fn implements_dummy(&self, spec: &HirSpec) -> bool { + self.fields.iter().all(|(_, f)| f.ty.implements_dummy(spec)) + } +} diff --git a/mir_rust/src/enum.rs b/mir_rust/src/enum.rs index 891a9b2..6d763a4 100644 --- a/mir_rust/src/enum.rs +++ b/mir_rust/src/enum.rs @@ -52,16 +52,23 @@ impl ToRustCode for Enum { let vis = vis.to_rust_code(); let doc = doc.to_rust_code(); let variants = variants.into_iter().map(|v| v.to_rust_code()); - let methods = methods.into_iter().map(|m| m.to_rust_code()); + let methods = if methods.is_empty() { + TokenStream::new() + } else { + let methods = methods.into_iter().map(|m| m.to_rust_code()); + quote! { + impl #name { + #(#methods)* + } + } + }; quote! { #doc #(#attributes)* #vis enum #name { #(#variants),* } - impl #name { - #(#methods)* - } + #methods } } } diff --git a/mir_rust/src/lib.rs b/mir_rust/src/lib.rs index 66b3f02..22f9243 100644 --- a/mir_rust/src/lib.rs +++ b/mir_rust/src/lib.rs @@ -23,7 +23,7 @@ pub use example::to_rust_example_value; pub use ident::ToRustIdent; pub use r#enum::make_enum; pub use record::make_item; -pub use ty::ToRustType; +pub use ty::{CanDerive, ToRustType}; pub use util::*; pub fn serde_rename2(value: &str, ident: &Ident) -> Option { diff --git a/mir_rust/src/record.rs b/mir_rust/src/record.rs index de1cac7..0b411a1 100644 --- a/mir_rust/src/record.rs +++ b/mir_rust/src/record.rs @@ -1,3 +1,4 @@ +use crate::ty::CanDerive; use crate::{derives_to_tokens, make_class, make_enum, ToRustIdent, ToRustType}; use hir::{Config, HirField, HirSpec, NewType, Record}; use mir::Item; @@ -42,6 +43,22 @@ pub fn make_item(record: &Record, spec: &HirSpec, cfg: &Config) -> Item bool { + match self { + Record::Enum(_) => false, + _ => self.fields().all(|f| f.ty.implements_default(spec)), + } + } + + fn implements_dummy(&self, spec: &HirSpec) -> bool { + match self { + Record::Enum(_) => false, + _ => self.fields().all(|f| f.ty.implements_default(spec)), + } + } +} + #[cfg(test)] mod tests { use super::*; diff --git a/mir_rust/src/ty.rs b/mir_rust/src/ty.rs index f73f5ab..86aa7ac 100644 --- a/mir_rust/src/ty.rs +++ b/mir_rust/src/ty.rs @@ -9,6 +9,9 @@ pub trait ToRustType { fn to_rust_type(&self) -> TokenStream; fn to_reference_type(&self, specifier: TokenStream) -> TokenStream; fn is_reference_type(&self) -> bool; +} + +pub trait CanDerive { fn implements_default(&self, spec: &HirSpec) -> bool; fn implements_dummy(&self, spec: &HirSpec) -> bool; } @@ -72,7 +75,9 @@ impl ToRustType for Ty { _ => false, } } +} +impl CanDerive for Ty { fn implements_default(&self, spec: &HirSpec) -> bool { match self { Ty::String => true, @@ -80,10 +85,10 @@ impl ToRustType for Ty { Ty::Float => true, Ty::Boolean => true, Ty::Array(_) => true, - Ty::Model(name) => { - let model = spec.get_record(name.as_str()).expect("Model not found"); - model.fields().all(|f| f.ty.implements_default(spec)) - } + Ty::Model(name) => spec + .get_record(name.as_str()) + .expect("Model not found") + .implements_default(spec), Ty::Unit => true, Ty::Any(_) => true, Ty::Date { .. } => true,