rabbitmq-backup-core 0.1.0

Core engine for RabbitMQ backup and restore operations
Documentation
//! Definitions export via RabbitMQ Management HTTP API.

use bytes::Bytes;
use tracing::info;

use crate::definitions::management::ManagementClient;
use crate::definitions::types::RabbitMqDefinitions;
use crate::error::Result;

/// Exports RabbitMQ definitions (topology/metadata) via the Management HTTP API.
pub struct DefinitionsExporter {
    client: ManagementClient,
}

impl DefinitionsExporter {
    pub fn new(client: ManagementClient) -> Self {
        Self { client }
    }

    /// Export definitions as a structured type.
    pub async fn export(&self, vhost: Option<&str>) -> Result<RabbitMqDefinitions> {
        let definitions = self.client.export_definitions(vhost).await?;

        info!(
            "Exported definitions: {} users, {} vhosts, {} queues, {} exchanges, {} bindings",
            definitions.users.len(),
            definitions.vhosts.len(),
            definitions.queues.len(),
            definitions.exchanges.len(),
            definitions.bindings.len(),
        );

        Ok(definitions)
    }

    /// Export definitions as JSON bytes (for storage).
    pub async fn export_json(&self, vhost: Option<&str>) -> Result<Bytes> {
        let definitions = self.export(vhost).await?;
        let json = serde_json::to_vec_pretty(&definitions)?;
        Ok(Bytes::from(json))
    }
}