delopay 0.20.0

Official Rust SDK for the DeloPay payments API
Documentation
//! Settings models: every setting of a merchant, a project or a shop, each a
//! key with a JSON value, listed with its default, whether a value is stored,
//! its revision and who changed it last.

use serde::Deserialize;
use serde_json::Value;

/// Every setting at one scope.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct SettingsResponse {
    /// `platform`, `merchant`, `project` or `shop`.
    pub scope: String,
    /// `platform`, the merchant id, the project id or the shop's profile id.
    pub scope_id: String,
    /// The settings at this scope.
    #[serde(default)]
    pub settings: Vec<SettingEntry>,
    /// Any additional response fields not modeled above.
    #[serde(flatten)]
    pub extra: serde_json::Map<String, Value>,
}

/// One setting at the requested scope.
#[derive(Debug, Clone, Deserialize)]
#[non_exhaustive]
pub struct SettingEntry {
    /// The setting's key, e.g. `replay_retention`.
    pub key: String,
    /// The connector, for settings kept per connector at the platform scope.
    #[serde(default)]
    pub qualifier: Option<String>,
    /// Who owns the stored value: `operator` or `merchant`. A value the
    /// platform operator set is listed beside the merchant's and is
    /// read-only to the merchant.
    pub authority: String,
    /// The value in force at this scope: the stored value, or the default.
    /// A secret is never returned: it is `null`, and
    /// [`is_set`](Self::is_set) says whether one is held.
    #[serde(default)]
    pub value: Value,
    /// The value that applies when nothing is stored at this scope.
    #[serde(default)]
    pub default_value: Value,
    /// Whether a value is stored at this scope.
    pub is_set: bool,
    /// Changes on every change and is never reused; `0` when nothing is
    /// stored. Send it back as `expected_revision`.
    pub revision: i64,
    /// When the stored value last changed (ISO 8601).
    #[serde(default)]
    pub updated_at: Option<String>,
    /// Who last changed the stored value.
    #[serde(default)]
    pub updated_by: Option<SettingActor>,
    /// Whether the setting holds a secret.
    pub secret: bool,
    /// The value that actually applies at this scope after the setting's own
    /// rules combine it with the scopes around it. `null` when it could not
    /// be resolved.
    #[serde(default)]
    pub effective_value: Value,
    /// Where `effective_value` comes from: one stored value
    /// (`scope`, `scope_id`, `authority`, `qualifier`) or `{"default": true}`.
    /// `None` when several scopes contribute or it could not be resolved.
    #[serde(default)]
    pub effective_source: Option<Value>,
    /// Read-only facts specific to this setting; `null` when it has none.
    #[serde(default)]
    pub detail: Value,
    /// Any additional response fields not modeled above.
    #[serde(flatten)]
    pub extra: serde_json::Map<String, Value>,
}

/// The person or credential that last changed a value.
///
/// `Debug` redacts [`name`](Self::name), a user's email address.
#[derive(Clone, Deserialize)]
#[non_exhaustive]
pub struct SettingActor {
    /// The user id, `admin_api_key` for the platform API key, or
    /// `platform_operator` when a merchant is shown a value the operator
    /// changed.
    pub id: String,
    /// The user's email address, when the actor is a user who still exists.
    #[serde(default)]
    pub name: Option<String>,
    /// Any additional response fields not modeled above.
    #[serde(flatten)]
    pub extra: serde_json::Map<String, Value>,
}

impl std::fmt::Debug for SettingActor {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        f.debug_struct("SettingActor")
            .field("id", &self.id)
            .field("name", &self.name.as_ref().map(|_| "[redacted]"))
            .field("extra", &self.extra)
            .finish()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn an_actor_debug_redacts_the_email() {
        let actor: SettingActor =
            serde_json::from_str(r#"{"id":"usr_1","name":"ada@example.com"}"#).unwrap();
        let rendered = format!("{actor:?}");
        assert!(!rendered.contains("ada@example.com"), "{rendered}");
        assert!(rendered.contains("usr_1"));
    }
}