grow-graylog 0.2.0

Rust SDK for Graylog Open by Grow Codes
Documentation
// Generated by sdk-kit 0.3.0 — do not edit; change sdk-kit and run "sdk-kit sync"
//! Redacting wrapper for credentials (SPEC §2.6).

use std::fmt;

use zeroize::{Zeroize, ZeroizeOnDrop};

use super::redact::REDACTED;

/// A credential such as a token or password.
///
/// `Debug`, `Display` and `Serialize` print `[REDACTED]`; the memory is zeroed on drop.
/// Use [`Secret::expose_secret`] (or its alias [`Secret::reveal`]) to read the value.
#[derive(Clone, Default, Zeroize, ZeroizeOnDrop)]
pub struct Secret(String);

impl Secret {
    /// Wraps a credential.
    pub fn new(value: impl Into<String>) -> Self {
        Secret(value.into())
    }

    /// Returns the wrapped value.
    pub fn expose_secret(&self) -> &str {
        &self.0
    }

    /// Alias of [`Secret::expose_secret`].
    pub fn reveal(&self) -> &str {
        &self.0
    }

    /// Whether the value is empty.
    pub fn is_empty(&self) -> bool {
        self.0.is_empty()
    }
}

impl fmt::Debug for Secret {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(REDACTED)
    }
}

impl fmt::Display for Secret {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(REDACTED)
    }
}

impl serde::Serialize for Secret {
    fn serialize<S: serde::Serializer>(&self, serializer: S) -> Result<S::Ok, S::Error> {
        serializer.serialize_str(REDACTED)
    }
}

impl From<String> for Secret {
    fn from(value: String) -> Self {
        Secret(value)
    }
}

impl From<&str> for Secret {
    fn from(value: &str) -> Self {
        Secret(value.to_owned())
    }
}

impl PartialEq for Secret {
    fn eq(&self, other: &Self) -> bool {
        // Length leaks are acceptable here; the comparison itself is constant time per byte.
        let (a, b) = (self.0.as_bytes(), other.0.as_bytes());
        a.len() == b.len() && a.iter().zip(b).fold(0u8, |acc, (x, y)| acc | (x ^ y)) == 0
    }
}

impl Eq for Secret {}