1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
use crate::property::{Property, SizedProperty};
use crate::typed::tags::{MaybeSizedType, Type};
use crate::typed::{tags, Erased, Tagged};
use core::fmt;
use core::fmt::Write;
use core::marker::PhantomData;
use core::ops::ControlFlow;

pub trait Provider<'a> {
    fn provide(&self, req: &mut Demand<'a>) -> DemandReply<()>;
    fn provide_mut(&mut self, req: &mut Demand<'a>) -> DemandReply<()> {
        req.done()
    }
}

pub trait ProviderExt<'a>: Provider<'a> {
    fn and<P: Provider<'a>>(self, other: P) -> And<Self, P>
    where
        Self: Sized,
    {
        And { l: self, r: other }
    }
}
impl<'a, P: Provider<'a>> ProviderExt<'a> for P {}

#[derive(Debug)]
pub struct EmptyProvider;
impl Provider<'_> for EmptyProvider {
    fn provide(&self, _: &mut Demand<'_>) -> DemandReply<()> {
        DemandReply::Continue(())
    }
}

#[derive(Debug)]
pub struct And<L, R> {
    l: L,
    r: R,
}
impl<'a, L: Provider<'a>, R: Provider<'a>> Provider<'a> for And<L, R> {
    fn provide(&self, req: &mut Demand<'a>) -> DemandReply<()> {
        self.l.provide(req)?;
        self.r.provide(req)
    }

    fn provide_mut(&mut self, req: &mut Demand<'a>) -> DemandReply<()> {
        self.l.provide_mut(req)?;
        self.r.provide_mut(req)
    }
}

#[doc(hidden)]
pub struct Token(PhantomData<()>);

impl fmt::Debug for Token {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str("TOKEN")
    }
}
impl fmt::Display for Token {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_char('_')
    }
}

/// Control-flow utility to help shortcut [`Demand::provide_ref`]/[`Demand::provide_mut`]
///
/// This type allows to easily chain calls while exiting as soon as possible by using
/// [`std::ops::ControlFlow`].
pub type DemandReply<T> = ControlFlow<Token, T>;

struct DemandTag<T>(PhantomData<T>);
impl<'a, T: Property<'a>> MaybeSizedType<'a> for DemandTag<T> {
    type Reified = T::Value;
}
impl<'a, T: SizedProperty<'a>> Type<'a> for DemandTag<T> {
    type Reified = T::Value;
}

#[repr(transparent)]
/// A type-erased demand for a Property
///
/// This struct is used by the [`Provider`] trait to request data from mechanisms that are not
/// necessarily of a `'static` lifetime.
pub struct Demand<'a>(dyn Erased<'a> + 'a);
impl<'a> Demand<'a> {
    pub(crate) fn new<T: tags::Type<'a>>(opt: &mut Tagged<'a, tags::Optional<T>>) -> &'a mut Self {
        unsafe { &mut *(opt as &mut dyn Erased as *mut dyn Erased as *mut Self) }
    }
}
impl<'a> Demand<'a> {
    #[allow(clippy::unused_self)]
    pub const fn done(&self) -> DemandReply<()> {
        DemandReply::Continue(())
    }

    fn provide<T: tags::Type<'a>>(&mut self, value: T::Reified) -> DemandReply<&mut Self> {
        if let Some(res) = self.0.downcast_mut::<tags::Optional<T>>() {
            res.0 = Some(value);
            DemandReply::Break(Token(PhantomData))
        } else {
            DemandReply::Continue(self)
        }
    }

    #[inline(always)]
    pub fn provide_ref<T: Property<'a>>(&mut self, value: &'a T::Value) -> DemandReply<&mut Self> {
        self.provide::<tags::Ref<DemandTag<T>>>(value)
    }

    #[inline(always)]
    pub fn provide_mut<T: Property<'a>>(
        &mut self,
        value: &'a mut T::Value,
    ) -> DemandReply<&mut Self> {
        self.provide::<tags::RefMut<DemandTag<T>>>(value)
    }
}

pub fn build_context<'a>(provider: &'a dyn Provider) -> &'a Context<'a> {
    unsafe { &*(provider as *const dyn Provider as *const Context) }
}

