Skip to content

Commit

Permalink
Generate enums for Gateway condition types and reasons
Browse files Browse the repository at this point in the history
Add a xtask to generate enums for Condition types and reasons along with
an implemention of the Display trait for easy stringification. Use xtask
for Gateway Condition types and reasons: `GatewayConditionType`,
`GatewayConditionReason`, `ListenerConditionType`,
`ListenerConditionReason`.

Signed-off-by: Sanskar Jaiswal <[email protected]>
  • Loading branch information
aryan9600 committed May 13, 2024
1 parent 298804f commit c5665f4
Show file tree
Hide file tree
Showing 6 changed files with 218 additions and 2 deletions.
74 changes: 74 additions & 0 deletions gateway-api/src/apis/experimental/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// WARNING: generated file - manual changes will be overriden

#[derive(Debug)]
enum GatewayConditionType {

Check warning on line 4 in gateway-api/src/apis/experimental/constants.rs

View workflow job for this annotation

GitHub Actions / integration-tests

enum `GatewayConditionType` is never used
Programmed,
Accepted,
Ready,
}

impl std::fmt::Display for GatewayConditionType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}

#[derive(Debug)]
enum GatewayConditionReason {

Check warning on line 17 in gateway-api/src/apis/experimental/constants.rs

View workflow job for this annotation

GitHub Actions / integration-tests

enum `GatewayConditionReason` is never used
Programmed,
Invalid,
NoResources,
AddressNotAssigned,
AddressNotUsable,
Accepted,
ListenersNotValid,
Pending,
UnsupportedAddress,
InvalidParameters,
Ready,
ListenersNotReady,
}

impl std::fmt::Display for GatewayConditionReason {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}

#[derive(Debug)]
enum ListenerConditionType {

Check warning on line 39 in gateway-api/src/apis/experimental/constants.rs

View workflow job for this annotation

GitHub Actions / integration-tests

enum `ListenerConditionType` is never used
Conflicted,
Accepted,
ResolvedRefs,
Programmed,
Ready,
}

impl std::fmt::Display for ListenerConditionType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}

#[derive(Debug)]
enum ListenerConditionReason {

Check warning on line 54 in gateway-api/src/apis/experimental/constants.rs

View workflow job for this annotation

GitHub Actions / integration-tests

enum `ListenerConditionReason` is never used
HostnameConflict,
ProtocolConflict,
NoConflicts,
Accepted,
PortUnavailable,
UnsupportedProtocol,
InvalidCertificateRef,
InvalidRouteKinds,
RefNotPermitted,
Programmed,
Invalid,
Pending,
Ready,
}

impl std::fmt::Display for ListenerConditionReason {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
1 change: 1 addition & 0 deletions gateway-api/src/apis/experimental/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// WARNING! generated file do not edit
pub mod constants;
mod enum_defaults;
pub mod gatewayclasses;
pub mod gateways;
Expand Down
74 changes: 74 additions & 0 deletions gateway-api/src/apis/standard/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,74 @@
// WARNING: generated file - manual changes will be overriden

#[derive(Debug)]
enum GatewayConditionType {

Check warning on line 4 in gateway-api/src/apis/standard/constants.rs

View workflow job for this annotation

GitHub Actions / integration-tests

enum `GatewayConditionType` is never used
Programmed,
Accepted,
Ready,
}

impl std::fmt::Display for GatewayConditionType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}

#[derive(Debug)]
enum GatewayConditionReason {

Check warning on line 17 in gateway-api/src/apis/standard/constants.rs

View workflow job for this annotation

GitHub Actions / integration-tests

enum `GatewayConditionReason` is never used
Programmed,
Invalid,
NoResources,
AddressNotAssigned,
AddressNotUsable,
Accepted,
ListenersNotValid,
Pending,
UnsupportedAddress,
InvalidParameters,
Ready,
ListenersNotReady,
}

impl std::fmt::Display for GatewayConditionReason {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}

