Skip to content

Commit

Permalink
wip
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtbuilds committed Dec 17, 2023
1 parent d10c5a4 commit 1d4879d
Show file tree
Hide file tree
Showing 11 changed files with 543 additions and 540 deletions.
984 changes: 478 additions & 506 deletions Cargo.lock

Large diffs are not rendered by default.

9 changes: 6 additions & 3 deletions core/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,9 +1,11 @@
[package]
name = "ln-core"
name = "libninja_core"
version = "0.1.0"
edition = "2021"

# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[lib]
name = "ln_core"

[dependencies]
anyhow = "1.0.71"
Expand All @@ -14,13 +16,14 @@ serde = { version = "1.0.166", features = ["derive"] }
quote = "1.0.29"
serde_json = "1.0.100"
proc-macro2 = "1.0.63"
tracing-ez = "0.3.0"
indexmap = "2.0"
syn = "2.0"
libninja_mir = { path = "../mir" }
include_dir = "0.7.3"
tera = "1.19.0"
libninja_hir = { path = "../hir" }
tracing = "0.1.40"
tracing-ez = "0.3.0"

[dev-dependencies]
serde_yaml = "0.9.25"
serde_yaml = "0.9.25"
28 changes: 19 additions & 9 deletions core/src/extractor.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,13 +4,13 @@ use anyhow::{anyhow, Result};
use convert_case::{Case, Casing};
use openapiv3::{OpenAPI, ReferenceOr, Schema};
use openapiv3 as oa;
use tracing_ez::{span, warn};

use ::hir::{AuthLocation, AuthorizationParameter, AuthorizationStrategy, DocFormat, HirSpec, Language, Location, Operation, Record, Ty, Parameter, Doc};
pub use record::*;
pub use resolution::{schema_ref_to_ty, schema_ref_to_ty_already_resolved, schema_to_ty};
pub use resolution::*;
use mir::NewType;
use tracing_ez::{warn, debug, span};

mod resolution;
mod record;
Expand All @@ -35,7 +35,7 @@ pub fn extract_spec(spec: &OpenAPI) -> Result<HirSpec> {
Ok(s)
}

pub fn is_optional(name: &str, param: &oa::Schema, parent: &Schema) -> bool {
pub fn is_optional(name: &str, param: &Schema, parent: &Schema) -> bool {
param.schema_data.nullable || !parent.required(name)
}

Expand All @@ -57,6 +57,7 @@ pub fn extract_request_schema<'a>(

pub fn extract_param(param: &ReferenceOr<oa::Parameter>, spec: &OpenAPI) -> Result<Parameter> {
span!("extract_param", param = ?param);

let param = param.resolve(spec)?;
let data = param.parameter_data_ref();
let param_schema_ref = data
Expand Down Expand Up @@ -308,7 +309,18 @@ fn remove_unused(spec: &mut HirSpec) {
};
}
}
spec.schemas.retain(|name, _| used.contains(name) || name.ends_with("Webhook"));
let count_before = spec.schemas.len();
spec.schemas.retain(|name, _| {
let needed = used.contains(name) || name.ends_with("Webhook");
if !needed {
debug!("Removing unused schema: {}", name);
}
needed
});
let count_after = spec.schemas.len();
if count_before == count_after {
debug!("No schemas removed in removed_unused");
}
}

