Skip to content

Commit

Permalink
feat: keep original string on unknown variants
Browse files Browse the repository at this point in the history
  • Loading branch information
lasantosr committed Sep 17, 2024
1 parent df8a348 commit d808a99
Show file tree
Hide file tree
Showing 67 changed files with 14,779 additions and 766 deletions.
12 changes: 4 additions & 8 deletions async-stripe-webhook/src/webhook.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
use std::str::FromStr;

use chrono::Utc;
use hmac::{Hmac, Mac};
use sha2::Sha256;
Expand Down Expand Up @@ -118,9 +116,7 @@ impl Webhook {

Ok(Event {
account: base_evt.account,
api_version: base_evt
.api_version
.map(|s| ApiVersion::from_str(&s).unwrap_or(ApiVersion::Unknown)),
api_version: base_evt.api_version.map(ApiVersion::from),
created: base_evt.created,
data: EventData {
object: event_obj,
Expand Down Expand Up @@ -204,7 +200,7 @@ mod tests {
format!("t={timestamp},v1={v1}")
}

fn mock_webhook_event(event_type: EventType, data: Value) -> Value {
fn mock_webhook_event(event_type: &EventType, data: Value) -> Value {
json!({
"id": "evt_123",
"object": "event",
Expand All @@ -227,7 +223,7 @@ mod tests {
#[track_caller]
fn parse_mock_webhook_event(event_type: EventType, data: Value) -> EventObject {
let now = Utc::now().timestamp();
let payload = mock_webhook_event(event_type, data).to_string();
let payload = mock_webhook_event(&event_type, data).to_string();
let sig = get_mock_stripe_sig(&payload, now);

let webhook = Webhook { current_timestamp: now };
Expand Down Expand Up @@ -257,7 +253,7 @@ mod tests {
"proration": false,
"quantity": 3
});
let payload = mock_webhook_event(EventType::InvoiceitemCreated, object);
let payload = mock_webhook_event(&EventType::InvoiceitemCreated, object);
let event_timestamp = 1533204620;
let signature = format!("t={event_timestamp},v1=5a81ebe328da1df19581cbc6c7377920947ffd30b56eebcc7ba9a6938a090965,v0=63f3a72374a733066c4be69ed7f8e5ac85c22c9f0a6a612ab9a025a9e4ee7eef");

Expand Down
5 changes: 4 additions & 1 deletion async-stripe/tests/it/hyper.rs
Original file line number Diff line number Diff line change
Expand Up @@ -227,7 +227,10 @@ async fn user_error_transfers() {
// NB: `Unknown` here because the error code reported in the issue is not
// present in the OpenAPI spec. Reporting unknown instead of an error seems
// better regardless so that stripe adding new variants is not a breaking change
assert_eq!(err.code, Some(ApiErrorsCode::Unknown));
assert_eq!(
err.code,
Some(ApiErrorsCode::Unknown(String::from("invalid_request_error")))
);
}
_ => panic!("Expected stripe error, got {:?}", res),
}
Expand Down
67 changes: 61 additions & 6 deletions generated/async-stripe-billing/src/billing_portal_session/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -144,7 +144,7 @@ const _: () = {
self.flow.take(),
self.id.take(),
self.livemode,
self.locale,
self.locale.take(),
self.on_behalf_of.take(),
self.return_url.take(),
self.url.take(),
Expand Down Expand Up @@ -239,7 +239,7 @@ impl stripe_types::Object for BillingPortalSession {
}
}
stripe_types::def_id!(BillingPortalSessionId);
#[derive(Copy, Clone, Eq, PartialEq)]
#[derive(Clone, Eq, PartialEq)]
#[non_exhaustive]
pub enum BillingPortalSessionLocale {
Auto,
Expand Down Expand Up @@ -290,10 +290,10 @@ pub enum BillingPortalSessionLocale {
ZhMinusHk,
ZhMinusTw,
/// An unrecognized value from Stripe. Should not be used as a request parameter.
Unknown,
Unknown(String),
}
impl BillingPortalSessionLocale {
pub fn as_str(self) -> &'static str {
pub fn as_str(&self) -> &str {
use BillingPortalSessionLocale::*;
match self {
Auto => "auto",
Expand Down Expand Up @@ -343,7 +343,7 @@ impl BillingPortalSessionLocale {
Zh => "zh",
ZhMinusHk => "zh-HK",
ZhMinusTw => "zh-TW",
Unknown => "unknown",
Unknown(v) => &v,
}
}
}
Expand Down Expand Up @@ -400,7 +400,7 @@ impl std::str::FromStr for BillingPortalSessionLocale {
"zh" => Ok(Zh),
"zh-HK" => Ok(ZhMinusHk),
"zh-TW" => Ok(ZhMinusTw),
_ => Ok(Self::Unknown),
v => Ok(Unknown(v.to_owned())),
}
}
}
Expand All @@ -415,6 +415,61 @@ impl std::fmt::Debug for BillingPortalSessionLocale {
f.write_str(self.as_str())
}
}
impl From<String> for BillingPortalSessionLocale {
fn from(s: String) -> Self {
use BillingPortalSessionLocale::*;
match s.as_str() {
"auto" => Auto,
"bg" => Bg,
"cs" => Cs,
"da" => Da,
"de" => De,
"el" => El,
"en" => En,
"en-AU" => EnMinusAu,
"en-CA" => EnMinusCa,
"en-GB" => EnMinusGb,
"en-IE" => EnMinusIe,
"en-IN" => EnMinusIn,
"en-NZ" => EnMinusNz,
"en-SG" => EnMinusSg,
"es" => Es,
"es-419" => EsMinus419,
"et" => Et,
"fi" => Fi,
"fil" => Fil,
"fr" => Fr,
"fr-CA" => FrMinusCa,
"hr" => Hr,
"hu" => Hu,
"id" => Id,
"it" => It,
"ja" => Ja,
"ko" => Ko,
"lt" => Lt,
"lv" => Lv,
"ms" => Ms,
"mt" => Mt,
"nb" => Nb,
"nl" => Nl,
"pl" => Pl,
"pt" => Pt,
"pt-BR" => PtMinusBr,
"ro" => Ro,
"ru" => Ru,
"sk" => Sk,
"sl" => Sl,
"sv" => Sv,
"th" => Th,
"tr" => Tr,
"vi" => Vi,
"zh" => Zh,
"zh-HK" => ZhMinusHk,
"zh-TW" => ZhMinusTw,
_ => Unknown(s),
}
}
}
impl serde::Serialize for BillingPortalSessionLocale {
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
Expand Down
Loading

0 comments on commit d808a99

Please sign in to comment.