Skip to main content

size_limit_demo/
size_limit_demo.rs

1use ingest::{ingest, IngestConfig, IngestMetadata, IngestPayload, IngestSource, RawIngestRecord};
2
3fn main() {
4    println!("--- Demonstrating Payload Size Limit Policies ---");
5
6    let cfg = IngestConfig {
7        max_payload_bytes: Some(32),
8        max_normalized_bytes: Some(20),
9        ..Default::default()
10    };
11
12    // --- Case 1: Payload within all limits ---
13    println!("\n1. Ingesting a payload that is within all size limits...");
14    let record1 = RawIngestRecord {
15        id: "demo-1-success".into(),
16        source: IngestSource::RawText,
17        metadata: IngestMetadata {
18            tenant_id: None,
19            doc_id: None,
20            received_at: None,
21            original_source: None,
22            attributes: None,
23        },
24        payload: Some(IngestPayload::Text("  valid payload data  ".into())), // Raw: 22 bytes, Norm: 18
25    };
26
27    match ingest(record1, &cfg) {
28        Ok(rec) => println!(
29            " -> Success! Normalized payload: {:?}",
30            rec.normalized_payload.unwrap()
31        ),
32        Err(err) => eprintln!(" -> Unexpected Error: {err}"),
33    }
34
35    // --- Case 2: Raw payload exceeds max_payload_bytes ---
36    println!("\n2. Ingesting a payload that exceeds the raw size limit...");
37    let record2 = RawIngestRecord {
38        id: "demo-2-raw-limit".into(),
39        source: IngestSource::RawText,
40        metadata: IngestMetadata {
41            tenant_id: None,
42            doc_id: None,
43            received_at: None,
44            original_source: None,
45            attributes: None,
46        },
47        payload: Some(IngestPayload::Text(
48            "this raw payload is definitely way too long".into(),
49        )), // 43 bytes
50    };
51
52    match ingest(record2, &cfg) {
53        Ok(_) => eprintln!(" -> Unexpected Success!"),
54        Err(err) => println!(" -> Success! Caught expected error: {err}"),
55    }
56
57    // --- Case 3: Normalized payload exceeds max_normalized_bytes ---
58    println!("\n3. Ingesting a payload that exceeds the normalized size limit...");
59    let record3 = RawIngestRecord {
60        id: "demo-3-norm-limit".into(),
61        source: IngestSource::RawText,
62        metadata: IngestMetadata {
63            tenant_id: None,
64            doc_id: None,
65            received_at: None,
66            original_source: None,
67            attributes: None,
68        },
69        payload: Some(IngestPayload::Text("short raw, but long normalized".into())), // Raw: 30 bytes, Norm: 29
70    };
71
72    match ingest(record3, &cfg) {
73        Ok(_) => eprintln!(" -> Unexpected Success!"),
74        Err(err) => println!(" -> Success! Caught expected error: {err}"),
75    }
76}