1use std::net::SocketAddr;
2use std::time::Duration;
3
4use spvirit_codec::spvd_decode::{DecodedValue, StructureDesc};
5
6#[derive(Clone, Debug)]
8pub struct PvOptions {
9 pub pv_name: String,
10 pub timeout: Duration,
11 pub server_addr: Option<SocketAddr>,
12 pub search_addr: Option<std::net::IpAddr>,
13 pub bind_addr: Option<std::net::IpAddr>,
14 pub name_servers: Vec<SocketAddr>,
15 pub udp_port: u16,
16 pub tcp_port: u16,
17 pub debug: bool,
18 pub no_broadcast: bool,
19 pub authnz_user: Option<String>,
20 pub authnz_host: Option<String>,
21}
22
23pub type PvGetOptions = PvOptions;
25
26impl PvOptions {
27 pub fn new(pv_name: String) -> Self {
28 Self {
29 pv_name,
30 timeout: Duration::from_secs(5),
31 server_addr: None,
32 search_addr: None,
33 bind_addr: None,
34 name_servers: Vec::new(),
35 udp_port: 5076,
36 tcp_port: 5075,
37 debug: false,
38 no_broadcast: false,
39 authnz_user: None,
40 authnz_host: None,
41 }
42 }
43}
44
45#[derive(Debug, Clone)]
47pub struct PvMonitorEvent {
48 pub pv_name: String,
49 pub value: DecodedValue,
50}
51
52#[derive(Debug)]
53pub struct PvGetResult {
54 pub pv_name: String,
55 pub value: DecodedValue,
56 pub raw_pva: Vec<u8>,
57 pub raw_pvd: Vec<u8>,
58 pub introspection: StructureDesc,
59}
60
61#[derive(Debug)]
62pub enum PvGetError {
63 Io(std::io::Error),
64 Timeout(&'static str),
65 Search(&'static str),
66 Protocol(String),
67 Decode(String),
68}
69
70impl std::fmt::Display for PvGetError {
71 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
72 match self {
73 PvGetError::Io(e) => write!(f, "io error: {}", e),
74 PvGetError::Timeout(ctx) => write!(f, "timeout: {}", ctx),
75 PvGetError::Search(ctx) => write!(f, "search error: {}", ctx),
76 PvGetError::Protocol(ctx) => write!(f, "protocol error: {}", ctx),
77 PvGetError::Decode(ctx) => write!(f, "decode error: {}", ctx),
78 }
79 }
80}
81
82impl std::error::Error for PvGetError {}
83
84impl From<std::io::Error> for PvGetError {
85 fn from(value: std::io::Error) -> Self {
86 Self::Io(value)
87 }
88}