cdns-rs 1.2.2

A native Sync/Async Rust implementation of client DNS resolver.
Documentation
/*-
 * cdns-rs - a simple sync/async DNS query library.
 * 
 * Copyright (C) 2020  Aleksandr Morozov
 * 
 * Copyright (C) 2025 Aleksandr Morozov
 * 
 * The syslog-rs crate can be redistributed and/or modified
 * under the terms of either of the following licenses:
 *
 *   1. the Mozilla Public License Version 2.0 (the “MPL”) OR
 *                     
 *   2. EUROPEAN UNION PUBLIC LICENCE v. 1.2 EUPL © the European Union 2007, 2016
 */

use std::{fmt, path::Path};

use crate::{a_sync::caches::CacheInstance, cfg_host_parser::HostConfig, ResolveConfig};

/// This trait is a common interface for the ASYNC IO operation and syncronization.
/// 
/// The implentation of this trait is mandatory to provide access to the IO and 
/// synchronization for the crate.
pub trait MutexedCaches: fmt::Debug
{
    /// A type which could be used for the File IO operations. This type must implement
    /// [UnifiedFs] which is a common interface for the required IO operations.
    type MetadataFs: UnifiedFs;

    /// An async mutex realization for the pre-specified type of [CacheInstance] [ResolveConfig].
    /// 
    /// This is generally a [Mutex] implementation whuch is defined in trait [AsyncMutex].
    type ResolveCache: AsyncMutex<CacheInstance<ResolveConfig, Self::MetadataFs>> + fmt::Debug;

    /// An async mutex realization for the pre-specified type of [CacheInstance] [HostConfig].
    /// 
    /// This is generally a [Mutex] implementation whuch is defined in trait [AsyncMutex].
    type HostCahae: AsyncMutex<CacheInstance<HostConfig, Self::MetadataFs>> + fmt::Debug;
}

/// A trait which unifies the `file` IO operations.
#[allow(async_fn_in_trait)]
pub trait UnifiedFs
{
    /// Error type for `metadata` operation.
    type ErrRes: fmt::Display;

    /// A `file` realization struct.
    type FileOp;

    /// Get the metadata for the file specified by the argument `path`.
    async fn metadata(path: &Path) -> Result<std::fs::Metadata, Self::ErrRes>;

    /// Open the file specified by the argument `path` for reading only!
    async fn open<P: AsRef<Path>>(path: P) -> std::io::Result<Self::FileOp>;

    /// Reads from the `file` of type`Self::FileOp` into a [String] buffer a file content.
    async fn read_to_string(file: &mut Self::FileOp, buf: &mut String) -> std::io::Result<usize>;
}

/// A trait which generalize the mutex from the std lib's of multiple async executors.
/// The trait can be implemented on `mutex` directly or on a structure with mutex as 
/// inner type. 
#[allow(async_fn_in_trait)]
pub trait AsyncMutex<DS: Sized>
{
    /// A mutex guard type.
    type MutxGuard<'mux>: AsyncMutexGuard<'mux, DS> where Self: 'mux;

    /// Creates new mutex instance for type which implements the [AsyncSyslogApi].
    fn a_new(v: DS) -> Self;

    /// Locks the mutex emmiting the `mutex guard`.
    async fn a_lock<'mux>(&'mux self) -> Self::MutxGuard<'mux>;
}

/// A trait which generalize the mutex guarding emited by the mutex from various async executors.
pub trait AsyncMutexGuard<'mux, DS: Sized>
{
    /// Returns the reference to the inner type of the mutex guard.
    fn guard(&self) -> &DS;

    /// Returns the mutable reference to the inner type of the mutex guard.
    fn guard_mut(&mut self) -> &mut DS;
}