use serde::{Deserialize, Serialize};
use crate::client::{Client, Response};
use crate::ids::{ShippingRateId, TaxCodeId};
use crate::params::{
CurrencyMap, Expand, Expandable, List, Metadata, Object, Paginable, RangeQuery, Timestamp,
};
use crate::resources::{Currency, TaxCode};
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct ShippingRate {
pub id: ShippingRateId,
pub active: bool,
pub created: Timestamp,
pub delivery_estimate: Option<ShippingRateDeliveryEstimate>,
pub display_name: Option<String>,
#[serde(skip_serializing_if = "Option::is_none")]
pub fixed_amount: Option<ShippingRateFixedAmount>,
pub livemode: bool,
pub metadata: Metadata,
pub tax_behavior: Option<ShippingRateTaxBehavior>,
pub tax_code: Option<Expandable<TaxCode>>,
#[serde(rename = "type")]
pub type_: ShippingRateType,
}
impl ShippingRate {
pub fn list(client: &Client, params: &ListShippingRates<'_>) -> Response<List<ShippingRate>> {
client.get_query("/shipping_rates", params)
}
pub fn create(client: &Client, params: CreateShippingRate<'_>) -> Response<ShippingRate> {
#[allow(clippy::needless_borrows_for_generic_args)]
client.post_form("/shipping_rates", ¶ms)
}
pub fn retrieve(
client: &Client,
id: &ShippingRateId,
expand: &[&str],
) -> Response<ShippingRate> {
client.get_query(&format!("/shipping_rates/{}", id), Expand { expand })
}
pub fn update(
client: &Client,
id: &ShippingRateId,
params: UpdateShippingRate<'_>,
) -> Response<ShippingRate> {
#[allow(clippy::needless_borrows_for_generic_args)]
client.post_form(&format!("/shipping_rates/{}", id), ¶ms)
}
}
impl Object for ShippingRate {
type Id = ShippingRateId;
fn id(&self) -> Self::Id {
self.id.clone()
}
fn object(&self) -> &'static str {
"shipping_rate"
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct ShippingRateDeliveryEstimate {
pub maximum: Option<ShippingRateDeliveryEstimateBound>,
pub minimum: Option<ShippingRateDeliveryEstimateBound>,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct ShippingRateDeliveryEstimateBound {
pub unit: ShippingRateDeliveryEstimateBoundUnit,
pub value: i64,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct ShippingRateFixedAmount {
pub amount: i64,
pub currency: Currency,
#[serde(skip_serializing_if = "Option::is_none")]
pub currency_options: Option<CurrencyMap<ShippingRateCurrencyOption>>,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct ShippingRateCurrencyOption {
pub amount: i64,
pub tax_behavior: ShippingRateCurrencyOptionTaxBehavior,
}
#[derive(Clone, Debug, Serialize)]
pub struct CreateShippingRate<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
pub delivery_estimate: Option<CreateShippingRateDeliveryEstimate>,
pub display_name: &'a str,
#[serde(skip_serializing_if = "Expand::is_empty")]
pub expand: &'a [&'a str],
#[serde(skip_serializing_if = "Option::is_none")]
pub fixed_amount: Option<CreateShippingRateFixedAmount>,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<Metadata>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tax_behavior: Option<ShippingRateTaxBehavior>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tax_code: Option<TaxCodeId>,
#[serde(rename = "type")]
#[serde(skip_serializing_if = "Option::is_none")]
pub type_: Option<ShippingRateType>,
}
impl<'a> CreateShippingRate<'a> {
pub fn new(display_name: &'a str) -> Self {
CreateShippingRate {
delivery_estimate: Default::default(),
display_name,
expand: Default::default(),
fixed_amount: Default::default(),
metadata: Default::default(),
tax_behavior: Default::default(),
tax_code: Default::default(),
type_: Default::default(),
}
}
}
#[derive(Clone, Debug, Serialize, Default)]
pub struct ListShippingRates<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
pub active: Option<bool>,
#[serde(skip_serializing_if = "Option::is_none")]
pub created: Option<RangeQuery<Timestamp>>,
#[serde(skip_serializing_if = "Option::is_none")]
pub currency: Option<Currency>,
#[serde(skip_serializing_if = "Option::is_none")]
pub ending_before: Option<ShippingRateId>,
#[serde(skip_serializing_if = "Expand::is_empty")]
pub expand: &'a [&'a str],
#[serde(skip_serializing_if = "Option::is_none")]
pub limit: Option<u64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub starting_after: Option<ShippingRateId>,
}
impl<'a> ListShippingRates<'a> {
pub fn new() -> Self {
ListShippingRates {
active: Default::default(),
created: Default::default(),
currency: Default::default(),
ending_before: Default::default(),
expand: Default::default(),
limit: Default::default(),
starting_after: Default::default(),
}
}
}
impl Paginable for ListShippingRates<'_> {
type O = ShippingRate;
fn set_last(&mut self, item: Self::O) {
self.starting_after = Some(item.id());
}
}
#[derive(Clone, Debug, Serialize, Default)]
pub struct UpdateShippingRate<'a> {
#[serde(skip_serializing_if = "Option::is_none")]
pub active: Option<bool>,
#[serde(skip_serializing_if = "Expand::is_empty")]
pub expand: &'a [&'a str],
#[serde(skip_serializing_if = "Option::is_none")]
pub fixed_amount: Option<UpdateShippingRateFixedAmount>,
#[serde(skip_serializing_if = "Option::is_none")]
pub metadata: Option<Metadata>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tax_behavior: Option<ShippingRateTaxBehavior>,
}
impl<'a> UpdateShippingRate<'a> {
pub fn new() -> Self {
UpdateShippingRate {
active: Default::default(),
expand: Default::default(),
fixed_amount: Default::default(),
metadata: Default::default(),
tax_behavior: Default::default(),
}
}
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct CreateShippingRateDeliveryEstimate {
#[serde(skip_serializing_if = "Option::is_none")]
pub maximum: Option<CreateShippingRateDeliveryEstimateMaximum>,
#[serde(skip_serializing_if = "Option::is_none")]
pub minimum: Option<CreateShippingRateDeliveryEstimateMinimum>,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct CreateShippingRateFixedAmount {
pub amount: i64,
pub currency: Currency,
#[serde(skip_serializing_if = "Option::is_none")]
pub currency_options: Option<CurrencyMap<CreateShippingRateFixedAmountCurrencyOptions>>,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct UpdateShippingRateFixedAmount {
#[serde(skip_serializing_if = "Option::is_none")]
pub currency_options: Option<CurrencyMap<UpdateShippingRateFixedAmountCurrencyOptions>>,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct CreateShippingRateDeliveryEstimateMaximum {
pub unit: CreateShippingRateDeliveryEstimateMaximumUnit,
pub value: i64,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct CreateShippingRateDeliveryEstimateMinimum {
pub unit: CreateShippingRateDeliveryEstimateMinimumUnit,
pub value: i64,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct CreateShippingRateFixedAmountCurrencyOptions {
pub amount: i64,
#[serde(skip_serializing_if = "Option::is_none")]
pub tax_behavior: Option<CreateShippingRateFixedAmountCurrencyOptionsTaxBehavior>,
}
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
pub struct UpdateShippingRateFixedAmountCurrencyOptions {
#[serde(skip_serializing_if = "Option::is_none")]
pub amount: Option<i64>,
#[serde(skip_serializing_if = "Option::is_none")]
pub tax_behavior: Option<UpdateShippingRateFixedAmountCurrencyOptionsTaxBehavior>,
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum CreateShippingRateDeliveryEstimateMaximumUnit {
BusinessDay,
Day,
Hour,
Month,
Week,
}
impl CreateShippingRateDeliveryEstimateMaximumUnit {
pub fn as_str(self) -> &'static str {
match self {
CreateShippingRateDeliveryEstimateMaximumUnit::BusinessDay => "business_day",
CreateShippingRateDeliveryEstimateMaximumUnit::Day => "day",
CreateShippingRateDeliveryEstimateMaximumUnit::Hour => "hour",
CreateShippingRateDeliveryEstimateMaximumUnit::Month => "month",
CreateShippingRateDeliveryEstimateMaximumUnit::Week => "week",
}
}
}
impl AsRef<str> for CreateShippingRateDeliveryEstimateMaximumUnit {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for CreateShippingRateDeliveryEstimateMaximumUnit {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
impl std::default::Default for CreateShippingRateDeliveryEstimateMaximumUnit {
fn default() -> Self {
Self::BusinessDay
}
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum CreateShippingRateDeliveryEstimateMinimumUnit {
BusinessDay,
Day,
Hour,
Month,
Week,
}
impl CreateShippingRateDeliveryEstimateMinimumUnit {
pub fn as_str(self) -> &'static str {
match self {
CreateShippingRateDeliveryEstimateMinimumUnit::BusinessDay => "business_day",
CreateShippingRateDeliveryEstimateMinimumUnit::Day => "day",
CreateShippingRateDeliveryEstimateMinimumUnit::Hour => "hour",
CreateShippingRateDeliveryEstimateMinimumUnit::Month => "month",
CreateShippingRateDeliveryEstimateMinimumUnit::Week => "week",
}
}
}
impl AsRef<str> for CreateShippingRateDeliveryEstimateMinimumUnit {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for CreateShippingRateDeliveryEstimateMinimumUnit {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
impl std::default::Default for CreateShippingRateDeliveryEstimateMinimumUnit {
fn default() -> Self {
Self::BusinessDay
}
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum CreateShippingRateFixedAmountCurrencyOptionsTaxBehavior {
Exclusive,
Inclusive,
Unspecified,
}
impl CreateShippingRateFixedAmountCurrencyOptionsTaxBehavior {
pub fn as_str(self) -> &'static str {
match self {
CreateShippingRateFixedAmountCurrencyOptionsTaxBehavior::Exclusive => "exclusive",
CreateShippingRateFixedAmountCurrencyOptionsTaxBehavior::Inclusive => "inclusive",
CreateShippingRateFixedAmountCurrencyOptionsTaxBehavior::Unspecified => "unspecified",
}
}
}
impl AsRef<str> for CreateShippingRateFixedAmountCurrencyOptionsTaxBehavior {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for CreateShippingRateFixedAmountCurrencyOptionsTaxBehavior {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
impl std::default::Default for CreateShippingRateFixedAmountCurrencyOptionsTaxBehavior {
fn default() -> Self {
Self::Exclusive
}
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ShippingRateCurrencyOptionTaxBehavior {
Exclusive,
Inclusive,
Unspecified,
}
impl ShippingRateCurrencyOptionTaxBehavior {
pub fn as_str(self) -> &'static str {
match self {
ShippingRateCurrencyOptionTaxBehavior::Exclusive => "exclusive",
ShippingRateCurrencyOptionTaxBehavior::Inclusive => "inclusive",
ShippingRateCurrencyOptionTaxBehavior::Unspecified => "unspecified",
}
}
}
impl AsRef<str> for ShippingRateCurrencyOptionTaxBehavior {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for ShippingRateCurrencyOptionTaxBehavior {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
impl std::default::Default for ShippingRateCurrencyOptionTaxBehavior {
fn default() -> Self {
Self::Exclusive
}
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ShippingRateDeliveryEstimateBoundUnit {
BusinessDay,
Day,
Hour,
Month,
Week,
}
impl ShippingRateDeliveryEstimateBoundUnit {
pub fn as_str(self) -> &'static str {
match self {
ShippingRateDeliveryEstimateBoundUnit::BusinessDay => "business_day",
ShippingRateDeliveryEstimateBoundUnit::Day => "day",
ShippingRateDeliveryEstimateBoundUnit::Hour => "hour",
ShippingRateDeliveryEstimateBoundUnit::Month => "month",
ShippingRateDeliveryEstimateBoundUnit::Week => "week",
}
}
}
impl AsRef<str> for ShippingRateDeliveryEstimateBoundUnit {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for ShippingRateDeliveryEstimateBoundUnit {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
impl std::default::Default for ShippingRateDeliveryEstimateBoundUnit {
fn default() -> Self {
Self::BusinessDay
}
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ShippingRateTaxBehavior {
Exclusive,
Inclusive,
Unspecified,
}
impl ShippingRateTaxBehavior {
pub fn as_str(self) -> &'static str {
match self {
ShippingRateTaxBehavior::Exclusive => "exclusive",
ShippingRateTaxBehavior::Inclusive => "inclusive",
ShippingRateTaxBehavior::Unspecified => "unspecified",
}
}
}
impl AsRef<str> for ShippingRateTaxBehavior {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for ShippingRateTaxBehavior {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
impl std::default::Default for ShippingRateTaxBehavior {
fn default() -> Self {
Self::Exclusive
}
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum ShippingRateType {
FixedAmount,
}
impl ShippingRateType {
pub fn as_str(self) -> &'static str {
match self {
ShippingRateType::FixedAmount => "fixed_amount",
}
}
}
impl AsRef<str> for ShippingRateType {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for ShippingRateType {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
impl std::default::Default for ShippingRateType {
fn default() -> Self {
Self::FixedAmount
}
}
#[derive(Copy, Clone, Debug, Deserialize, Serialize, Eq, PartialEq)]
#[serde(rename_all = "snake_case")]
pub enum UpdateShippingRateFixedAmountCurrencyOptionsTaxBehavior {
Exclusive,
Inclusive,
Unspecified,
}
impl UpdateShippingRateFixedAmountCurrencyOptionsTaxBehavior {
pub fn as_str(self) -> &'static str {
match self {
UpdateShippingRateFixedAmountCurrencyOptionsTaxBehavior::Exclusive => "exclusive",
UpdateShippingRateFixedAmountCurrencyOptionsTaxBehavior::Inclusive => "inclusive",
UpdateShippingRateFixedAmountCurrencyOptionsTaxBehavior::Unspecified => "unspecified",
}
}
}
impl AsRef<str> for UpdateShippingRateFixedAmountCurrencyOptionsTaxBehavior {
fn as_ref(&self) -> &str {
self.as_str()
}
}
impl std::fmt::Display for UpdateShippingRateFixedAmountCurrencyOptionsTaxBehavior {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
self.as_str().fmt(f)
}
}
impl std::default::Default for UpdateShippingRateFixedAmountCurrencyOptionsTaxBehavior {
fn default() -> Self {
Self::Exclusive
}
}