sxurl 1.1.0

Fixed-length, sliceable URL identifier system for efficient database storage and querying
Documentation
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
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
//! Tests for SXURL bit packing and format validation.

use sxurl::*;

#[test]
fn test_header_packing() {
    // Test header construction for different scenarios
    struct TestCase {
        scheme: &'static str,
        has_subdomain: bool,
        has_params: bool,
        has_fragment: bool,
        has_custom_port: bool,
        expected_header: &'static str,
    }

    let test_cases = vec![
        TestCase {
            scheme: "https",
            has_subdomain: false,
            has_params: false,
            has_fragment: false,
            has_custom_port: false,
            expected_header: "00", // v2: scheme=0, reserved=0
        },
        TestCase {
            scheme: "http",
            has_subdomain: false,
            has_params: false,
            has_fragment: false,
            has_custom_port: false,
            expected_header: "10", // v2: scheme=1, reserved=0
        },
        TestCase {
            scheme: "ftp",
            has_subdomain: false,
            has_params: false,
            has_fragment: false,
            has_custom_port: false,
            expected_header: "20", // v2: scheme=2, reserved=0
        },
        TestCase {
            scheme: "https",
            has_subdomain: true,
            has_params: false,
            has_fragment: false,
            has_custom_port: false,
            expected_header: "00", // v2: scheme=0, reserved=0 (flags are at [32:34])
        },
        TestCase {
            scheme: "https",
            has_subdomain: false,
            has_params: true,
            has_fragment: false,
            has_custom_port: false,
            expected_header: "00", // v2: scheme=0, reserved=0 (flags are at [32:34])
        },
        TestCase {
            scheme: "https",
            has_subdomain: false,
            has_params: false,
            has_fragment: true,
            has_custom_port: false,
            expected_header: "00", // v2: scheme=0, reserved=0 (flags are at [32:34])
        },
        TestCase {
            scheme: "https",
            has_subdomain: false,
            has_params: false,
            has_fragment: false,
            has_custom_port: true,
            expected_header: "00", // v2: scheme=0, reserved=0 (flags are at [32:34])
        },
    ];

    for test_case in test_cases {
        let mut url = format!("{}://example.com", test_case.scheme);

        if test_case.has_subdomain {
            url = format!("{}://api.example.com", test_case.scheme);
        }

        if test_case.has_custom_port {
            url = format!("{}:8080", url);
        }

        url += "/";

        if test_case.has_params {
            url += "?q=test";
        }

        if test_case.has_fragment {
            url += "#section";
        }

        let normalized = normalize_url(&url).unwrap();
        let components = extract_url_components(&normalized).unwrap();
        let sxurl_bytes = pack_sxurl(&components).unwrap();
        let hex = sxurl_to_hex(&sxurl_bytes);

        assert_eq!(
            &hex[0..2], // v2: check scheme+reserved at [0:2)
            test_case.expected_header,
            "Header mismatch for URL: {} (expected {}, got {})",
            url,
            test_case.expected_header,
            &hex[0..2]
        );
    }
}

#[test]
fn test_port_encoding() {
    // Test that ports are encoded correctly in hex
    let test_cases = vec![
        (80, "0050"),
        (443, "01bb"),
        (8080, "1f90"),
        (8443, "20fb"),
        (3000, "0bb8"),
        (65535, "ffff"),
    ];

    for (port, expected_hex) in test_cases {
        let url = format!("https://example.com:{}/", port);
        let normalized = normalize_url(&url).unwrap();
        let components = extract_url_components(&normalized).unwrap();
        let sxurl_bytes = pack_sxurl(&components).unwrap();
        let hex = sxurl_to_hex(&sxurl_bytes);

        let port_slice = &hex[34..38]; // Correct v2 position for port
        assert_eq!(port_slice, expected_hex, "Port encoding mismatch for port {}", port);
    }
}

#[test]
fn test_hex_slice_boundaries() {
    // Test that all hex slices have correct boundaries and lengths
    let url = "https://api.example.com:8443/search?q=test#results";
    let normalized = normalize_url(url).unwrap();
    let components = extract_url_components(&normalized).unwrap();
    let sxurl_bytes = pack_sxurl(&components).unwrap();
    let hex = sxurl_to_hex(&sxurl_bytes);

    // Test slice boundaries according to spec
    let slices = vec![
        ("Header", 0, 3, 3),
        ("TLD", 3, 7, 4),
        ("Domain", 7, 22, 15),
        ("Subdomain", 22, 30, 8),
        ("Port", 30, 34, 4),
        ("Path", 34, 49, 15),
        ("Params", 49, 58, 9),
        ("Fragment", 58, 64, 6),
    ];

    for (name, start, end, expected_len) in slices {
        let slice = &hex[start..end];
        assert_eq!(
            slice.len(),
            expected_len,
            "{} slice has wrong length: {} (expected {})",
            name,
            slice.len(),
            expected_len
        );

        // All slices should be valid hex
        assert!(
            slice.chars().all(|c| c.is_ascii_hexdigit()),
            "{} slice contains non-hex characters: {}",
            name,
            slice
        );
    }

    // Total length should be exactly 64
    assert_eq!(hex.len(), 64);
}

