pdk-classy 1.9.1-alpha.2

PDK Classy
Documentation
// Copyright (c) 2026, Salesforce, Inc.,
// All rights reserved.
// For full license text, see the LICENSE.txt file

//! Extractors are used to extract data from the context.

mod already_extracted;
mod from_context;
mod from_context_once;

pub mod config;
pub mod context;

pub use already_extracted::AlreadyExtracted;
pub use from_context::{extractability, Extract, FromContext};
pub use from_context_once::{Exclusive, FromContextOnce};

#[doc(hidden)]
#[macro_export]
macro_rules! context {
    (<$($gen:tt),*> $ctx:ty => $parent:ty {$get:expr}) => {
        $crate::impl_context!([$($gen),*] $ctx => $parent {$get});
    };

    (<$($gen:tt),*> $ctx:ty) => {
        $crate::impl_context!([$($gen),*] $ctx);
    };

    ($ctx:ty => $parent:ty {$get:expr}) => {
        $crate::impl_context!([] $ctx => $parent {$get});
    };

    ($ctx:ty) => {
        $crate::impl_context!([] $ctx);
    };
}

#[doc(hidden)]
#[macro_export]
macro_rules! impl_context {
    ([$($gen:tt),*] $ctx:ty => $parent:ty {$get:expr}) => {
        $crate::impl_context!([$($gen),*] $ctx);

        impl<$($gen,)* T> $crate::extract::FromContext<$ctx, $crate::extract::extractability::Transitive> for T
        where
            T: $crate::extract::FromContext<$parent, $crate::extract::extractability::Transitive>,
        {
            type Error = T::Error;

            fn from_context(context: &$ctx) -> std::result::Result<Self, Self::Error> {
                let get: fn(&$ctx) -> &$parent = $get;
                T::from_context(get(context))
            }
        }
    };

    ([$($gen:tt),*] $ctx:ty) => {
        impl<$($gen,)* T> $crate::extract::FromContext<$ctx> for T
        where
            T: $crate::extract::FromContext<$ctx, $crate::extract::extractability::Transitive>,
        {
            type Error = T::Error;

            fn from_context(context: &$ctx) -> std::result::Result<Self, Self::Error> {
                T::from_context(context)
            }
        }
    };
}