astro_dnssd/browse.rs
1pub use crate::os::{BrowseError, ServiceBrowser};
2use std::collections::HashMap;
3
4/// Service browsing result type
5pub type Result<T, E = BrowseError> = std::result::Result<T, E>;
6
7/// Type of service event from browser, if a service is being added or removed from network
8#[derive(Debug, Copy, Clone, PartialEq, Eq)]
9pub enum ServiceEventType {
10 /// Service has been added to the network
11 Added,
12 /// Service is removed from the network
13 Removed,
14}
15
16/// Encapsulates information about a service
17#[derive(Debug)]
18pub struct Service {
19 /// Name of service, usually a user friendly name
20 pub name: String,
21 /// Registration type, i.e. _http._tcp.
22 pub regtype: String,
23 /// Interface index (unsure what this is for)
24 pub interface_index: Option<u32>,
25 /// Domain service is on, typically local.
26 pub domain: String,
27 /// Whether this service is being added or not
28 pub event_type: ServiceEventType,
29 // /// Full name of service
30 // pub full_name: String,
31 /// Hostname of service, usable with gethostbyname()
32 pub hostname: String,
33 /// Port service is on
34 pub port: u16,
35 /// TXT record service has if any
36 pub txt_record: Option<HashMap<String, String>>,
37}
38
39/// Builder for creating a browser, allowing optionally specifying a domain with chaining (maybe builder is excessive)
40pub struct ServiceBrowserBuilder {
41 pub(crate) regtype: String,
42 pub(crate) domain: Option<String>,
43}
44
45impl ServiceBrowserBuilder {
46 /// Creates new service browser for given service type, i.e. ._http._tcp
47 pub fn new(regtype: &str) -> ServiceBrowserBuilder {
48 ServiceBrowserBuilder {
49 regtype: String::from(regtype),
50 domain: None,
51 }
52 }
53 /// Adds a specified domain to browser's search
54 pub fn with_domain(mut self, domain: &str) -> ServiceBrowserBuilder {
55 self.domain = Some(String::from(domain));
56 self
57 }
58 /// Starts the browser
59 pub fn browse(self) -> Result<ServiceBrowser> {
60 crate::os::browse(self)
61 }
62}