#[derive(Debug)]
enum ListenerConditionType {

Check warning on line 39 in gateway-api/src/apis/standard/constants.rs

View workflow job for this annotation

GitHub Actions / integration-tests

enum `ListenerConditionType` is never used
Conflicted,
Accepted,
ResolvedRefs,
Programmed,
Ready,
}

impl std::fmt::Display for ListenerConditionType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}

#[derive(Debug)]
enum ListenerConditionReason {

Check warning on line 54 in gateway-api/src/apis/standard/constants.rs

View workflow job for this annotation

GitHub Actions / integration-tests

enum `ListenerConditionReason` is never used
HostnameConflict,
ProtocolConflict,
NoConflicts,
Accepted,
PortUnavailable,
UnsupportedProtocol,
InvalidCertificateRef,
InvalidRouteKinds,
RefNotPermitted,
Programmed,
Invalid,
Pending,
Ready,
}

impl std::fmt::Display for ListenerConditionReason {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
write!(f, "{:?}", self)
}
}
1 change: 1 addition & 0 deletions gateway-api/src/apis/standard/mod.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
// WARNING! generated file do not edit
pub mod constants;
mod enum_defaults;
pub mod gatewayclasses;
pub mod gateways;
Expand Down
15 changes: 15 additions & 0 deletions update.sh
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,16 @@ ENUMS_WITH_DEFAULTS=${ENUMS_WITH_DEFAULTS:1}
GATEWAY_API_ENUMS=${ENUMS_WITH_DEFAULTS} cargo xtask gen_enum_defaults >> gateway-api/src/apis/standard/enum_defaults.rs
echo "mod enum_defaults;" >> gateway-api/src/apis/standard/mod.rs

GATEWAY_CONDITION_CONSTANTS="GatewayConditionType=Programmed,Accepted,Ready"
GATEWAY_REASON_CONSTANTS="GatewayConditionReason=Programmed,Invalid,NoResources,AddressNotAssigned,AddressNotUsable,Accepted,ListenersNotValid,Pending,UnsupportedAddress,InvalidParameters,Ready,ListenersNotReady"
LISTENER_CONDITION_CONSTANTS="ListenerConditionType=Conflicted,Accepted,ResolvedRefs,Programmed,Ready"
LISTENER_REASON_CONSTANTS="ListenerConditionReason=HostnameConflict,ProtocolConflict,NoConflicts,Accepted,PortUnavailable,UnsupportedProtocol,InvalidCertificateRef,InvalidRouteKinds,RefNotPermitted,Programmed,Invalid,Pending,Ready"

GATEWAY_CONDITION_CONSTANTS=${GATEWAY_CONDITION_CONSTANTS} GATEWAY_REASON_CONSTANTS=${GATEWAY_REASON_CONSTANTS} \
LISTENER_CONDITION_CONSTANTS=${LISTENER_CONDITION_CONSTANTS} LISTENER_REASON_CONSTANTS=${LISTENER_REASON_CONSTANTS} \
cargo xtask gen_condition_constants >> gateway-api/src/apis/standard/constants.rs
echo "pub mod constants;" >> gateway-api/src/apis/standard/mod.rs

echo "// WARNING! generated file do not edit" > gateway-api/src/apis/experimental/mod.rs

for API in "${EXPERIMENTAL_APIS[@]}"
Expand All @@ -97,5 +107,10 @@ ENUMS_WITH_DEFAULTS=${ENUMS_WITH_DEFAULTS:1}
GATEWAY_API_ENUMS=${ENUMS_WITH_DEFAULTS} cargo xtask gen_enum_defaults >> gateway-api/src/apis/experimental/enum_defaults.rs
echo "mod enum_defaults;" >> gateway-api/src/apis/experimental/mod.rs

