Added documentation to FromString trait on IpAddr

This commit is contained in:
Luke Else 2023-02-11 16:35:30 +00:00
parent 247587d99b
commit d5ec71a000

View File

@ -32,6 +32,16 @@ impl ToString for IpAddr {
impl FromStr for IpAddr { impl FromStr for IpAddr {
type Err = InvalidIPErr; type Err = InvalidIPErr;
/// Function that generates a IpAddr / Err from a string /// Function that generates a IpAddr / Err from a string
///
/// # Limitation
/// Currently only works for IPs of type IPv4
/// # Example
/// ```
/// let ip: &str = "127.0.0.1";
///
/// let parsed_ip: IpAddr = IpAddr::from_str(ip);
/// ///parsed_ip = IpAddr::V4(127,0,0,1)
/// ```
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
let split_ip = s.split('.'); let split_ip = s.split('.');
if split_ip.clone().count() != 4 { if split_ip.clone().count() != 4 {
@ -86,6 +96,8 @@ mod tests {
#[test] #[test]
#[should_panic] #[should_panic]
///Tests if an invalid Ip will cause a panic when
/// converting from a string to an IpAddr
fn invalid_string_to_ip() { fn invalid_string_to_ip() {
use super::*; use super::*;
let ip = "Hello, world"; let ip = "Hello, world";