fn sanitize_spec(spec: &mut HirSpec) {
Expand All @@ -323,9 +335,7 @@ fn sanitize_spec(spec: &mut HirSpec) {
.collect();
for record in spec.schemas.values_mut() {
for field in record.fields_mut() {
let Ty::Model(name) = &field.ty else {
continue;
};
let Ty::Model(name) = &field.ty else { continue; };
let Some(rename_to) = optional_short_circuit.get(name) else {
continue;
};
Expand Down Expand Up @@ -450,11 +460,11 @@ fn get_name(schema_ref: oa::SchemaReference) -> String {

/// Add the models for operations that have structs for their required params.
/// E.g. linkTokenCreate has >3 required params, so it has a struct.
pub fn add_operation_models(sourcegen: Language, mut spec: HirSpec) -> Result<HirSpec> {
pub fn add_operation_models(lang: Language, mut spec: HirSpec) -> Result<HirSpec> {
let mut new_schemas = vec![];
for op in &spec.operations {
if op.use_required_struct(sourcegen) {
new_schemas.push((op.required_struct_name(), Record::Struct(op.required_struct(sourcegen))));
if op.use_required_struct(lang) {
new_schemas.push((op.required_struct_name(), Record::Struct(op.required_struct(lang))));
}
}
spec.schemas.extend(new_schemas);
Expand Down
11 changes: 9 additions & 2 deletions core/src/extractor/record.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use indexmap::IndexMap;
/// Records are the "model"s of the MIR world. model is a crazy overloaded word though.
use openapiv3::{ObjectType, OpenAPI, ReferenceOr, Schema, SchemaData, SchemaKind, SchemaReference, StringType, Type};
use tracing_ez::warn;
use tracing::warn;

use hir::{Doc, HirField, Record, StrEnum, Struct, NewType};

Expand Down Expand Up @@ -147,12 +147,19 @@ fn create_record_from_all_of(name: &str, all_of: &[ReferenceOr<Schema>], schema_
pub fn extract_records(spec: &OpenAPI) -> Result<BTreeMap<String, Record>> {
let mut result: BTreeMap<String, Record> = BTreeMap::new();
let mut schema_lookup = HashMap::new();

spec.add_child_schemas(&mut schema_lookup);
for (name, schema) in schema_lookup {
for (mut name, schema) in schema_lookup {
let rec = create_record(&name, schema, spec);
let name = rec.name().to_string();
result.insert(name, rec);
}

for (name, schema_ref) in spec.schemas() {
let Some(reference) = schema_ref.as_ref_str() else { continue; };
result.insert(name.clone(), Record::TypeAlias(name.clone(), create_field(&schema_ref, spec)));
}

Ok(result)
}

Expand Down
2 changes: 1 addition & 1 deletion core/src/extractor/resolution.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
use openapiv3::{ArrayType, OpenAPI, ReferenceOr, Schema, SchemaKind, SchemaReference};
use tracing_ez::warn;
use tracing::warn;

use hir::Ty;

Expand Down
1 change: 0 additions & 1 deletion hir/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -317,7 +317,6 @@ pub struct HirSpec {
pub api_docs_url: Option<String>,
}


pub enum ServerStrategy {
/// No servers were provided, so we pass a base URL
BaseUrl,
Expand Down
7 changes: 4 additions & 3 deletions libninja/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,18 +35,19 @@ toml = "0.8.8"
topo_sort = "0.4.0"
url = "2.4.0"
http = "0.2.9"
tracing-ez = "0.3.0"
strum = "0.25"
actix-web = { version = "4.3.1", optional = true }
semver = "1.0.17"
indexmap = "2.0"
ln-macro = { path = "../macro" }
ln-core = { path = "../core" }
libninja_macro = { path = "../macro" }
libninja_core = { path = "../core" }
libninja_mir = { path = "../mir" }
libninja_hir = { path = "../hir" }
libninja_commercial = { path = "../commercial", optional = true }
ignore = "0.4.21"
text_io = "0.1.12"
tracing-subscriber = "0.3.18"
tracing = "0.1.40"

[dev-dependencies]
env_logger = "0.10.0"
Expand Down
24 changes: 16 additions & 8 deletions libninja/src/bin/libninja.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,10 @@ use ln_core::{OutputConfig, PackageConfig};
use hir::Language;
use libninja::rust::generate_rust_library;
use std::path::Path;
use tracing::Level;
use libninja::command::*;
use tracing_subscriber::util::SubscriberInitExt;
use tracing_subscriber::layer::SubscriberExt;

fn warn_if_not_found(command: &str) {
if std::process::Command::new(command)
Expand All @@ -30,12 +33,6 @@ struct Cli {
verbose: bool,
}

#[derive(Args, Debug)]
pub struct Foobar {
pub foo: String,
pub bar: String,
}

#[derive(Subcommand, Debug)]
pub enum Command {
Gen(Generate),
Expand All @@ -50,8 +47,19 @@ pub enum Command {

fn main() -> Result<()> {
let cli = Cli::parse();

tracing_ez::set_global_default_stderr();
let level = if cli.verbose { Level::DEBUG } else { Level::INFO };
tracing_subscriber::registry()
.with(tracing_subscriber::fmt::layer()
.without_time()
)
.with(tracing_subscriber::filter::Targets::new()
.with_target(env!("CARGO_BIN_NAME"), level)
.with_target("libninja_mir", level)
.with_target("libninja_hir", level)
.with_target("ln_core", level)
.with_target("ln_macro", level)
)
.init();

match cli.command {
Command::Gen(generate) => {
Expand Down
3 changes: 2 additions & 1 deletion libninja/src/command/generate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ use std::process::Output;
use anyhow::Result;
use clap::{Args, ValueEnum};
use convert_case::{Case, Casing};
use tracing::debug;
use crate::{OutputConfig, Language, PackageConfig, read_spec, generate_library};
use ln_core::{ConfigFlags};

Expand Down Expand Up @@ -36,7 +37,7 @@ pub struct Generate {
#[clap(short, long)]
output_dir: Option<String>,

#[clap(short, long)]
#[clap(long)]
version: Option<String>,

/// config options
Expand Down
11 changes: 6 additions & 5 deletions libninja/src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -120,7 +120,6 @@ pub fn generate_rust_library(spec: OpenAPI, opts: OutputConfig) -> Result<()> {

// Prepare the HIR Spec.
let spec = extract_spec(&spec)?;
let spec = add_operation_models(opts.language, spec)?;
let extras = calculate_extras(&spec);

// if src doesn't exist that's fine
Expand Down Expand Up @@ -157,6 +156,8 @@ pub fn generate_rust_library(spec: OpenAPI, opts: OutputConfig) -> Result<()> {
write_lib_rs(&spec, &extras, &opts)?;
write_serde_module_if_needed(&extras, &opts.dest)?;

let spec = add_operation_models(opts.language, spec)?;

if build_examples {
write_examples(&spec, &opts)?;
}
Expand All @@ -173,15 +174,15 @@ pub fn generate_rust_library(spec: OpenAPI, opts: OutputConfig) -> Result<()> {
Ok(())
}

fn write_model_module(mir_spec: &HirSpec, opts: &PackageConfig) -> Result<()> {
fn write_model_module(spec: &HirSpec, opts: &PackageConfig) -> Result<()> {
let config = &opts.config;
let src_path = opts.dest.join("src");

let model_rs = generate_model_rs(mir_spec, config);
let model_rs = generate_model_rs(spec, config);
write_rust_file_to_path(&src_path.join("model.rs"), model_rs)?;
fs::create_dir_all(src_path.join("model"))?;
for (name, record) in &mir_spec.schemas {
let file = generate_single_model_file(name, record, mir_spec, config);
for (name, record) in &spec.schemas {
let file = generate_single_model_file(name, record, spec, config);
let name = sanitize_filename(name);
write_rust_file_to_path(&src_path.join("model").join(name).with_extension("rs"), file)?;
}
Expand Down
3 changes: 2 additions & 1 deletion macro/Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
[package]
name = "ln-macro"
name = "libninja_macro"
version = "0.1.0"
authors = [
]
Expand All @@ -8,6 +8,7 @@ license = "MIT"
edition = "2021"

[lib]
name = "ln_macro"
proc_macro = true

[dependencies]
Expand Down

0 comments on commit 1d4879d

Please sign in to comment.