Modern Apps git
git clone https://git.maproj.org/Modern-Apps.git
translate ▾

Modern-Apps.git / third_party / betocore / np_adv / src / credential / source.rs

source.rs84 lines · 3.2 KiBRaw
1// Copyright 2023 Google LLC
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7// http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15//! Definitions of traits for structures which supply
16//! credentials for discovering advertisements/advertisement
17//! sections for a _particular_ protocol version.
18
19use crate::credential::matched::{MatchedCredential, ReferencedMatchedCredential};
20use crate::credential::{DiscoveryMetadataCryptoMaterial, MatchableCredential, ProtocolVersion};
21
22/// A source of credentials for a particular protocol version,
23/// utilizing any [`DiscoveryMetadataCryptoMaterial`] which is usable
24/// for discovering advertisements in that protocol version.
25///
26/// This trait is largely leveraged as a tool for building
27/// new kinds of [`crate::credential::book::CredentialBook`]s
28/// via the [`crate::credential::book::CredentialBookFromSources`]
29/// wrapper.
30///
31/// See [`crate::credential::book::CachedCredentialSource`]
32/// for an example of this pattern.
33pub trait CredentialSource<'a, V: ProtocolVersion>
34where
35 Self: 'a,
36{
37 /// The kind of data yielded to the caller upon a successful
38 /// identity-match.
39 type Matched: MatchedCredential;
40
41 /// The kind of crypto-material yielded from the wrapped
42 /// iterator.
43 type Crypto: DiscoveryMetadataCryptoMaterial<V>;
44
45 /// The iterator type produced which emits credentials.
46 /// This is a lending iterator which may borrow things from `self`.
47 type Iterator: Iterator<Item = (Self::Crypto, Self::Matched)>;
48
49 /// Iterate over the available credentials
50 fn iter(&'a self) -> Self::Iterator;
51}
52
53/// A simple [`CredentialSource`] which iterates over a provided slice of credentials
54pub struct SliceCredentialSource<'c, V: ProtocolVersion, M: MatchedCredential> {
55 credentials: &'c [MatchableCredential<V, M>],
56}
57
58impl<'c, V: ProtocolVersion, M: MatchedCredential> SliceCredentialSource<'c, V, M> {
59 /// Construct the credential supplier from the provided slice of credentials.
60 pub fn new(credentials: &'c [MatchableCredential<V, M>]) -> Self {
61 Self { credentials }
62 }
63}
64
65impl<'a, 'b, V: ProtocolVersion, M: MatchedCredential> CredentialSource<'a, V>
66 for SliceCredentialSource<'b, V, M>
67where
68 'b: 'a,
69 Self: 'b,
70 &'a <V as ProtocolVersion>::DiscoveryCredential: DiscoveryMetadataCryptoMaterial<V>,
71{
72 type Matched = ReferencedMatchedCredential<'a, M>;
73 type Crypto = &'a V::DiscoveryCredential;
74 type Iterator = core::iter::Map<
75 core::slice::Iter<'a, MatchableCredential<V, M>>,
76 fn(
77 &'a MatchableCredential<V, M>,
78 ) -> (&'a V::DiscoveryCredential, ReferencedMatchedCredential<'a, M>),
79 >;
80
81 fn iter(&'a self) -> Self::Iterator {
82 self.credentials.iter().map(MatchableCredential::<V, M>::as_pair)
83 }
84}