seedfaker-core 0.4.0-alpha.1

Core library for seedfaker — deterministic synthetic generator for realistic, correlated, and noisy test records
Documentation
use crate::ctx::GenContext;
use crate::rng::Rng;

// Common first-octet ranges per locale for context-aware generation.
// Not exhaustive — just enough for realistic-looking test data.
fn locale_ip_first_octet(rng: &mut Rng, code: &str) -> i64 {
    let octets: &[i64] = match code {
        "en" => &[
            3, 4, 8, 12, 13, 15, 17, 20, 23, 24, 32, 34, 35, 38, 40, 44, 45, 47, 48, 50, 52, 54,
            55, 56, 63, 64, 65, 66, 67, 68, 69, 70, 71, 72, 73, 74, 75, 76, 96, 97, 98, 99, 100,
            104, 107, 108, 128, 129, 130, 131, 132, 134, 135, 136, 137, 138, 140, 142, 143, 144,
            147, 148, 149, 152, 155, 156, 157, 158, 159, 160, 161, 162, 163, 164, 165, 166, 167,
            168, 169, 170, 172, 173, 174, 184, 198, 199, 204, 205, 206, 207, 208, 209, 216,
        ],
        "en-gb" => &[
            2, 5, 25, 31, 51, 62, 78, 81, 82, 86, 90, 109, 141, 146, 151, 176, 178, 185, 193, 194,
            195, 212, 213, 217,
        ],
        "de" | "de-at" => &[
            5, 31, 37, 46, 62, 77, 78, 79, 80, 81, 82, 83, 84, 85, 87, 88, 89, 91, 92, 93, 94, 95,
            109, 141, 145, 176, 178, 185, 188, 193, 194, 195, 212, 213, 217,
        ],
        "fr" | "fr-be" | "fr-ca" => &[
            2, 5, 31, 37, 46, 62, 77, 78, 80, 81, 82, 83, 86, 88, 90, 109, 141, 176, 185, 193, 194,
            195, 212, 213, 217,
        ],
        "ru" => &[
            5, 31, 37, 46, 62, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 89, 90, 91, 92, 93,
            94, 95, 109, 141, 176, 178, 185, 188, 193, 194, 195, 212, 213, 217,
        ],
        "ja" => &[
            1, 14, 27, 36, 42, 49, 58, 59, 60, 61, 101, 106, 110, 111, 112, 113, 114, 115, 116,
            117, 118, 119, 120, 121, 122, 123, 124, 125, 126, 133, 150, 153, 157, 163, 175, 180,
            182, 183, 202, 210, 211, 218, 219, 220, 221,
        ],
        "zh" => &[
            1, 14, 27, 36, 42, 49, 58, 59, 60, 61, 101, 106, 110, 111, 112, 113, 114, 115, 116,
            117, 118, 119, 120, 121, 122, 123, 124, 125, 139, 140, 171, 175, 180, 182, 183, 202,
            210, 211, 218, 219, 220, 221, 222, 223,
        ],
        "hi" => &[
            1, 14, 27, 36, 42, 49, 59, 61, 103, 106, 110, 112, 115, 116, 117, 119, 120, 121, 122,
            123, 124, 125, 150, 157, 175, 180, 182, 183, 202, 210, 218, 219, 220, 221, 223,
        ],
        "ko" => &[
            1, 14, 27, 39, 42, 49, 58, 59, 61, 106, 110, 112, 114, 115, 116, 118, 119, 121, 122,
            125, 175, 180, 182, 183, 203, 210, 211, 218, 219, 220, 221, 222,
        ],
        "pt-br" => {
            &[131, 138, 139, 143, 146, 150, 152, 170, 177, 179, 186, 187, 189, 191, 200, 201]
        }
        "it" => &[
            2, 5, 31, 37, 46, 62, 77, 78, 79, 80, 81, 82, 83, 85, 87, 88, 93, 95, 109, 141, 151,
            176, 185, 193, 194, 195, 212, 213, 217,
        ],
        "es" => &[
            2, 5, 31, 37, 46, 62, 77, 78, 79, 80, 81, 82, 83, 84, 85, 86, 87, 88, 109, 141, 176,
            185, 193, 194, 195, 212, 213, 217,
        ],
        _ => &[],
    };
    if octets.is_empty() {
        rng.range(1, 223)
    } else {
        *rng.choice(octets)
    }
}

// Format: RFC 791 (IPv4) — https://www.rfc-editor.org/rfc/rfc791
pub fn gen(ctx: &mut GenContext<'_>, buf: &mut String) {
    let a = if ctx.identity.is_some() {
        let loc = ctx.locale();
        locale_ip_first_octet(&mut ctx.rng, loc.code)
    } else {
        ctx.rng.range(1, 223)
    };
    let b = ctx.rng.range(0, 255);
    let c = ctx.rng.range(0, 255);
    let d = ctx.rng.range(1, 254);
    let mut b2 = itoa::Buffer::new();
    buf.push_str(b2.format(a));
    buf.push('.');
    buf.push_str(b2.format(b));
    buf.push('.');
    buf.push_str(b2.format(c));
    buf.push('.');
    buf.push_str(b2.format(d));
}