1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
use crate::{Dummy, Fake, Faker};
use rand::distributions::{Distribution, Uniform};
use rand::Rng;
use std::net::{IpAddr, Ipv4Addr, Ipv6Addr, SocketAddrV4, SocketAddrV6};

impl Dummy<Faker> for Ipv4Addr {
    fn dummy_with_rng<R: Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
        let u = Uniform::new_inclusive(u8::min_value(), u8::max_value());
        Ipv4Addr::new(u.sample(rng), u.sample(rng), u.sample(rng), u.sample(rng))
    }
}

impl Dummy<Faker> for Ipv6Addr {
    fn dummy_with_rng<R: Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
        let u = Uniform::new_inclusive(u16::min_value(), u16::max_value());
        Ipv6Addr::new(
            u.sample(rng),
            u.sample(rng),
            u.sample(rng),
            u.sample(rng),
            u.sample(rng),
            u.sample(rng),
            u.sample(rng),
            u.sample(rng),
        )
    }
}

impl Dummy<Faker> for IpAddr {
    fn dummy_with_rng<R: Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
        if Faker.fake_with_rng::<bool, _>(rng) {
            IpAddr::V4(Faker.fake_with_rng::<Ipv4Addr, _>(rng))
        } else {
            IpAddr::V6(Faker.fake_with_rng::<Ipv6Addr, _>(rng))
        }
    }
}

impl Dummy<Faker> for SocketAddrV4 {
    fn dummy_with_rng<R: Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
        let ip: Ipv4Addr = Faker.fake_with_rng(rng);
        let port: u16 = Faker.fake_with_rng(rng);
        SocketAddrV4::new(ip, port)
    }
}

impl Dummy<Faker> for SocketAddrV6 {
    fn dummy_with_rng<R: Rng + ?Sized>(_: &Faker, rng: &mut R) -> Self {
        let ip: Ipv6Addr = Faker.fake_with_rng(rng);
        let port: u16 = Faker.fake_with_rng(rng);
        let flowinfo: u32 = Faker.fake_with_rng(rng);
        let scope_id: u32 = Faker.fake_with_rng(rng);
        SocketAddrV6::new(ip, port, flowinfo, scope_id)
    }
}