relay_cabi/
constants.rs

1pub use relay_base_schema::data_category::{CategoryUnit, DataCategory};
2pub use relay_base_schema::events::EventType;
3pub use relay_base_schema::spans::SpanStatus;
4
5use crate::core::RelayStr;
6
7/// Returns the API name of the given `DataCategory`.
8#[unsafe(no_mangle)]
9#[relay_ffi::catch_unwind]
10pub unsafe extern "C" fn relay_data_category_name(category: DataCategory) -> RelayStr {
11    RelayStr::new(category.name())
12}
13
14/// Parses a `DataCategory` from its API name.
15#[unsafe(no_mangle)]
16#[relay_ffi::catch_unwind]
17pub unsafe extern "C" fn relay_data_category_parse(name: *const RelayStr) -> DataCategory {
18    unsafe { (*name).as_str() }
19        .parse()
20        .unwrap_or(DataCategory::Unknown)
21}
22
23/// Parses a `DataCategory` from an event type.
24#[unsafe(no_mangle)]
25#[relay_ffi::catch_unwind]
26pub unsafe extern "C" fn relay_data_category_from_event_type(
27    event_type: *const RelayStr,
28) -> DataCategory {
29    unsafe { (*event_type).as_str() }
30        .parse::<EventType>()
31        .unwrap_or_default()
32        .into()
33}
34
35/// Sentinel value indicating no category unit (used when `Option<CategoryUnit>` is `None`).
36const CATEGORY_UNIT_NONE: i8 = -1;
37
38/// Returns the API name of the given `CategoryUnit`.
39#[unsafe(no_mangle)]
40#[relay_ffi::catch_unwind]
41pub unsafe extern "C" fn relay_category_unit_name(unit: CategoryUnit) -> RelayStr {
42    RelayStr::new(unit.name())
43}
44
45/// Parses a `CategoryUnit` from its API name.
46///
47/// Returns the unit value (0=Count, 1=Bytes, 2=Milliseconds) or `-1` for invalid/unknown names.
48#[unsafe(no_mangle)]
49#[relay_ffi::catch_unwind]
50pub unsafe extern "C" fn relay_category_unit_parse(name: *const RelayStr) -> i8 {
51    let name_str = unsafe { (*name).as_str() };
52    match CategoryUnit::from_name(name_str) {
53        Some(unit) => unit as i8,
54        None => CATEGORY_UNIT_NONE,
55    }
56}
57
58/// Returns the `CategoryUnit` for a given `DataCategory`.
59///
60/// Returns the unit value (0=Count, 1=Bytes, 2=Milliseconds) or `-1` if the category
61/// has no defined unit (e.g., `DataCategory::Unknown`).
62#[unsafe(no_mangle)]
63#[relay_ffi::catch_unwind]
64pub unsafe extern "C" fn relay_data_category_unit(category: DataCategory) -> i8 {
65    match CategoryUnit::from_category(&category) {
66        Some(unit) => unit as i8,
67        None => CATEGORY_UNIT_NONE,
68    }
69}