Skip to content

Commit

Permalink
rename outputs to something more appropriate
Browse files Browse the repository at this point in the history
outputstruct can be an enum now, so rename to container

containers contain members, and a new Output wrapper contains all
containers.

does main refactoring in #70

Signed-off-by: clux <[email protected]>
  • Loading branch information
clux committed May 3, 2022
1 parent 406576c commit c6639dc
Show file tree
Hide file tree
Showing 4 changed files with 23 additions and 22 deletions.
18 changes: 9 additions & 9 deletions src/analyzer.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
//! Deals entirely with schema analysis for the purpose of creating output structs + members
use crate::{OutputMember, OutputStruct, Output};
use crate::{Container, Member, Output};
use anyhow::{bail, Result};
use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::{
JSONSchemaProps, JSONSchemaPropsOrArray, JSONSchemaPropsOrBool, JSON,
Expand Down Expand Up @@ -30,7 +30,7 @@ fn analyze_(
current: &str,
stack: &str,
level: u8,
results: &mut Vec<OutputStruct>,
results: &mut Vec<Container>,
) -> Result<()> {
let props = schema.properties.clone().unwrap_or_default();
let mut array_recurse_level: HashMap<String, u8> = Default::default();
Expand Down Expand Up @@ -148,7 +148,7 @@ fn analyze_enum_properties(
stack: &str,
level: u8,
schema: &JSONSchemaProps,
) -> Result<OutputStruct, anyhow::Error> {
) -> Result<Container, anyhow::Error> {
let mut members = vec![];
debug!("analyzing enum {}", serde_json::to_string(&schema).unwrap());
for en in items {
Expand All @@ -161,14 +161,14 @@ fn analyze_enum_properties(
// Create member and wrap types correctly
let member_doc = None;
debug!("with enum member {} of type {}", name, rust_type);
members.push(OutputMember {
members.push(Member {
type_: rust_type,
name: name.to_string(),
serde_annot: vec![],
docs: member_doc,
})
}
Ok(OutputStruct {
Ok(Container {
name: stack.to_string(),
members,
level,
Expand All @@ -185,7 +185,7 @@ fn analyze_object_properties(
array_recurse_level: &mut HashMap<String, u8>,
level: u8,
schema: &JSONSchemaProps,
) -> Result<Vec<OutputStruct>, anyhow::Error> {
) -> Result<Vec<Container>, anyhow::Error> {
let mut results = vec![];
let mut members = vec![];
//debug!("analyzing object {}", serde_json::to_string(&schema).unwrap());
Expand Down Expand Up @@ -304,7 +304,7 @@ fn analyze_object_properties(
let member_doc = value.description.clone();
if reqs.contains(key) {
debug!("with required member {} of type {}", key, &rust_type);
members.push(OutputMember {
members.push(Member {
type_: rust_type,
name: key.to_string(),
serde_annot: vec![],
Expand All @@ -313,7 +313,7 @@ fn analyze_object_properties(
} else {
// option wrapping needed if not required
debug!("with optional member {} of type {}", key, rust_type);
members.push(OutputMember {
members.push(Member {
type_: format!("Option<{}>", rust_type),
name: key.to_string(),
serde_annot: vec![
Expand All @@ -328,7 +328,7 @@ fn analyze_object_properties(
// probably better to do impl Default to avoid having to make custom fns
}
}
results.push(OutputStruct {
results.push(Container {
name: stack.to_string(),
members,
level,
Expand Down
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
mod analyzer;
pub use analyzer::analyze;
mod output;
pub use output::{OutputMember, OutputStruct, Output};
pub use output::{Container, Member, Output};

// synced from https://doc.rust-lang.org/reference/keywords.html feb 2022
pub const KEYWORDS: [&str; 52] = [
Expand Down
4 changes: 2 additions & 2 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{anyhow, Context, Result};
use k8s_openapi::apiextensions_apiserver::pkg::apis::apiextensions::v1::{
CustomResourceDefinition, CustomResourceDefinitionVersion, CustomResourceSubresources,
};
use kopium::{analyze, OutputStruct, KEYWORDS};
use kopium::{analyze, Container, KEYWORDS};
use kube::{api, core::Version, Api, Client, ResourceExt};
use quote::format_ident;
use std::path::PathBuf;
Expand Down Expand Up @@ -318,7 +318,7 @@ impl Kopium {
println!("#[derive({})]", derives.join(", "));
}

fn print_prelude(&self, results: &[OutputStruct]) {
fn print_prelude(&self, results: &[Container]) {
if !self.rust_case && !self.hide_inner_attr {
println!("#![allow(non_snake_case)]");
// NB: we cannot allow warnings for bad enum names see #69
Expand Down
21 changes: 11 additions & 10 deletions src/output.rs
Original file line number Diff line number Diff line change
@@ -1,26 +1,26 @@
use heck::{ToSnakeCase, ToPascalCase};
use heck::{ToPascalCase, ToSnakeCase};

/// All found containers
pub struct Output(pub Vec<OutputStruct>);
pub struct Output(pub Vec<Container>);

/// Output container found by analyzer
#[derive(Default, Debug)]
pub struct OutputStruct {
pub struct Container {
/// The short name of the struct (kind + capitalized suffix)
pub name: String,
/// The nestedness level the container was found in
pub level: u8,
/// Members or enum members of the container
pub members: Vec<OutputMember>,
pub members: Vec<Member>,
/// Documentation properties extracted for the container
pub docs: Option<String>,
/// Whether this container is an enum
pub is_enum: bool,
}

/// Output member belonging to an OutputStruct
/// Output member belonging to an Container
#[derive(Default, Debug)]
pub struct OutputMember {
pub struct Member {
/// The raw, unsanitized name of the member
///
/// This must be sanitized against KEYWORDS before it can be printed
Expand All @@ -34,12 +34,12 @@ pub struct OutputMember {
/// - skip_serializing_if = "Option::is_none" (if the type is an Option)
/// - rename = "orig_name" (if the type does not match rust casing conventions)
///
/// The `rename` attribute is only set if `OutputStruct::rename` is called.
/// The `rename` attribute is only set if `Container::rename` is called.
pub serde_annot: Vec<String>,
pub docs: Option<String>,
}

impl OutputStruct {
impl Container {
pub fn uses_btreemaps(&self) -> bool {
self.members.iter().any(|m| m.type_.contains("BTreeMap"))
}
Expand All @@ -57,7 +57,7 @@ impl OutputStruct {
}
}

impl OutputStruct {
impl Container {
/// Rename all struct members to rust conventions
pub fn rename(&mut self) {
for m in &mut self.members {
Expand All @@ -67,7 +67,8 @@ impl OutputStruct {
m.serde_annot.push(format!("rename = \"{}\"", m.name));
}
m.name = pascald;
} else { // regular container
} else {
// regular container
let snaked = m.name.to_snake_case();
if snaked != m.name {
m.serde_annot.push(format!("rename = \"{}\"", m.name));
Expand Down

0 comments on commit c6639dc

Please sign in to comment.