serde_buf 0.1.3

Generic buffering for serde
Documentation
#![allow(dead_code)]

use serde_derive::{Deserialize, Serialize};

use serde_buf::Ref;

#[derive(Serialize, Deserialize)]
pub struct OwnedData {
    id: i32,
    title: String,
    attributes: Vec<String>,
}

pub fn owned_data() -> OwnedData {
    OwnedData {
        id: 42,
        title: "A very important document".to_owned(),
        attributes: vec!["#1".to_owned(), "#2".to_owned(), "#3".to_owned()],
    }
}

#[derive(Serialize)]
pub struct BorrowedData<'a> {
    id: i32,
    title: &'a str,
    attributes: &'a [&'a str],
}

pub fn borrowed_data() -> BorrowedData<'static> {
    BorrowedData {
        id: 42,
        title: "A very important document",
        attributes: &["#1", "#2", "#3"],
    }
}

// The same value as `borrowed_data`, built manually through `Ref`'s
// constructors so its strings stay borrowed instead of being copied
pub fn borrowed_data_ref<'a>(data: &BorrowedData<'a>) -> Ref<'a> {
    Ref::record_struct(
        "BorrowedData",
        [
            ("id", Ref::i32(data.id)),
            ("title", Ref::str(data.title)),
            (
                "attributes",
                Ref::seq(data.attributes.iter().map(|a| Ref::str(*a))),
            ),
        ],
    )
}

#[derive(Serialize)]
pub struct ExportLogsServiceRequest<'a> {
    resource_logs: &'a [ResourceLogs<'a>],
}

#[derive(Serialize)]
pub struct ResourceLogs<'a> {
    resource: Option<Resource<'a>>,
    scope_logs: &'a [ScopeLogs<'a>],
    schema_url: &'a str,
}

#[derive(Serialize)]
pub struct Resource<'a> {
    attributes: &'a [KeyValue<'a>],
    dropped_attribute_count: u32,
}

#[derive(Serialize)]
pub struct ScopeLogs<'a> {
    scope: Option<InstrumentationScope<'a>>,
    log_records: &'a [LogRecord<'a>],
    schema_url: &'a str,
}

#[derive(Serialize)]
pub struct InstrumentationScope<'a> {
    name: &'a str,
    version: &'a str,
    attributes: &'a [KeyValue<'a>],
    dropped_attribute_count: u32,
}

#[derive(Serialize)]
pub enum SeverityNumber {
    Unspecified = 0,
    Trace = 1,
    Debug = 5,
    Info = 9,
    Warn = 13,
    Error = 17,
    Fatal = 21,
}

#[derive(Serialize)]
pub enum AnyValue<'a> {
    String(&'a str),
    Bool(bool),
    Int(i64),
    Double(f64),
    Array(&'a [AnyValue<'a>]),
    Kvlist(KvList<'a>),
    Bytes(&'a [u8]),
}

#[derive(Serialize)]
pub struct KvList<'a> {
    values: &'a [KeyValue<'a>],
}

#[derive(Serialize)]
pub struct KeyValue<'a> {
    key: &'a str,
    value: Option<AnyValue<'a>>,
}

#[derive(Serialize)]
pub struct LogRecord<'a> {
    time_unix_nano: u64,
    severity_number: SeverityNumber,
    severity_text: &'a str,
    body: Option<AnyValue<'a>>,
    attributes: &'a [KeyValue<'a>],
    dropped_attributes_count: u32,
    flags: u32,
    trace_id: Option<&'a [u8]>,
    span_id: Option<&'a [u8]>,
    observed_time_unix_nano: u64,
}