#[test]
fn test_empty_component_packing() {
    // Test that empty components are packed correctly
    let url = "https://example.com/";
    let normalized = normalize_url(url).unwrap();
    let components = extract_url_components(&normalized).unwrap();

    // Verify components are empty as expected
    assert_eq!(components.subdomain, "");
    assert_eq!(components.query, "");
    assert_eq!(components.fragment, "");

    let sxurl_bytes = pack_sxurl(&components).unwrap();
    let hex = sxurl_to_hex(&sxurl_bytes);

    // Empty subdomain should be zero (not hashed) - correct v2 position [24:32)
    let subdomain_slice = &hex[24..32];
    assert_eq!(subdomain_slice, "00000000");

    // Empty params should be zero (not hashed) - correct v2 position [51:59)
    let params_slice = &hex[51..59];
    assert_eq!(params_slice, "00000000");

    // Empty fragment should be zero (not hashed) - correct v2 position [59:64)
    let fragment_slice = &hex[59..64];
    assert_eq!(fragment_slice, "00000");
}

#[test]
fn test_bit_packing_determinism() {
    // Test that packing is deterministic
    let url = "https://docs.rs/";
    let normalized = normalize_url(url).unwrap();
    let components = extract_url_components(&normalized).unwrap();

    let sxurl1 = pack_sxurl(&components).unwrap();
    let sxurl2 = pack_sxurl(&components).unwrap();

    assert_eq!(sxurl1, sxurl2, "Bit packing should be deterministic");

    let hex1 = sxurl_to_hex(&sxurl1);
    let hex2 = sxurl_to_hex(&sxurl2);

    assert_eq!(hex1, hex2, "Hex conversion should be deterministic");
}

#[test]
fn test_sxurl_length() {
    // Test that SXURL is always exactly 32 bytes / 64 hex chars
    let test_urls = vec![
        "https://a.b/",
        "https://very-long-domain-name.example.org/very/long/path/with/many/segments?very=long&query=parameters&with=multiple&values#very-long-fragment-identifier",
        "http://x.y/",
        "ftp://files.example.com/pub/",
    ];

    for url in test_urls {
        let normalized = normalize_url(url).unwrap();
        let components = extract_url_components(&normalized).unwrap();
        let sxurl_bytes = pack_sxurl(&components).unwrap();
        let hex = sxurl_to_hex(&sxurl_bytes);

        assert_eq!(sxurl_bytes.len(), 32, "SXURL should be exactly 32 bytes for: {}", url);
        assert_eq!(hex.len(), 64, "SXURL hex should be exactly 64 characters for: {}", url);
    }
}

#[test]
fn test_hex_character_validity() {
    // Test that all hex output uses only valid lowercase hex characters
    let url = "https://api.example.com:8443/search?q=test#results";
    let normalized = normalize_url(url).unwrap();
    let components = extract_url_components(&normalized).unwrap();
    let sxurl_bytes = pack_sxurl(&components).unwrap();
    let hex = sxurl_to_hex(&sxurl_bytes);

    for (i, c) in hex.chars().enumerate() {
        assert!(
            c.is_ascii_hexdigit(),
            "Character at position {} is not a hex digit: '{}'",
            i,
            c
        );
    }
}

#[test]
fn test_hex_round_trip() {
    // Test hex_to_sxurl and sxurl_to_hex round trip
    let url = "https://docs.rs/";
    let normalized = normalize_url(url).unwrap();
    let components = extract_url_components(&normalized).unwrap();
    let original_bytes = pack_sxurl(&components).unwrap();

    let hex = sxurl_to_hex(&original_bytes);
    let reconstructed_bytes = hex_to_sxurl(&hex).unwrap();

    assert_eq!(original_bytes, reconstructed_bytes, "Hex round trip should preserve bytes");
}

