vstorage 0.6.0

Common API for various icalendar/vcard storages.
Documentation
// Copyright 2023-2026 Hugo Osvaldo Barrera
//
// SPDX-License-Identifier: EUPL-1.2

//! Collection mapping types.

use crate::{CollectionId, Href};

/// Mapping of two collections, resolved based on the storage's current state.
///
/// Identifies which two collections (one from each storage) are mapped to each other.
#[derive(Debug, PartialEq)]
pub struct ResolvedMapping {
    pub(crate) alias: String,
    pub(crate) a: ResolvedCollection,
    pub(crate) b: ResolvedCollection,
}

impl ResolvedMapping {
    /// Returns the alias for this mapping.
    ///
    /// This is only used for logging and in user interfaces.
    #[must_use]
    pub fn alias(&self) -> &str {
        &self.alias
    }

    /// Returns resolved data for the collection on side A.
    #[must_use]
    pub fn a(&self) -> &ResolvedCollection {
        &self.a
    }

    /// Returns resolved data for the collection on side B.
    #[must_use]
    pub fn b(&self) -> &ResolvedCollection {
        &self.b
    }
}

impl std::fmt::Display for ResolvedMapping {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        String::fmt(&self.alias, f)
    }
}

/// Collection as resolved based on existing data.
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct ResolvedCollection {
    pub(crate) id: Option<CollectionId>,
    pub(crate) href: Href,
    pub(crate) exists: bool,
}

impl ResolvedCollection {
    /// The `id` for this collection.
    ///
    /// Is None if collection does not exist AND was declared via an `href`.
    #[must_use]
    pub fn id(&self) -> Option<&CollectionId> {
        self.id.as_ref()
    }

    /// The `href` for this collection.
    ///
    /// If the collection does not exist, this is the intended `href` under which is must be created.
    #[must_use]
    pub fn href(&self) -> &Href {
        &self.href
    }
}