Skip to content

Commit

Permalink
add doc test example to SocketUri
Browse files Browse the repository at this point in the history
  • Loading branch information
Arian8j2 committed Sep 25, 2024
1 parent 2328e63 commit 8a4d478
Showing 1 changed file with 16 additions and 12 deletions.
28 changes: 16 additions & 12 deletions forwarder/src/socket/uri.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,22 @@ use super::SocketProtocol;
use anyhow::ensure;
use std::{fmt::Display, net::SocketAddr, str::FromStr};

/// # Examples
/// ```
/// use forwarder::socket::{SocketUri, SocketProtocol};
/// use std::{str::FromStr, net::{IpAddr, Ipv4Addr, SocketAddr}};
///
/// let uri = SocketUri::from_str("127.0.0.1:8000/udp")?;
/// assert_eq!(
/// uri.addr,
/// SocketAddr::new(
/// IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)),
/// 8000
/// )
/// );
/// assert_eq!(uri.protocol, SocketProtocol::Udp);
/// # Ok::<(), anyhow::Error>(())
/// ```
#[derive(Clone, Copy, Debug)]
pub struct SocketUri {
pub addr: SocketAddr,
Expand Down Expand Up @@ -55,18 +71,6 @@ impl TryFrom<&str> for SocketUri {
#[cfg(test)]
mod tests {
use super::*;
use std::net::{IpAddr, Ipv4Addr};

#[test]
fn test_udp_uri() {
let input = "127.0.0.1:8000/udp";
let uri = SocketUri::from_str(input).unwrap();
assert_eq!(
uri.addr,
SocketAddr::new(IpAddr::V4(Ipv4Addr::new(127, 0, 0, 1)), 8000)
);
assert_eq!(uri.protocol, SocketProtocol::Udp);
}

#[test]
fn test_invalid_uri_should_fail() {
Expand Down

0 comments on commit 8a4d478

Please sign in to comment.