_archivist_c_lib_rustc_static/
lib.rs

1// Copyright 2021 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be found in the LICENSE file.
3
4use diagnostics_message::{self as message, MonikerWithUrl};
5use std::ffi::CString;
6use std::os::raw::c_char;
7
8/// # Safety
9///
10/// Same as for `std::slice::from_raw_parts`. Summarizing in terms of this API:
11///
12/// - `msg` must be valid for reads for `size`, and it must be properly aligned.
13/// - `msg` must point to `size` consecutive u8 values.
14/// - The `size` of the slice must be no larger than `isize::MAX`, and adding
15///   that size to data must not “wrap around” the address space. See the safety
16///   documentation of pointer::offset.
17#[no_mangle]
18pub unsafe extern "C" fn fuchsia_decode_log_message_to_json(
19    msg: *const u8,
20    size: usize,
21) -> *mut c_char {
22    let managed_ptr = std::slice::from_raw_parts(msg, size);
23    let data = &message::from_structured(
24        MonikerWithUrl { moniker: "test_moniker".try_into().unwrap(), url: "".into() },
25        managed_ptr,
26    )
27    .unwrap();
28    let item = serde_json::to_string(&data).unwrap();
29    CString::new(format!("[{}]", item)).unwrap().into_raw()
30}
31
32/// # Safety
33///
34/// This should only be called with a pointer obtained through
35/// `fuchsia_decode_log_message_to_json`.
36#[no_mangle]
37pub unsafe extern "C" fn fuchsia_free_decoded_log_message(msg: *mut c_char) {
38    let str_to_free = CString::from_raw(msg);
39    let _freer = str_to_free;
40}