strut_rabbitmq/util/field_table/
retrieve.rs

1use crate::util::Coerce;
2use lapin::types::{AMQPValue, FieldTable};
3
4/// Artificial trait implemented for [`FieldTable`] to allow optional retrieval
5/// of inferred, typed values from the underlying map.
6pub trait Retrieve<'a, T> {
7    /// Optionally retrieves a value of type `T` inferred from the value stored
8    /// under the given `key`, provided the key is set **and** a value of type
9    /// `T` can be inferred from its value.
10    fn retrieve(&'a self, key: &str) -> Option<T>;
11}
12
13/// Implements [`Retrieve`] for every type `T` for which the underlying
14/// [`AMQPValue`] implements [`Coerce`].
15impl<'a, T> Retrieve<'a, T> for FieldTable
16where
17    AMQPValue: Coerce<'a, T>,
18{
19    fn retrieve(&'a self, key: &str) -> Option<T> {
20        self.inner().get(key).and_then(AMQPValue::coerce)
21    }
22}