Skip to content

Commit

Permalink
better docs; fix #18 authentication conditionals
Browse files Browse the repository at this point in the history
  • Loading branch information
kurtbuilds committed Dec 14, 2023
1 parent c737728 commit a4f5d5a
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 12 deletions.
6 changes: 3 additions & 3 deletions libninja/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -22,15 +22,15 @@ serde_json = "1.0.100"
serde_yaml = "0.9.22"
syn = "2.0"
tokio = { version = "1.29.1", features = ["full"] }
openapiv3-extended = { version = "3.1" , features = ["v2"] }
openapiv3-extended = { version = "3.1", features = ["v2"] }
convert_case = "0.6.0"
prettyplease = "0.2"
clap = { version = "4.4.10", features = ["derive"] }
tera = "1.19.0"
include_dir = "0.7.3"
regex = "1.9.0"
indoc = "2.0.2"
cargo_toml = "0.17.1"
cargo_toml = { git = "https://github.com/kurtbuilds/cargo_toml" }
toml = "0.8.8"
topo_sort = "0.4.0"
url = "2.4.0"
Expand All @@ -44,7 +44,7 @@ ln-macro = { path = "../macro" }
ln-core = { path = "../core" }
libninja_mir = { path = "../mir" }
libninja_hir = { path = "../hir" }
libninja_commercial = { path = "../commercial" , optional = true }
libninja_commercial = { path = "../commercial", optional = true }
ignore = "0.4.21"
text_io = "0.1.12"

Expand Down
9 changes: 8 additions & 1 deletion libninja/src/rust.rs
Original file line number Diff line number Diff line change
Expand Up @@ -267,6 +267,12 @@ fn write_request_module(spec: &HirSpec, opts: &PackageConfig) -> Result<()> {
let mut imports = vec![];
fs::create_dir_all(src_path.join("request"))?;
let mut modules = vec![];

let authenticate = spec.has_security()
.then(|| quote! {
r = self.client.authenticate(r);
}).unwrap_or_default();

for operation in &spec.operations {
let fname = operation.file_name();
let request_structs = build_request_struct(operation, spec, &opts);
Expand All @@ -284,6 +290,7 @@ fn write_request_module(spec: &HirSpec, opts: &PackageConfig) -> Result<()> {
let builder_methods = builder_methods
.into_iter()
.map(|s| codegen_function(s, quote! { mut self , }));

let file = quote! {
use crate::#client_name;
#(#request_structs)*
Expand All @@ -301,7 +308,7 @@ fn write_request_module(spec: &HirSpec, opts: &PackageConfig) -> Result<()> {
let url = #url;
let mut r = self.client.client.#method(url);
r = r.set_query(self.params);
r = self.client.authenticate(r);
#authenticate
let res = r.await?;
res.json().map_err(Into::into)
})
Expand Down
12 changes: 6 additions & 6 deletions libninja/src/rust/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -193,8 +193,8 @@ pub fn authenticate_variant(
}
}

pub fn build_Client_authenticate(mir_spec: &HirSpec, opt: &PackageConfig) -> TokenStream {
let authenticate_variant = mir_spec.security
pub fn build_Client_authenticate(spec: &HirSpec, opt: &PackageConfig) -> TokenStream {
let authenticate_variant = spec.security
.iter()
.map(|req| authenticate_variant(req, opt))
.collect::<Vec<_>>();
Expand Down Expand Up @@ -235,14 +235,14 @@ fn build_new_fn(security: bool, opt: &PackageConfig) -> TokenStream {
}
}

pub fn impl_Client(mir_spec: &HirSpec, opt: &PackageConfig) -> TokenStream {
pub fn impl_Client(spec: &HirSpec, opt: &PackageConfig) -> TokenStream {
let client_struct_name = opt.client_name().to_rust_struct();
let path_fns = impl_ServiceClient_paths(mir_spec);
let path_fns = impl_ServiceClient_paths(spec);

let security = mir_spec.has_security();
let security = spec.has_security();
let new_fn = build_new_fn(security, opt);
let authenticate = if security {
build_Client_authenticate(mir_spec, opt)
build_Client_authenticate(spec, opt)
} else {
TokenStream::new()
};
Expand Down
7 changes: 5 additions & 2 deletions libninja/src/rust/request.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,9 +248,12 @@ pub fn build_request_struct(
// let mut instance_methods = vec![build_send_function(operation, spec)];
// let mut_self_instance_methods = build_request_struct_builder_methods(operation);

let doc = doc("Create this with the associated client method.
let fn_name = operation.name.to_rust_ident().0;
let response = operation.ret.to_rust_type().to_string().replace(" ", "");
let client = opt.client_name().to_rust_struct().to_string().replace(" ", "");
let doc = Some(Doc(format!(r#"You should use this struct via [`{client}::{fn_name}`].
That method takes required values as arguments. Set optional values using builder methods on this struct.");
On request success, this will return a [`{response}`]."#, )));
let mut result = vec![Class {
name: operation.request_struct_name().to_rust_struct(),
doc,
Expand Down

0 comments on commit a4f5d5a

Please sign in to comment.