arlyon / async-stripe

Async (and blocking!) Rust bindings for the Stripe API
https://payments.rs
Apache License 2.0
454 stars 129 forks source link

Can't retrieve customer if i haven't got `CustomerId` which only can be got from creating a customer #498

Closed vincent-thomas closed 8 months ago

vincent-thomas commented 8 months ago

Describe the bug

When i try to retrieve a customer from Customer::retrieve() i need to input from the struct Customerid. Which has a private field, and no functions that create a CustomerId, so its impossible to retrieve a customer, without having to create it in the same code block

To Reproduce

   let test = Customer::retrieve(&c.stripe, &CustomerId("test"), &[".."]); // Error: error[E0423]: cannot initialize a tuple struct which contains private fields

On some inspection, the macro definition looks like this

#[derive(Clone, Debug, Default, Eq, PartialEq, Hash)]
pub struct $struct_name(/* private field => can't initialize myself */ smol_str::SmolStr);

If making the field pub it would make it possible to get customers without creating them

Expected behavior

To be able to create a CustomerId, for then to retrieve the Customer

Code snippets

No response

OS

Not relevant

Rust version

1.77.0-nightly - but not relevant

Library version

0.31.0

API version

2023-10-16

Additional context

No response

augustoccesar commented 8 months ago

Hello! Maybe what you are looking for is from_str.

let customer_id = CustomerId::from_str("test")?;
let customer = Customer::retrieve(&client, &customer_id, &[]).await?;
vincent-thomas commented 8 months ago

Thank you this did work. Maybe implement the trait From<&str> and From to follow that convention too? I know this is codegen so i don't think i can implement it but i think this would be a good idea.

vincent-thomas commented 8 months ago

Correction TryFrom<&str> trait as .from_str() returns Result