pub const fn log_record1_data() -> LogRecord<'static> {
    LogRecord {
        time_unix_nano: 1696310935000000000,
        observed_time_unix_nano: 1696310935000000000,
        severity_number: SeverityNumber::Info,
        severity_text: "Info",
        body: Some(AnyValue::String("Added 1 x product {\"Name\":\"Rocket Ship Dark Roast, Whole Beans\",\"SizeInGrams\":100} to order")),
        attributes: &[
            KeyValue {
                key: "Action",
                value: Some(AnyValue::String("AddItem")),
            },
            KeyValue {
                key: "Controller",
                value: Some(AnyValue::String("OrdersController")),
            },
            KeyValue {
                key: "Application",
                value: Some(AnyValue::String("Roastery Web Frontend")),
            },
            KeyValue {
                key: "OrderId",
                value: Some(AnyValue::String("order-154613823ae87469e1edc0")),
            },
            KeyValue {
                key: "Origin",
                value: Some(AnyValue::String("seqcli sample ingest")),
            },
            KeyValue {
                key: "Product",
                value: Some(AnyValue::Kvlist(KvList {
                    values: &[
                        KeyValue {
                            key: "Name",
                            value: Some(AnyValue::String("Rocket Ship Dark Roast, Whole Beans")),
                        },
                        KeyValue {
                            key: "SizeInGrams",
                            value: Some(AnyValue::Int(100)),
                        },
                    ],
                })),
            },
            KeyValue {
                key: "ProductId",
                value: Some(AnyValue::String("product-8908fd0sa")),
            },
            KeyValue {
                key: "RequestId",
                value: Some(AnyValue::String("b249369fe6c70fa27e4897")),
            },
            KeyValue {
                key: "SourceContext",
                value: Some(AnyValue::String("Roastery.Api.OrdersController")),
            },
        ],
        dropped_attributes_count: 0,
        flags: 0,
        trace_id: None,
        span_id: None,
    }
}

pub const fn log_record2_data() -> LogRecord<'static> {
    LogRecord {
        time_unix_nano: 1696311466000000000,
        observed_time_unix_nano: 1696311466000000000,
        severity_number: SeverityNumber::Debug,
        severity_text: "Debug",
        body: Some(AnyValue::String("Execution of insert into roastery.orderitem (orderid, productid) values ('order-724537b0f2348b2d60aabf', 'product-cvsad9033') returning id; affected 1 rows in 9.241 ms")),
        attributes: &[
            KeyValue {
                key: "Action",
                value: Some(AnyValue::String("AddItem")),
            },
            KeyValue {
                key: "Controller",
                value: Some(AnyValue::String("OrdersController")),
            },
            KeyValue {
                key: "Application",
                value: Some(AnyValue::String("Roastery Web Frontend")),
            },
            KeyValue {
                key: "Elapsed",
                value: Some(AnyValue::Double(9.241)),
            },
            KeyValue {
                key: "OrderId",
                value: Some(AnyValue::String("order-724537b0f2348b2d60aabf")),
            },
            KeyValue {
                key: "Origin",
                value: Some(AnyValue::String("seqcli sample ingest")),
            },
            KeyValue {
                key: "ProductId",
                value: Some(AnyValue::String("product-cvsad9033")),
            },
            KeyValue {
                key: "RequestId",
                value: Some(AnyValue::String("044ca890649f3111d0bddb")),
            },
            KeyValue {
                key: "RowCount",
                value: Some(AnyValue::Int(1)),
            },
            KeyValue {
                key: "SourceContext",
                value: Some(AnyValue::String("Roastery.Data.Database")),
            },
            KeyValue {
                key: "Sql",
                value: Some(AnyValue::String("insert into roastery.orderitem (orderid, productid) values ('order-724537b0f2348b2d60aabf', 'product-cvsad9033') returning id;")),
            },
        ],
        dropped_attributes_count: 0,
        flags: 0,
        trace_id: None,
        span_id: None,
    }
}

