From 7964c373cde3dbea37d2aef86cca045fc31ca4f6 Mon Sep 17 00:00:00 2001 From: Jonas Platte Date: Mon, 3 Mar 2025 15:02:12 +0100 Subject: [PATCH] Replace svix-lib-rust template with rust example template (#85) --- .../api_resource.rs.jinja | 41 ++++---------- .../api_summary.rs.jinja | 3 +- .../component_type.rs.jinja | 0 .../types/integer_enum.rs.jinja | 0 .../types/string_enum.rs.jinja | 7 +-- .../types/struct.rs.jinja | 2 +- .../api_extra/application_create.rs | 16 ------ templates/svix-lib-rust/api_extra/message.rs | 33 ------------ ...message_attempt_list_attempted_messages.rs | 40 -------------- .../api_extra/message_expunge_content.rs | 54 ------------------- 10 files changed, 15 insertions(+), 181 deletions(-) rename templates/{svix-lib-rust => rust}/api_resource.rs.jinja (78%) rename templates/{svix-lib-rust => rust}/api_summary.rs.jinja (86%) rename templates/{svix-lib-rust => rust}/component_type.rs.jinja (100%) rename templates/{svix-lib-rust => rust}/types/integer_enum.rs.jinja (100%) rename templates/{svix-lib-rust => rust}/types/string_enum.rs.jinja (76%) rename templates/{svix-lib-rust => rust}/types/struct.rs.jinja (98%) delete mode 100644 templates/svix-lib-rust/api_extra/application_create.rs delete mode 100644 templates/svix-lib-rust/api_extra/message.rs delete mode 100644 templates/svix-lib-rust/api_extra/message_attempt_list_attempted_messages.rs delete mode 100644 templates/svix-lib-rust/api_extra/message_expunge_content.rs diff --git a/templates/svix-lib-rust/api_resource.rs.jinja b/templates/rust/api_resource.rs.jinja similarity index 78% rename from templates/svix-lib-rust/api_resource.rs.jinja rename to templates/rust/api_resource.rs.jinja index c8177a7..f0e6a25 100644 --- a/templates/svix-lib-rust/api_resource.rs.jinja +++ b/templates/rust/api_resource.rs.jinja @@ -1,3 +1,13 @@ +{# + This example template is a simplified version of the one we use in + https://github.com/svix/svix-webhooks + + It currently depends on the (non-generated) `crate::error::Result`, + `crate::Configuration` and `crate::request::Request` + + A PR to make it more generic (e.g. use reqwest directly) would be welcome +-#} +// this file is @generated {% set resource_type_name = resource.name | to_upper_camel_case -%} use crate::{ @@ -8,25 +18,12 @@ use crate::{ {% for op in resource.operations -%} {% if op | has_query_or_header_params %} - {% if not op | has_required_query_or_header_params -%} - #[derive(Default)] - {% endif -%} pub struct {{ resource_type_name }}{{ op.name | to_upper_camel_case }}Options { {% for p in op.query_params -%} - {% if p.type.is_datetime() -%} - {% set ty = "String" -%} - {% else -%} - {% set ty = p.type.to_rust() -%} - {% endif -%} + {% set ty = p.type.to_rust() -%} {% if not p.required %}{% set ty %}Option<{{ ty }}>{% endset %}{% endif %} {% if p.description is defined -%} {{ p.description | to_doc_comment(style="rust") }} - {# we currently use String for date-time params, for backwards compat -#} - {# document the format so it's not _that_ awkward -#} - {% if p.type.is_datetime() -%} - /// - /// RFC3339 date string. - {% endif -%} {% endif -%} pub {{ p.name | to_snake_case }}: {{ ty }}, {% endfor -%} @@ -95,11 +92,6 @@ impl<'a> {{ resource_type_name }}<'a> { ; {% endif -%} - {% if has_header_params -%} - {# unpack PostOptions -#} - let PostOptions { idempotency_key } = options.unwrap_or_default(); - {% endif -%} - {# make the request #} crate::request::Request::new(http1::Method::{{ op.method | upper }}, "{{ op.path }}") @@ -131,15 +123,4 @@ impl<'a> {{ resource_type_name }}<'a> { .execute(self.cfg) .await } - - {% set extra_path -%} - api_extra/{{ resource.name | to_snake_case }}_{{ op.name | to_snake_case }}.rs - {%- endset -%} - {% include extra_path ignore missing %} - {% endfor %} } - -{% set extra_path -%} - api_extra/{{ resource.name | to_snake_case }}.rs -{%- endset -%} -{% include extra_path ignore missing %} diff --git a/templates/svix-lib-rust/api_summary.rs.jinja b/templates/rust/api_summary.rs.jinja similarity index 86% rename from templates/svix-lib-rust/api_summary.rs.jinja rename to templates/rust/api_summary.rs.jinja index 37e92b8..3396241 100644 --- a/templates/svix-lib-rust/api_summary.rs.jinja +++ b/templates/rust/api_summary.rs.jinja @@ -1,3 +1,4 @@ +// this file is @generated {% for resource in api.resources -%} mod {{ resource.name | to_snake_case }}; {% endfor %} @@ -8,7 +9,7 @@ pub use self::{ {{ resource.name | to_snake_case }}::{ {{ resource_type_name }}, {% for op in resource.operations -%} - {% if op.query_params | length > 0 -%} + {% if op | has_query_or_header_params -%} {{ resource_type_name }}{{ op.name | to_upper_camel_case }}Options, {% endif -%} {% endfor -%} diff --git a/templates/svix-lib-rust/component_type.rs.jinja b/templates/rust/component_type.rs.jinja similarity index 100% rename from templates/svix-lib-rust/component_type.rs.jinja rename to templates/rust/component_type.rs.jinja diff --git a/templates/svix-lib-rust/types/integer_enum.rs.jinja b/templates/rust/types/integer_enum.rs.jinja similarity index 100% rename from templates/svix-lib-rust/types/integer_enum.rs.jinja rename to templates/rust/types/integer_enum.rs.jinja diff --git a/templates/svix-lib-rust/types/string_enum.rs.jinja b/templates/rust/types/string_enum.rs.jinja similarity index 76% rename from templates/svix-lib-rust/types/string_enum.rs.jinja rename to templates/rust/types/string_enum.rs.jinja index c0669c7..c54af26 100644 --- a/templates/svix-lib-rust/types/string_enum.rs.jinja +++ b/templates/rust/types/string_enum.rs.jinja @@ -3,14 +3,9 @@ use std::fmt; use serde::{Deserialize, Serialize}; {{ doc_comment }} -#[derive( - Clone, Copy, Debug, Default, Eq, PartialEq, Ord, PartialOrd, Hash, Serialize, Deserialize, -)] +#[derive(Clone, Copy, Debug, PartialEq, Eq, PartialOrd, Ord, Hash, Serialize, Deserialize)] pub enum {{ type.name | to_upper_camel_case }} { {% for value in type.values -%} - {% if loop.first -%} - #[default] - {% endif -%} #[serde(rename = "{{ value }}")] {{ value | to_upper_camel_case }}, {% endfor -%} diff --git a/templates/svix-lib-rust/types/struct.rs.jinja b/templates/rust/types/struct.rs.jinja similarity index 98% rename from templates/svix-lib-rust/types/struct.rs.jinja rename to templates/rust/types/struct.rs.jinja index 6318c35..c40cf52 100644 --- a/templates/svix-lib-rust/types/struct.rs.jinja +++ b/templates/rust/types/struct.rs.jinja @@ -10,7 +10,7 @@ use super::{ }; {{ doc_comment }} -#[derive(Clone, Debug, Default, PartialEq, Deserialize, Serialize)] +#[derive(Clone, Debug, Deserialize, Serialize)] pub struct {{ type.name | to_upper_camel_case }} { {% for field in type.fields %} {% if field.description is defined -%} diff --git a/templates/svix-lib-rust/api_extra/application_create.rs b/templates/svix-lib-rust/api_extra/application_create.rs deleted file mode 100644 index bc53ec2..0000000 --- a/templates/svix-lib-rust/api_extra/application_create.rs +++ /dev/null @@ -1,16 +0,0 @@ -/// Create the application with the given ID, or create a new one if it -/// doesn't exist yet. -pub async fn get_or_create( - &self, - application_in: ApplicationIn, - options: Option, -) -> Result { - let ApplicationCreateOptions { idempotency_key } = options.unwrap_or_default(); - - crate::request::Request::new(http1::Method::POST, "/api/v1/app") - .with_query_param("get_if_exists", "true".to_owned()) - .with_optional_header_param("idempotency-key", idempotency_key) - .with_body_param(application_in) - .execute(self.cfg) - .await -} diff --git a/templates/svix-lib-rust/api_extra/message.rs b/templates/svix-lib-rust/api_extra/message.rs deleted file mode 100644 index 1d5b016..0000000 --- a/templates/svix-lib-rust/api_extra/message.rs +++ /dev/null @@ -1,33 +0,0 @@ -#[cfg(feature = "svix_beta")] -#[derive(Clone, Debug)] -pub struct V1MessageEventsParams { - /// The app's ID or UID - pub app_id: String, - /// Limit the number of returned items - pub limit: Option, - /// The iterator returned from a prior invocation - pub iterator: Option, - /// Filter response based on the event type - pub event_types: Option>, - /// Filter response based on the event type. - pub channels: Option>, - pub after: Option, -} - -#[cfg(feature = "svix_beta")] -#[derive(Clone, Debug)] -pub struct V1MessageEventsSubscriptionParams { - /// The app's ID or UID - pub app_id: String, - /// The esub's ID or UID - pub subscription_id: String, - /// Limit the number of returned items - pub limit: Option, - /// The iterator returned from a prior invocation - pub iterator: Option, - /// Filter response based on the event type - pub event_types: Option>, - /// Filter response based on the event type. - pub channels: Option>, - pub after: Option, -} diff --git a/templates/svix-lib-rust/api_extra/message_attempt_list_attempted_messages.rs b/templates/svix-lib-rust/api_extra/message_attempt_list_attempted_messages.rs deleted file mode 100644 index 1867ee3..0000000 --- a/templates/svix-lib-rust/api_extra/message_attempt_list_attempted_messages.rs +++ /dev/null @@ -1,40 +0,0 @@ -#[deprecated = "Use `list_by_msg` instead, setting the `endpoint_id` in `options`."] -pub async fn list_attempts_for_endpoint( - &self, - app_id: String, - msg_id: String, - endpoint_id: String, - options: Option, -) -> Result { - let MessageAttemptListByMsgOptions { - iterator, - limit, - event_types, - before, - after, - channel, - tag, - status, - status_code_class: _, - endpoint_id: _, - with_content: _, - } = options.unwrap_or_default(); - - crate::request::Request::new( - http1::Method::GET, - "/api/v1/app/{app_id}/msg/{msg_id}/endpoint/{endpoint_id}/attempt", - ) - .with_optional_query_param("limit", limit) - .with_optional_query_param("iterator", iterator) - .with_optional_query_param("channel", channel) - .with_optional_query_param("tag", tag) - .with_optional_query_param("status", status) - .with_optional_query_param("before", before) - .with_optional_query_param("after", after) - .with_optional_query_param("event_types", event_types) - .with_path_param("app_id", app_id.to_string()) - .with_path_param("msg_id", msg_id.to_string()) - .with_path_param("endpoint_id", endpoint_id.to_string()) - .execute(self.cfg) - .await -} diff --git a/templates/svix-lib-rust/api_extra/message_expunge_content.rs b/templates/svix-lib-rust/api_extra/message_expunge_content.rs deleted file mode 100644 index 800c7c8..0000000 --- a/templates/svix-lib-rust/api_extra/message_expunge_content.rs +++ /dev/null @@ -1,54 +0,0 @@ -#[cfg(feature = "svix_beta")] -pub async fn events( - &self, - params: V1MessageEventsParams, -) -> Result { - let V1MessageEventsParams { - app_id, - limit, - iterator, - event_types, - channels, - after, - } = params; - - crate::request::Request::new(http1::Method::GET, "/api/v1/app/{app_id}/events") - .with_path_param("app_id", app_id) - .with_optional_query_param("limit", limit) - .with_optional_query_param("iterator", iterator) - .with_optional_query_param("event_types", event_types) - .with_optional_query_param("channels", channels) - .with_optional_query_param("after", after) - .execute(self.cfg) - .await -} - -#[cfg(feature = "svix_beta")] -pub async fn events_subscription( - &self, - params: V1MessageEventsSubscriptionParams, -) -> Result { - let V1MessageEventsSubscriptionParams { - app_id, - subscription_id, - limit, - iterator, - event_types, - channels, - after, - } = params; - - crate::request::Request::new( - http1::Method::GET, - "/api/v1/app/{app_id}/events/subscription/{subscription_id}", - ) - .with_path_param("app_id", app_id.to_string()) - .with_path_param("subscription_id", subscription_id.to_string()) - .with_optional_query_param("limit", limit) - .with_optional_query_param("iterator", iterator) - .with_optional_query_param("event_types", event_types) - .with_optional_query_param("channels", channels) - .with_optional_query_param("after", after) - .execute(self.cfg) - .await -}