use serde::{Deserialize, Serialize};
pub const AP2_EXTENSION_URI: &str = "https://github.com/google-agentic-commerce/ap2/tree/v0.1";
pub const INTENT_MANDATE_DATA_KEY: &str = "ap2.mandates.IntentMandate";
pub const CART_MANDATE_DATA_KEY: &str = "ap2.mandates.CartMandate";
pub const PAYMENT_MANDATE_DATA_KEY: &str = "ap2.mandates.PaymentMandate";
pub const PAYMENT_RECEIPT_DATA_KEY: &str = "ap2.PaymentReceipt";
pub const RISK_DATA_KEY: &str = "risk_data";
#[derive(Debug, Clone, PartialEq, Eq, Hash, Serialize, Deserialize)]
pub enum Ap2Role {
#[serde(rename = "merchant")]
Merchant,
#[serde(rename = "shopper")]
Shopper,
#[serde(rename = "credentials-provider")]
CredentialsProvider,
#[serde(rename = "payment-processor")]
PaymentProcessor,
}
impl Ap2Role {
pub fn as_str(&self) -> &'static str {
match self {
Ap2Role::Merchant => "merchant",
Ap2Role::Shopper => "shopper",
Ap2Role::CredentialsProvider => "credentials-provider",
Ap2Role::PaymentProcessor => "payment-processor",
}
}
}
impl std::fmt::Display for Ap2Role {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.write_str(self.as_str())
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn role_serialization() {
assert_eq!(
serde_json::to_string(&Ap2Role::Merchant).unwrap(),
"\"merchant\""
);
assert_eq!(
serde_json::to_string(&Ap2Role::CredentialsProvider).unwrap(),
"\"credentials-provider\""
);
assert_eq!(
serde_json::to_string(&Ap2Role::PaymentProcessor).unwrap(),
"\"payment-processor\""
);
}
#[test]
fn role_deserialization() {
let role: Ap2Role = serde_json::from_str("\"shopper\"").unwrap();
assert_eq!(role, Ap2Role::Shopper);
}
#[test]
fn role_display() {
assert_eq!(
Ap2Role::CredentialsProvider.to_string(),
"credentials-provider"
);
}
}