GATEWAY_CONDITION_CONSTANTS=${GATEWAY_CONDITION_CONSTANTS} GATEWAY_REASON_CONSTANTS=${GATEWAY_REASON_CONSTANTS} \
LISTENER_CONDITION_CONSTANTS=${LISTENER_CONDITION_CONSTANTS} LISTENER_REASON_CONSTANTS=${LISTENER_REASON_CONSTANTS} \
cargo xtask gen_condition_constants >> gateway-api/src/apis/experimental/constants.rs
echo "pub mod constants;" >> gateway-api/src/apis/experimental/mod.rs

# Format the code.
cargo fmt
55 changes: 53 additions & 2 deletions xtask/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
use std::{collections::BTreeMap, env};

use codegen::{Function, Scope};
use codegen::{Enum, Function, Scope, Variant};

fn main() {
let task = env::args().nth(1);

match task.as_deref() {
Some("gen_enum_defaults") => gen_enum_defaults().unwrap(),
Some("gen_condition_constants") => gen_condition_constants().unwrap(),
_ => print_help(),
}
}
Expand All @@ -16,12 +17,58 @@ fn print_help() {
"Tasks:
gen_enum_defaults generates Default trait impls for enums
gen_constants generates constants used for Conditions
"
)
}

type DynError = Box<dyn std::error::Error>;

fn gen_condition_constants() -> Result<(), DynError> {
let gateway_condition_types = env::var("GATEWAY_CONDITION_CONSTANTS")?;
let gateway_reason_types = env::var("GATEWAY_REASON_CONSTANTS")?;
let listener_condition_types = env::var("LISTENER_CONDITION_CONSTANTS")?;
let listener_reason_types = env::var("LISTENER_REASON_CONSTANTS")?;

let mut scope = Scope::new();
gen_const_enums(&mut scope, gateway_condition_types);
gen_const_enums(&mut scope, gateway_reason_types);
gen_const_enums(&mut scope, listener_condition_types);
gen_const_enums(&mut scope, listener_reason_types);
println!("{}", gen_generated_file_warning());
println!("{}", scope.to_string());
Ok(())
}

fn gen_const_enums(scope: &mut Scope, constants: String) {
let enum_type_and_variants: Vec<&str> = constants.split('=').collect();
let enum_type = enum_type_and_variants[0];
let variants: Vec<&str> = enum_type_and_variants[1].split(',').collect();

let mut enumeration = Enum::new(enum_type);
enumeration.derive("Debug");

for variant in variants {
let var = Variant::new(variant);
enumeration.push_variant(var);
}
scope.push_enum(enumeration);

gen_display_impl(scope, enum_type);
}

fn gen_display_impl(scope: &mut Scope, ty: &str) {
let mut func = Function::new("fmt".to_string());
func.arg_ref_self();
func.arg("f", "&mut std::fmt::Formatter");
func.ret("std::fmt::Result");
func.line("write!(f, \"{:?}\", self)");
scope
.new_impl(ty)
.impl_trait("std::fmt::Display")
.push_fn(func);
}

fn gen_enum_defaults() -> Result<(), DynError> {
// GATEWAY_API_ENUMS provides the enum names along with their default variant to be used in the
// generated Default impl. For eg: GATEWAY_API_ENUMS=enum1=default1,enum2=default2.
Expand Down Expand Up @@ -52,7 +99,7 @@ fn gen_enum_defaults() -> Result<(), DynError> {
}
}

println!("// WARNING: generated file - manual changes will be overriden\n");
println!("{}", gen_generated_file_warning());

// Generate use statements for the enums.
if httproute_enums.len() > 0 {
Expand All @@ -68,6 +115,10 @@ fn gen_enum_defaults() -> Result<(), DynError> {
Ok(())
}

fn gen_generated_file_warning() -> String {
"// WARNING: generated file - manual changes will be overriden\n".into()
}

fn gen_use_stmt(items: Vec<String>, module: String) -> String {
let mut stmt = String::from(format!("use super::{}::{{", module));
for item in items {
Expand Down

0 comments on commit c5665f4

Please sign in to comment.