summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorSunil Nimmagadda <sunil@nimmagadda.net>2024-01-10 16:29:32 +0530
committerSunil Nimmagadda <sunil@nimmagadda.net>2024-01-10 16:29:32 +0530
commitd21ab88e43cddad4342f7236094db56d904633a7 (patch)
treeea37746a9f6465baa75bf63d48208d96ef5a87c4 /src
parentab8df74ad7c4308b21690e508fd1cad61a57f8cf (diff)
Recover a lost doc test.
Diffstat (limited to 'src')
-rw-r--r--src/vrrpv2.rs26
1 files changed, 25 insertions, 1 deletions
diff --git a/src/vrrpv2.rs b/src/vrrpv2.rs
index baf69cf..989a36b 100644
--- a/src/vrrpv2.rs
+++ b/src/vrrpv2.rs
@@ -133,7 +133,31 @@ fn checksum(bytes: &[u8]) -> u16 {
}
!(sum as u16)
}
-
+/// Parse and validate a byte array to construct a VRRPv2 struct.
+///
+/// # Examples
+///
+/// ```
+/// use vrrpd::vrrpv2::VRRPv2;
+/// use vrrpd::vrrpv2::VRRPv2AuthType;
+/// use vrrpd::vrrpv2::from_bytes;
+/// use std::net::Ipv4Addr;
+///
+/// let bytes = [
+/// 0x21, 0x01, 0x64, 0x01, 0x00, 0x01, 0xba, 0x52, 0xc0, 0xa8, 0x00, 0x01, 0x00, 0x00, 0x00,
+/// 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x00,
+/// ];
+/// let expected = VRRPv2 {
+/// virtual_router_id: 1,
+/// priority: 100,
+/// count_ip_addrs: 1,
+/// auth_type: VRRPv2AuthType::VRRPv2AuthNoAuth,
+/// checksum: 47698,
+/// advertisement_interval: 1,
+/// ip_addrs: vec![Ipv4Addr::from([192, 168, 0, 1])],
+/// };
+/// assert_eq!(from_bytes(&bytes), Ok(expected));
+/// ```
pub fn from_bytes(bytes: &[u8]) -> Result<VRRPv2, VRRPv2Error> {
let vrrpv2 = parse(bytes)?;
if checksum(bytes) != 0 {