pub const fn log_record3_data() -> LogRecord<'static> {
    LogRecord {
        time_unix_nano: 1696311899000000000,
        observed_time_unix_nano: 1696311899000000000,
        severity_number: SeverityNumber::Error,
        severity_text: "Error",
        body: Some(AnyValue::String("HTTP POST /api/orders/order-ad424d996f277c10e38056/items responded 500 in 10.211 ms")),
        attributes: &[
            KeyValue {
                key: "Exception",
                value: Some(AnyValue::String("System.OperationCanceledException: A deadlock was detected and the transaction chosen as the deadlock victim.\n   at Roastery.Data.Database.LogExecAsync(String sql, Int32 rowCount) in /Users/user/dl/osx/seqcli/src/Roastery/Data/Database.cs:line 139\n   at Roastery.Data.Database.SelectAsync[T](Func`2 predicate, String where) in /Users/user/dl/osx/seqcli/src/Roastery/Data/Database.cs:line 55\n   at Roastery.Api.OrdersController.AddItem(HttpRequest request) in /Users/user/dl/osx/seqcli/src/Roastery/Api/OrdersController.cs:line 98\n   at Roastery.Web.Router.InvokeAsync(HttpRequest request) in /Users/user/dl/osx/seqcli/src/Roastery/Web/Router.cs:line 94\n   at Roastery.Web.FaultInjectionMiddleware.InvokeAsync(HttpRequest request) in /Users/user/dl/osx/seqcli/src/Roastery/Web/FaultInjectionMiddleware.cs:line 64\n   at Roastery.Web.SchedulingLatencyMiddleware.InvokeAsync(HttpRequest request) in /Users/user/dl/osx/seqcli/src/Roastery/Web/SchedulingLatencyMiddleware.cs:line 32\n   at Roastery.Web.RequestLoggingMiddleware.InvokeAsync(HttpRequest request) in /Users/user/dl/osx/seqcli/src/Roastery/Web/RequestLoggingMiddleware.cs:line 29")),
            },
            KeyValue {
                key: "Application",
                value: Some(AnyValue::String("Roastery Web Frontend")),
            },
            KeyValue {
                key: "Elapsed",
                value: Some(AnyValue::Double(10.2111)),
            },
            KeyValue {
                key: "Origin",
                value: Some(AnyValue::String("seqcli sample ingest")),
            },
            KeyValue {
                key: "RequestId",
                value: Some(AnyValue::String("4e48b8a4a87cd9ecfb6e37")),
            },
            KeyValue {
                key: "RequestMethod",
                value: Some(AnyValue::String("POST")),
            },
            KeyValue {
                key: "RequestPath",
                value: Some(AnyValue::String("/api/orders/order-ad424d996f277c10e38056/items")),
            },
            KeyValue {
                key: "SourceContext",
                value: Some(AnyValue::String("Roastery.Web.RequestLoggingMiddleware")),
            },
            KeyValue {
                key: "StatusCode",
                value: Some(AnyValue::Int(500)),
            },
        ],
        dropped_attributes_count: 0,
        flags: 0,
        trace_id: None,
        span_id: None,
    }
}

pub fn export_logs_service_request_data() -> ExportLogsServiceRequest<'static> {
    ExportLogsServiceRequest {
        resource_logs: &[ResourceLogs {
            resource: Some(Resource {
                attributes: &[KeyValue {
                    key: "service.name",
                    value: Some(AnyValue::String("sval_protobuf_tests")),
                }],
                dropped_attribute_count: 0,
            }),
            scope_logs: {
                const SCOPE_LOGS: &'static [ScopeLogs<'static>] = &[ScopeLogs {
                    scope: None,
                    log_records: &[log_record1_data(), log_record2_data(), log_record3_data()],
                    schema_url: "",
                }];

                SCOPE_LOGS
            },
            schema_url: "",
        }],
    }
}

// An exponential histogram: a seq of 160 bucket midpoint/count tuples.
// Nothing is borrowed or owned, so this measures pure container and
// primitive encoding
pub fn histogram_data() -> Vec<(f64, u32)> {
    (0..160)
        .map(|i| (2f64.powf(i as f64 / 20.0), i as u32 * 37 % 256))
        .collect()
}

// A map where all keys are small strings, buffered inline through
// `serde`'s copying `serialize_str`
pub fn small_key_map_data() -> std::collections::BTreeMap<String, u64> {
    (0..32).map(|i| (format!("key-{i}"), i as u64)).collect()
}