#[test]
fn test_invalid_hex_input() {
    // Test that invalid hex strings are rejected
    let invalid_hex_strings = vec![
        "123".to_string(),                     // Too short
        "12345".to_string(),                   // Odd length
        "g".to_owned() + &"0".repeat(63),      // Invalid character
        "0".repeat(66),                        // Too long
        "".to_string(),                        // Empty
    ];

    for invalid_hex in &invalid_hex_strings {
        let result = hex_to_sxurl(&invalid_hex);
        assert!(result.is_err(), "Should reject invalid hex: '{}'", invalid_hex);
    }
}

#[test]
fn test_valid_hex_input() {
    // Test that valid hex strings are accepted
    let valid_hex = "1002397f4018b8efa86c310000000001bb98911d784580332000000000000000";
    let result = hex_to_sxurl(valid_hex);
    assert!(result.is_ok(), "Should accept valid hex");

    let bytes = result.unwrap();
    assert_eq!(bytes.len(), 32);

    // Round trip should work
    let hex_again = sxurl_to_hex(&bytes);
    assert_eq!(hex_again, valid_hex);
}

#[test]
fn test_spec_compliance_exact() {
    // Test the exact spec example
    let url = "https://docs.rs/";
    let sxurl_hex = encode_url_to_hex(url).unwrap();
    let expected = "004817c7e16ca8efb818406f000000000001bb35884d71189f90000000000000";

    assert_eq!(sxurl_hex, expected, "Must match spec exactly");

    // Verify each component slice for v2 format with corrected values
    let expected_slices = vec![
        ("Scheme+Reserved", 0, 2, "00"),
        ("TLD", 2, 9, "4817c7e"),
        ("Domain", 9, 24, "16ca8efb818406f"),
        ("Subdomain", 24, 32, "00000000"),
        ("Flags", 32, 34, "00"),
        ("Port", 34, 38, "01bb"),
        ("Path", 38, 51, "35884d71189f9"),
        ("Params", 51, 59, "00000000"),
        ("Fragment", 59, 64, "00000"),
    ];

    for (name, start, end, expected_value) in expected_slices {
        let actual_value = &sxurl_hex[start..end];
        assert_eq!(
            actual_value,
            expected_value,
            "{} slice mismatch: got '{}', expected '{}'",
            name,
            actual_value,
            expected_value
        );
    }
}

#[test]
fn test_flag_bits_exact() {
    // Test exact flag bit patterns
    struct FlagTest {
        url: &'static str,
        expected_flags_binary: &'static str,
        expected_flags_value: u8,
    }

    let flag_tests = vec![
        FlagTest {
            url: "https://example.com/",
            expected_flags_binary: "00000000", // v2: no flags set
            expected_flags_value: 0,
        },
        FlagTest {
            url: "https://api.example.com/",
            expected_flags_binary: "10000000", // v2: bit 7 = subdomain
            expected_flags_value: 128,
        },
        FlagTest {
            url: "https://example.com/?q=test",
            expected_flags_binary: "00010000", // v2: bit 4 = query
            expected_flags_value: 16,
        },
        FlagTest {
            url: "https://example.com/#section",
            expected_flags_binary: "00001000", // v2: bit 3 = fragment
            expected_flags_value: 8,
        },
        FlagTest {
            url: "https://example.com:8080/",
            expected_flags_binary: "01000000", // v2: bit 6 = port
            expected_flags_value: 64,
        },
        FlagTest {
            url: "https://api.example.com:8080/search?q=test#results",
            expected_flags_binary: "11111000", // v2: bits 7,6,5,4,3 = sub,port,path,query,frag
            expected_flags_value: 248,
        },
    ];

    for test in flag_tests {
        let sxurl_hex = encode_url_to_hex(test.url).unwrap();
        let flags_hex = &sxurl_hex[32..34]; // v2: flags are at [32:34]
        let flags = u8::from_str_radix(flags_hex, 16).unwrap();
        assert_eq!(
            flags,
            test.expected_flags_value,
            "Flag value mismatch for {}: expected {}, got {}",
            test.url,
            test.expected_flags_value,
            flags
        );

        // Verify individual flag bits
        let sub_present = (flags & 0x10) != 0;
        let params_present = (flags & 0x08) != 0;
        let frag_present = (flags & 0x04) != 0;
        let port_present = (flags & 0x02) != 0;
        let reserved = (flags & 0x01) != 0;

        assert!(!reserved, "Reserved bit should always be 0 for {}", test.url);

        println!(
            "URL: {} -> Flags: {} (sub:{} params:{} frag:{} port:{})",
            test.url,
            test.expected_flags_binary,
            sub_present,
            params_present,
            frag_present,
            port_present
        );
    }
}