Skip to main content

deserialize/
deserialize.rs

1use scry_protocol::{FlexBuffersDeserializer, FlatBuffersSerializer, QueryEventBuilder};
2use std::time::Duration;
3
4fn main() {
5    println!("Scry Protocol - Deserialization Example\n");
6
7    // First, create and serialize some events
8    // (In a real scenario, these bytes would come from the network)
9    println!("Creating sample batch...");
10    let events = vec![
11        QueryEventBuilder::new("SELECT * FROM users WHERE id = 1")
12            .connection_id("conn-001")
13            .database("production")
14            .duration(Duration::from_millis(5))
15            .rows(1)
16            .build(),
17        QueryEventBuilder::new("UPDATE users SET last_login = NOW() WHERE id = 1")
18            .connection_id("conn-001")
19            .database("production")
20            .duration(Duration::from_millis(8))
21            .rows(1)
22            .build(),
23        QueryEventBuilder::new("SELECT COUNT(*) FROM orders WHERE user_id = 1")
24            .connection_id("conn-001")
25            .database("production")
26            .duration(Duration::from_millis(12))
27            .rows(1)
28            .build(),
29    ];
30
31    let bytes = FlatBuffersSerializer::serialize_batch(&events, "proxy-demo", 42);
32    println!("Serialized {} events into {} bytes\n", events.len(), bytes.len());
33
34    // Now deserialize the batch
35    println!("Deserializing batch...");
36    match FlexBuffersDeserializer::deserialize_batch(&bytes) {
37        Ok(batch) => {
38            println!("Deserialization successful!\n");
39
40            println!("Batch metadata:");
41            println!("  Proxy ID: {}", batch.proxy_id);
42            println!("  Batch sequence: {}", batch.batch_seq);
43            println!("  Event count: {}\n", batch.events.len());
44
45            println!("Events:");
46            for (i, event) in batch.events.iter().enumerate() {
47                println!("  Event {}:", i + 1);
48                println!("    ID: {}", event.event_id);
49                println!("    Query: {}", event.query);
50                println!("    Database: {}", event.database);
51                println!("    Connection: {}", event.connection_id);
52                println!("    Duration: {:?}", event.duration);
53                println!("    Rows: {:?}", event.rows);
54                println!("    Success: {}", event.success);
55                if let Some(err) = &event.error {
56                    println!("    Error: {}", err);
57                }
58                println!();
59            }
60
61            println!("All events deserialized successfully!");
62        }
63        Err(e) => {
64            eprintln!("Deserialization failed: {}", e);
65            std::process::exit(1);
66        }
67    }
68}