diagnostics_log_validator_utils/
lib.rs

1// Copyright 2024 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5use diagnostics_log_encoding::{Argument, Record, Value};
6use {fidl_fuchsia_diagnostics as fdiagnostics, fidl_fuchsia_validate_logs as fvalidate};
7
8pub fn record_to_fidl(record: Record<'_>) -> fvalidate::Record {
9    fvalidate::Record {
10        timestamp: record.timestamp,
11        severity: fdiagnostics::Severity::from_primitive(record.severity).unwrap(),
12        arguments: record.arguments.into_iter().map(argument_to_fidl).collect(),
13    }
14}
15
16fn argument_to_fidl(argument: Argument<'_>) -> fvalidate::Argument {
17    let name = argument.name().to_string();
18    let value = match argument {
19        Argument::Pid(pid) => fvalidate::Value::UnsignedInt(pid.raw_koid()),
20        Argument::Tid(tid) => fvalidate::Value::UnsignedInt(tid.raw_koid()),
21        Argument::Tag(tag) => fvalidate::Value::Text(tag.to_string()),
22        Argument::Dropped(dropped) => fvalidate::Value::UnsignedInt(dropped),
23        Argument::File(file) => fvalidate::Value::Text(file.to_string()),
24        Argument::Line(line) => fvalidate::Value::UnsignedInt(line),
25        Argument::Message(msg) => fvalidate::Value::Text(msg.to_string()),
26        Argument::Other { value, .. } => value_to_fidl(value),
27    };
28    fvalidate::Argument { name, value }
29}
30
31pub fn value_to_fidl(value: Value<'_>) -> fvalidate::Value {
32    match value {
33        Value::Text(s) => fvalidate::Value::Text(s.to_string()),
34        Value::SignedInt(n) => fvalidate::Value::SignedInt(n),
35        Value::UnsignedInt(n) => fvalidate::Value::UnsignedInt(n),
36        Value::Floating(n) => fvalidate::Value::Floating(n),
37        Value::Boolean(n) => fvalidate::Value::Boolean(n),
38    }
39}
40
41pub fn fidl_to_record(record: fvalidate::Record) -> Record<'static> {
42    Record {
43        timestamp: record.timestamp,
44        severity: record.severity.into_primitive(),
45        arguments: record.arguments.into_iter().map(fidl_to_argument).collect(),
46    }
47}
48
49fn fidl_to_argument(argument: fvalidate::Argument) -> Argument<'static> {
50    let fvalidate::Argument { name, value } = argument;
51    match value {
52        fvalidate::Value::Text(s) => Argument::new(name, s),
53        fvalidate::Value::SignedInt(n) => Argument::new(name, n),
54        fvalidate::Value::UnsignedInt(n) => Argument::new(name, n),
55        fvalidate::Value::Floating(n) => Argument::new(name, n),
56        fvalidate::Value::Boolean(n) => Argument::new(name, n),
57        fvalidate::ValueUnknown!() => {
58            unreachable!("got unknown value which we never set in our tests")
59        }
60    }
61}