Skip to content

Commit

Permalink
checkpoint
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtbuilds committed Sep 17, 2024
1 parent 0259821 commit a877fdc
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 33 deletions.
12 changes: 6 additions & 6 deletions codegen_rust/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,7 +148,7 @@ pub fn struct_Client(spec: &HirSpec, opt: &Config) -> Class<TokenStream> {

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() {
Expand Down Expand Up @@ -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<httpclient::Client> = OnceLock::new();
static SHARED_HTTPCLIENT: OnceLock<Client> = OnceLock::new();

pub fn default_http_client() -> httpclient::Client {
httpclient::Client::new()
pub fn default_http_client() -> Client {
Client::new()
.base_url(#url)
}

Expand All @@ -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))
}
}
Expand Down
14 changes: 3 additions & 11 deletions hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,15 +298,9 @@ pub enum ServerStrategy {
impl ServerStrategy {
pub fn env_var_for_strategy(&self, service_name: &str) -> Option<String> {
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))),
}
}
}
Expand Down Expand Up @@ -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> {
Expand Down
1 change: 0 additions & 1 deletion libninja/tests/regression/link_token_create.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use hir::HirSpec;
use mir_rust::class::implements_default;
use openapiv3::{OpenAPI, Schema};
use serde_yaml::from_str;

Expand Down
18 changes: 12 additions & 6 deletions mir_rust/src/class.rs
Original file line number Diff line number Diff line change
@@ -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,
Expand Down Expand Up @@ -82,10 +82,6 @@ impl ToRustCode for Field<TokenStream> {
}
}

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<TokenStream> {
let mut attributes = Vec::new();
let rust_ident = name.to_rust_ident();
Expand Down Expand Up @@ -212,7 +208,7 @@ fn ref_target(s: &Struct) -> Option<RefTarget> {
}

pub fn make_class(s: &Struct, config: &Config, spec: &HirSpec) -> Class<TokenStream> {
let default = implements_default(s, spec).then(|| {
let default = s.implements_default(spec).then(|| {
quote! { , Default }
});
let derives = derives_to_tokens(&config.derives);
Expand Down Expand Up @@ -264,3 +260,13 @@ pub fn make_class(s: &Struct, config: &Config, spec: &HirSpec) -> Class<TokenStr
imports: vec![],
}
}

impl CanDerive for Struct {
fn implements_default(&self, spec: &HirSpec) -> 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))
}
}
15 changes: 11 additions & 4 deletions mir_rust/src/enum.rs
Original file line number Diff line number Diff line change
Expand Up @@ -52,16 +52,23 @@ impl ToRustCode for Enum<TokenStream> {
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
}
}
}
Expand Down
2 changes: 1 addition & 1 deletion mir_rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<TokenStream> {
Expand Down
17 changes: 17 additions & 0 deletions mir_rust/src/record.rs
Original file line number Diff line number Diff line change
@@ -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;
Expand Down Expand Up @@ -42,6 +43,22 @@ pub fn make_item(record: &Record, spec: &HirSpec, cfg: &Config) -> Item<TokenStr
}
}

impl CanDerive for Record {
fn implements_default(&self, spec: &HirSpec) -> 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::*;
Expand Down
13 changes: 9 additions & 4 deletions mir_rust/src/ty.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -72,18 +75,20 @@ impl ToRustType for Ty {
_ => false,
}
}
}

impl CanDerive for Ty {
fn implements_default(&self, spec: &HirSpec) -> bool {
match self {
Ty::String => true,
Ty::Integer { .. } => true,
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,
Expand Down

0 comments on commit a877fdc

Please sign in to comment.