Skip to content

Commit

Permalink
Add default-enabled feature on all API operations.
Browse files Browse the repository at this point in the history
Crates that don't need the operations can disable the feature to save on
compile time and resources.

Ref kube-rs/kube#62
  • Loading branch information
Arnavion committed Aug 24, 2019
1 parent 41141b3 commit 5936200
Show file tree
Hide file tree
Showing 1,031 changed files with 35,076 additions and 20,009 deletions.
4 changes: 4 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,10 @@ serde-value = "0.6.x"
url = "1.7.x"

[features]
default = ["api"]

api = [] # Enables API operation functions and response types. If disabled, only the resource types will be exported.

# Each feature corresponds to a supported version of Kubernetes
v1_8 = []
v1_9 = []
Expand Down
13 changes: 13 additions & 0 deletions k8s-openapi-codegen-common/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1590,6 +1590,10 @@ pub fn write_operation(
}
}

if mod_root.is_some() {
writeln!(out, r#"{}#[cfg(feature = "api")]"#, indent)?;
}

writeln!(out, "{}{}fn {}(", indent, vis, operation_fn_name)?;
for (parameter_name, parameter_type, _) in &required_parameters {
writeln!(out, "{} {}: {},", indent, parameter_name, parameter_type)?;
Expand Down Expand Up @@ -1787,6 +1791,9 @@ pub fn write_operation(
writeln!(out, "/// Optional parameters of [`{}`]", operation_fn_name)?;
}

if mod_root.is_some() {
writeln!(out, r#"#[cfg(feature = "api")]"#)?;
}
writeln!(out, "#[derive(Clone, Copy, Debug, Default)]")?;
write!(out, "{}struct {}", vis, operation_optional_parameters_name)?;
if any_optional_fields_have_lifetimes {
Expand Down Expand Up @@ -1825,6 +1832,9 @@ pub fn write_operation(
operation_result_name, operation_fn_name)?;
}

if mod_root.is_some() {
writeln!(out, r#"#[cfg(feature = "api")]"#)?;
}
writeln!(out, "#[derive(Debug)]")?;
writeln!(out, "{}enum {} {{", vis, operation_result_name)?;

Expand Down Expand Up @@ -1874,6 +1884,9 @@ pub fn write_operation(
writeln!(out, "}}")?;
writeln!(out)?;

if mod_root.is_some() {
writeln!(out, r#"#[cfg(feature = "api")]"#)?;
}
writeln!(out, "impl {}::Response for {} {{", crate_root, operation_result_name)?;
writeln!(out, " fn try_from_parts(status_code: http::StatusCode, buf: &[u8]) -> Result<(Self, usize), {}::ResponseError> {{", crate_root)?;

Expand Down
32 changes: 18 additions & 14 deletions k8s-openapi-codegen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -149,7 +149,7 @@ fn run(supported_version: supported_version::SupportedVersion, out_dir_base: &st
for definition_path in spec.definitions.keys() {
log::trace!("Working on {} ...", definition_path);

let parent_mod_rs_file = std::cell::RefCell::new(None);
let parent_mod_rs_file_and_mod_name = std::cell::RefCell::new(None);

let run_result = k8s_openapi_codegen_common::run(
&spec.definitions,
Expand Down Expand Up @@ -200,37 +200,41 @@ fn run(supported_version: supported_version::SupportedVersion, out_dir_base: &st
let mut parent_mod_rs = std::io::BufWriter::new(std::fs::OpenOptions::new().append(true).create(true).open(current.join("mod.rs"))?);
writeln!(parent_mod_rs)?;
writeln!(parent_mod_rs, "mod {};", mod_name)?;
writeln!(parent_mod_rs, "pub use self::{}::{{", mod_name)?;
writeln!(parent_mod_rs, " {},", type_name)?;

parent_mod_rs_file.replace(Some(parent_mod_rs));
writeln!(parent_mod_rs, "pub use self::{}::{};", mod_name, type_name)?;

let file_name = current.join(&*mod_name).with_extension("rs");
let file = std::io::BufWriter::new(std::fs::File::create(file_name)?);

parent_mod_rs_file_and_mod_name.replace(Some((parent_mod_rs, mod_name)));

Ok(file)
},
|operation_optional_parameters_name, operation_result_name| {
let mut parent_mod_rs = parent_mod_rs_file.borrow_mut();
let parent_mod_rs = parent_mod_rs.as_mut().unwrap();
let mut parent_mod_rs_file_and_mod_name = parent_mod_rs_file_and_mod_name.borrow_mut();
let (parent_mod_rs, mod_name) = parent_mod_rs_file_and_mod_name.as_mut().unwrap();
match (operation_optional_parameters_name, operation_result_name) {
(Some(operation_optional_parameters_name), Some(operation_result_name)) =>
writeln!(parent_mod_rs, " {}, {},", operation_optional_parameters_name, operation_result_name)?,
writeln!(
parent_mod_rs,
r#"#[cfg(feature = "api")] pub use self::{}::{{{}, {}}};"#,
mod_name, operation_optional_parameters_name, operation_result_name)?,
(Some(operation_optional_parameters_name), None) =>
writeln!(parent_mod_rs, " {},", operation_optional_parameters_name)?,
writeln!(
parent_mod_rs,
r#"#[cfg(feature = "api")] pub use self::{}::{};"#,
mod_name, operation_optional_parameters_name)?,
(None, Some(operation_result_name)) =>
writeln!(parent_mod_rs, " {},", operation_result_name)?,
writeln!(
parent_mod_rs,
r#"#[cfg(feature = "api")] pub use self::{}::{};"#,
mod_name, operation_result_name)?,
(None, None) =>
(),
}
Ok(())
},
)?;

let mut parent_mod_rs = parent_mod_rs_file.borrow_mut();
let parent_mod_rs = parent_mod_rs.as_mut().unwrap();
writeln!(parent_mod_rs, "}};")?;

num_generated_structs += run_result.num_generated_structs;
num_generated_type_aliases += run_result.num_generated_type_aliases;
num_generated_apis += run_result.num_generated_apis;
Expand Down
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,6 +266,17 @@
//! }
//! ```
//!
//!
//! # Crate features
//!
//! - As mentioned above, enabling one of the `v1_*` features selects which version of the Kubernetes API server this crate should target.
//!
//! - If the `api` feature is disabled, the library will only contain the resource types like [`api::core::v1::Pod`] and not the associated operation functions
//! like [`api::core::v1::Pod::read_namespaced_pod`]. The corresponding `Response` and `Optional` types will also not be accessible.
//!
//! This feature is enabled by default, but can be disabled if your crate does not need the operation functions to save on compile time and resources.
//!
//!
//! # Custom resource definitions
//!
//! The [`k8s-openapi-derive` crate](https://crates.io/crates/k8s-openapi-derive) provides a custom derive for generating clientsets
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ impl InitializerConfiguration {
/// * `optional`
///
/// Optional parameters. Use `Default::default()` to not pass any.
#[cfg(feature = "api")]
pub fn create_initializer_configuration(
body: &crate::v1_10::api::admissionregistration::v1alpha1::InitializerConfiguration,
optional: CreateInitializerConfigurationOptional<'_>,
Expand All @@ -51,13 +52,15 @@ impl InitializerConfiguration {
}

/// Optional parameters of [`InitializerConfiguration::create_initializer_configuration`]
#[cfg(feature = "api")]
#[derive(Clone, Copy, Debug, Default)]
pub struct CreateInitializerConfigurationOptional<'a> {
/// If 'true', then the output is pretty printed.
pub pretty: Option<&'a str>,
}

/// Use `<CreateInitializerConfigurationResponse as Response>::try_from_parts` to parse the HTTP response body of [`InitializerConfiguration::create_initializer_configuration`]
#[cfg(feature = "api")]
#[derive(Debug)]
pub enum CreateInitializerConfigurationResponse {
Ok(crate::v1_10::api::admissionregistration::v1alpha1::InitializerConfiguration),
Expand All @@ -66,6 +69,7 @@ pub enum CreateInitializerConfigurationResponse {
Other(Result<Option<serde_json::Value>, serde_json::Error>),
}

#[cfg(feature = "api")]
impl crate::Response for CreateInitializerConfigurationResponse {
fn try_from_parts(status_code: http::StatusCode, buf: &[u8]) -> Result<(Self, usize), crate::ResponseError> {
match status_code {
Expand Down Expand Up @@ -127,6 +131,7 @@ impl InitializerConfiguration {
/// * `list_optional`
///
/// List options. Use `Default::default()` to not pass any.
#[cfg(feature = "api")]
pub fn delete_collection_initializer_configuration(
delete_optional: crate::v1_10::DeleteOptional<'_>,
list_optional: crate::v1_10::ListOptional<'_>,
Expand All @@ -147,13 +152,15 @@ impl InitializerConfiguration {
}

/// Use `<DeleteCollectionInitializerConfigurationResponse as Response>::try_from_parts` to parse the HTTP response body of [`InitializerConfiguration::delete_collection_initializer_configuration`]
#[cfg(feature = "api")]
#[derive(Debug)]
pub enum DeleteCollectionInitializerConfigurationResponse {
OkStatus(crate::v1_10::apimachinery::pkg::apis::meta::v1::Status),
OkValue(crate::v1_10::api::admissionregistration::v1alpha1::InitializerConfigurationList),
Other(Result<Option<serde_json::Value>, serde_json::Error>),
}

#[cfg(feature = "api")]
impl crate::Response for DeleteCollectionInitializerConfigurationResponse {
fn try_from_parts(status_code: http::StatusCode, buf: &[u8]) -> Result<(Self, usize), crate::ResponseError> {
match status_code {
Expand Down Expand Up @@ -212,6 +219,7 @@ impl InitializerConfiguration {
/// * `optional`
///
/// Optional parameters. Use `Default::default()` to not pass any.
#[cfg(feature = "api")]
pub fn delete_initializer_configuration(
name: &str,
optional: crate::v1_10::DeleteOptional<'_>,
Expand All @@ -231,13 +239,15 @@ impl InitializerConfiguration {
}

/// Use `<DeleteInitializerConfigurationResponse as Response>::try_from_parts` to parse the HTTP response body of [`InitializerConfiguration::delete_initializer_configuration`]
#[cfg(feature = "api")]
#[derive(Debug)]
pub enum DeleteInitializerConfigurationResponse {
OkStatus(crate::v1_10::apimachinery::pkg::apis::meta::v1::Status),
OkValue(crate::v1_10::api::admissionregistration::v1alpha1::InitializerConfiguration),
Other(Result<Option<serde_json::Value>, serde_json::Error>),
}

#[cfg(feature = "api")]
impl crate::Response for DeleteInitializerConfigurationResponse {
fn try_from_parts(status_code: http::StatusCode, buf: &[u8]) -> Result<(Self, usize), crate::ResponseError> {
match status_code {
Expand Down Expand Up @@ -294,6 +304,7 @@ impl InitializerConfiguration {
/// * `optional`
///
/// Optional parameters. Use `Default::default()` to not pass any.
#[cfg(feature = "api")]
pub fn list_initializer_configuration(
optional: crate::v1_10::ListOptional<'_>,
) -> Result<(http::Request<Vec<u8>>, fn(http::StatusCode) -> crate::ResponseBody<ListInitializerConfigurationResponse>), crate::RequestError> {
Expand All @@ -312,12 +323,14 @@ impl InitializerConfiguration {
}

/// Use `<ListInitializerConfigurationResponse as Response>::try_from_parts` to parse the HTTP response body of [`InitializerConfiguration::list_initializer_configuration`]
#[cfg(feature = "api")]
#[derive(Debug)]
pub enum ListInitializerConfigurationResponse {
Ok(crate::v1_10::api::admissionregistration::v1alpha1::InitializerConfigurationList),
Other(Result<Option<serde_json::Value>, serde_json::Error>),
}

#[cfg(feature = "api")]
impl crate::Response for ListInitializerConfigurationResponse {
fn try_from_parts(status_code: http::StatusCode, buf: &[u8]) -> Result<(Self, usize), crate::ResponseError> {
match status_code {
Expand Down Expand Up @@ -365,6 +378,7 @@ impl InitializerConfiguration {
/// * `optional`
///
/// Optional parameters. Use `Default::default()` to not pass any.
#[cfg(feature = "api")]
pub fn patch_initializer_configuration(
name: &str,
body: &crate::v1_10::apimachinery::pkg::apis::meta::v1::Patch,
Expand Down Expand Up @@ -392,12 +406,14 @@ impl InitializerConfiguration {
}

/// Use `<PatchInitializerConfigurationResponse as Response>::try_from_parts` to parse the HTTP response body of [`InitializerConfiguration::patch_initializer_configuration`]
#[cfg(feature = "api")]
#[derive(Debug)]
pub enum PatchInitializerConfigurationResponse {
Ok(crate::v1_10::api::admissionregistration::v1alpha1::InitializerConfiguration),
Other(Result<Option<serde_json::Value>, serde_json::Error>),
}

#[cfg(feature = "api")]
impl crate::Response for PatchInitializerConfigurationResponse {
fn try_from_parts(status_code: http::StatusCode, buf: &[u8]) -> Result<(Self, usize), crate::ResponseError> {
match status_code {
Expand Down Expand Up @@ -443,6 +459,7 @@ impl InitializerConfiguration {
/// * `optional`
///
/// Optional parameters. Use `Default::default()` to not pass any.
#[cfg(feature = "api")]
pub fn read_initializer_configuration(
name: &str,
optional: ReadInitializerConfigurationOptional<'_>,
Expand Down Expand Up @@ -477,6 +494,7 @@ impl InitializerConfiguration {
}

/// Optional parameters of [`InitializerConfiguration::read_initializer_configuration`]
#[cfg(feature = "api")]
#[derive(Clone, Copy, Debug, Default)]
pub struct ReadInitializerConfigurationOptional<'a> {
/// Should the export be exact. Exact export maintains cluster-specific fields like 'Namespace'.
Expand All @@ -488,12 +506,14 @@ pub struct ReadInitializerConfigurationOptional<'a> {
}

/// Use `<ReadInitializerConfigurationResponse as Response>::try_from_parts` to parse the HTTP response body of [`InitializerConfiguration::read_initializer_configuration`]
#[cfg(feature = "api")]
#[derive(Debug)]
pub enum ReadInitializerConfigurationResponse {
Ok(crate::v1_10::api::admissionregistration::v1alpha1::InitializerConfiguration),
Other(Result<Option<serde_json::Value>, serde_json::Error>),
}

#[cfg(feature = "api")]
impl crate::Response for ReadInitializerConfigurationResponse {
fn try_from_parts(status_code: http::StatusCode, buf: &[u8]) -> Result<(Self, usize), crate::ResponseError> {
match status_code {
Expand Down Expand Up @@ -541,6 +561,7 @@ impl InitializerConfiguration {
/// * `optional`
///
/// Optional parameters. Use `Default::default()` to not pass any.
#[cfg(feature = "api")]
pub fn replace_initializer_configuration(
name: &str,
body: &crate::v1_10::api::admissionregistration::v1alpha1::InitializerConfiguration,
Expand Down Expand Up @@ -569,20 +590,23 @@ impl InitializerConfiguration {
}

/// Optional parameters of [`InitializerConfiguration::replace_initializer_configuration`]
#[cfg(feature = "api")]
#[derive(Clone, Copy, Debug, Default)]
pub struct ReplaceInitializerConfigurationOptional<'a> {
/// If 'true', then the output is pretty printed.
pub pretty: Option<&'a str>,
}

/// Use `<ReplaceInitializerConfigurationResponse as Response>::try_from_parts` to parse the HTTP response body of [`InitializerConfiguration::replace_initializer_configuration`]
#[cfg(feature = "api")]
#[derive(Debug)]
pub enum ReplaceInitializerConfigurationResponse {
Ok(crate::v1_10::api::admissionregistration::v1alpha1::InitializerConfiguration),
Created(crate::v1_10::api::admissionregistration::v1alpha1::InitializerConfiguration),
Other(Result<Option<serde_json::Value>, serde_json::Error>),
}

#[cfg(feature = "api")]
impl crate::Response for ReplaceInitializerConfigurationResponse {
fn try_from_parts(status_code: http::StatusCode, buf: &[u8]) -> Result<(Self, usize), crate::ResponseError> {
match status_code {
Expand Down Expand Up @@ -634,6 +658,7 @@ impl InitializerConfiguration {
/// * `optional`
///
/// Optional parameters. Use `Default::default()` to not pass any.
#[cfg(feature = "api")]
pub fn watch_initializer_configuration(
optional: crate::v1_10::WatchOptional<'_>,
) -> Result<(http::Request<Vec<u8>>, fn(http::StatusCode) -> crate::ResponseBody<WatchInitializerConfigurationResponse>), crate::RequestError> {
Expand All @@ -652,12 +677,14 @@ impl InitializerConfiguration {
}

/// Use `<WatchInitializerConfigurationResponse as Response>::try_from_parts` to parse the HTTP response body of [`InitializerConfiguration::watch_initializer_configuration`]
#[cfg(feature = "api")]
#[derive(Debug)]
pub enum WatchInitializerConfigurationResponse {
Ok(crate::v1_10::apimachinery::pkg::apis::meta::v1::WatchEvent<InitializerConfiguration>),
Other(Result<Option<serde_json::Value>, serde_json::Error>),
}

#[cfg(feature = "api")]
impl crate::Response for WatchInitializerConfigurationResponse {
fn try_from_parts(status_code: http::StatusCode, buf: &[u8]) -> Result<(Self, usize), crate::ResponseError> {
match status_code {
Expand Down
32 changes: 12 additions & 20 deletions src/v1_10/api/admissionregistration/v1alpha1/mod.rs
Original file line number Diff line number Diff line change
@@ -1,28 +1,20 @@

mod initializer;
pub use self::initializer::{
Initializer,
};
pub use self::initializer::Initializer;

mod initializer_configuration;
pub use self::initializer_configuration::{
InitializerConfiguration,
CreateInitializerConfigurationOptional, CreateInitializerConfigurationResponse,
DeleteCollectionInitializerConfigurationResponse,
DeleteInitializerConfigurationResponse,
ListInitializerConfigurationResponse,
PatchInitializerConfigurationResponse,
ReadInitializerConfigurationOptional, ReadInitializerConfigurationResponse,
ReplaceInitializerConfigurationOptional, ReplaceInitializerConfigurationResponse,
WatchInitializerConfigurationResponse,
};
pub use self::initializer_configuration::InitializerConfiguration;
#[cfg(feature = "api")] pub use self::initializer_configuration::{CreateInitializerConfigurationOptional, CreateInitializerConfigurationResponse};
#[cfg(feature = "api")] pub use self::initializer_configuration::DeleteCollectionInitializerConfigurationResponse;
#[cfg(feature = "api")] pub use self::initializer_configuration::DeleteInitializerConfigurationResponse;
#[cfg(feature = "api")] pub use self::initializer_configuration::ListInitializerConfigurationResponse;
#[cfg(feature = "api")] pub use self::initializer_configuration::PatchInitializerConfigurationResponse;
#[cfg(feature = "api")] pub use self::initializer_configuration::{ReadInitializerConfigurationOptional, ReadInitializerConfigurationResponse};
#[cfg(feature = "api")] pub use self::initializer_configuration::{ReplaceInitializerConfigurationOptional, ReplaceInitializerConfigurationResponse};
#[cfg(feature = "api")] pub use self::initializer_configuration::WatchInitializerConfigurationResponse;

mod initializer_configuration_list;
pub use self::initializer_configuration_list::{
InitializerConfigurationList,
};
pub use self::initializer_configuration_list::InitializerConfigurationList;

mod rule;
pub use self::rule::{
Rule,
};
pub use self::rule::Rule;
Loading

0 comments on commit 5936200

Please sign in to comment.