agnostic_mdns/
sync.rs

1use core::convert::Infallible;
2use mdns_proto::proto::{Label, ResourceRecord, ResourceType};
3
4use crate::service::Service;
5
6mod server;
7
8pub use server::{Closer, Server};
9
10/// The interface used to integrate with the server and
11/// to serve records dynamically
12pub trait Zone {
13  /// The error type of the zone
14  type Error: core::error::Error;
15
16  /// Returns the answers for a DNS question.
17  fn answers<'a>(
18    &'a self,
19    name: Label<'a>,
20    rt: ResourceType,
21  ) -> Result<impl Iterator<Item = ResourceRecord<'a>> + 'a, Self::Error>;
22
23  /// Returns the additional records for a DNS question.
24  fn additionals<'a>(
25    &'a self,
26    name: Label<'a>,
27    rt: ResourceType,
28  ) -> Result<impl Iterator<Item = ResourceRecord<'a>> + 'a, Self::Error>;
29}
30
31macro_rules! auto_impl {
32  ($($name:ty),+$(,)?) => {
33    $(
34      impl<Z: Zone> Zone for $name {
35        type Error = Z::Error;
36
37        fn answers<'a>(
38          &'a self,
39          name: Label<'a>,
40          rt: ResourceType,
41        ) -> Result<impl Iterator<Item = ResourceRecord<'a>> + 'a, Self::Error> {
42          (**self).answers(name, rt)
43        }
44
45        fn additionals<'a>(
46          &'a self,
47          name: Label<'a>,
48          rt: ResourceType,
49        ) -> Result<impl Iterator<Item = ResourceRecord<'a>> + 'a, Self::Error> {
50          (**self).additionals(name, rt)
51        }
52      }
53    )*
54  };
55}
56
57auto_impl!(std::sync::Arc<Z>, triomphe::Arc<Z>, std::boxed::Box<Z>,);
58
59impl Zone for Service {
60  type Error = Infallible;
61
62  fn answers<'a>(
63    &'a self,
64    qn: Label<'a>,
65    rt: ResourceType,
66  ) -> Result<impl Iterator<Item = ResourceRecord<'a>> + 'a, Self::Error> {
67    Ok(self.fetch_answers(qn, rt))
68  }
69
70  fn additionals<'a>(
71    &'a self,
72    _: Label<'a>,
73    _: ResourceType,
74  ) -> Result<impl Iterator<Item = ResourceRecord<'a>> + 'a, Self::Error> {
75    Ok(std::iter::empty())
76  }
77}