#[repr(transparent)]
/// Strongly typed dynamic value access
///
/// Values a mechanism makes available can be queried using [`get_ref`](Context::get_ref) and
/// [`get_mut`](Context::get_mut). These methods are generic over the [`Property`] and return
/// its associated type [`Property::Value`]. This allows e.g. both [`AuthId`] and [`Realm`] to
/// return values of type `&str`.
///
/// Which values are available depends on the mechanism instance, refer to its documentation for
/// details.
///
/// **Note**: Querying a property that is available for the mechanism but is not set will still
/// return `Some`. So e.g. in a PLAIN exchange `get_ref<Authzid>` will return `Some("")` if no
/// authzid was transmitted.
///
/// This struct thus implements a similar functionality to the (at the time of writing unstable)
/// [provide_any/Provider system](https://doc.rust-lang.org/std/any/trait.Provider.html).
/// The main difference between `Provider` and `Context` is that the latter uses the above
/// mentioned layer of indirection: The generic parameter implementing
/// [`Property<'a>`] specifies the type being returned (i.e. [`Property::Value`]) <br/>
/// [(read more..)](crate::docs::adr::adr0002_context_vs_provide_any)
///
/// [`AuthId`]: crate::property::AuthId
/// [`Realm`]: crate::property::Realm
pub struct Context<'a>(dyn Provider<'a>);
impl<'a> Context<'a> {
    #[inline]
    /// Query the value of a given Property
    ///
    /// This will return `Some(&P::Value)` if the given property is available for the running
    /// mechanism.
    ///
    /// **Note**: this method will also return `Some` if the property is available with the given
    /// mechanism but does not have a value, for example because it is optional. In those cases the
    /// value will be a specific sentinel indicating that fact.
    /// (e.g. `get_ref<Authzid>` returns `Some("")` in `PLAIN` exchanges if no authzid was send by
    /// the client)
    ///
    /// ```no_run
    /// # let context: &rsasl::callback::Context<'_> = unimplemented!();
    /// if let Some("EXAMPLE.COM") = context.get_ref::<rsasl::property::Realm>() {
    ///     // Special handling
    /// }
    /// ```
    pub fn get_ref<P: Property<'a>>(&self) -> Option<&'a P::Value> {
        let mut tagged = Tagged::<'_, tags::Optional<tags::Ref<DemandTag<P>>>>(None);
        self.0.provide(Demand::new(&mut tagged));
        tagged.0
    }

    #[inline]
    /// Request mutable access to the value of a given Property
    ///
    /// This will return `None` if the given property is not available for the running mechanism,
    /// **or** if the mechanism does not allow mutable access to its value.
    pub fn get_mut<P: Property<'a>>(&mut self) -> Option<&'a mut P::Value> {
        let mut tagged = Tagged::<'_, tags::Optional<tags::RefMut<DemandTag<P>>>>(None);
        self.0.provide_mut(Demand::new(&mut tagged));
        tagged.0
    }
}

#[repr(transparent)]
pub struct ThisProvider<'a, P: Property<'a>>(&'a P::Value);
impl<'a, P: Property<'a>> ThisProvider<'a, P> {
    pub const fn with(value: &'a P::Value) -> ThisProvider<'a, P> {
        ThisProvider(value)
    }

    const fn back(&self) -> &'a P::Value {
        self.0
    }
}

impl<'a, P> Provider<'a> for ThisProvider<'a, P>
where
    P: Property<'a>,
{
    fn provide(&self, req: &mut Demand<'a>) -> DemandReply<()> {
        req.provide_ref::<P>(self.back())?.done()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_thisprovider() {
        struct TestTag;
        impl Property<'_> for TestTag {
            type Value = str;
        }
        let value = "hello ";
        let p = ThisProvider::<TestTag>::with(value);
        let value2 = "world!";
        let p2 = ThisProvider::<TestTag>::with(value2);
        let ctx = build_context(&p);
        assert_eq!(ctx.get_ref::<TestTag>().unwrap(), value);
        let ctx2 = build_context(&p2);
        assert_eq!(ctx2.get_ref::<TestTag>().unwrap(), value2);
    }

    static_assertions::assert_obj_safe!(Provider);
}