relay_profiling/
utils.rs

1use std::fmt::Display;
2use std::str::FromStr;
3
4use serde::{Deserialize, de};
5use serde_json::{Map, Value};
6
7use crate::types::ClientSdk;
8
9pub fn is_zero(n: &u64) -> bool {
10    *n == 0
11}
12
13pub fn deserialize_number_from_string<'de, T, D>(deserializer: D) -> Result<T, D::Error>
14where
15    D: de::Deserializer<'de>,
16    T: FromStr + Deserialize<'de>,
17    <T as FromStr>::Err: Display,
18{
19    #[derive(Deserialize)]
20    #[serde(untagged)]
21    enum AnyType<T> {
22        Array(Vec<Value>),
23        Bool(bool),
24        Null,
25        Number(T),
26        Object(Map<String, Value>),
27        String(String),
28    }
29
30    match AnyType::<T>::deserialize(deserializer)? {
31        AnyType::String(s) => s.parse::<T>().map_err(serde::de::Error::custom),
32        AnyType::Number(n) => Ok(n),
33        AnyType::Bool(v) => Err(serde::de::Error::custom(format!(
34            "unsupported value: {v:?}"
35        ))),
36        AnyType::Array(v) => Err(serde::de::Error::custom(format!(
37            "unsupported value: {v:?}"
38        ))),
39        AnyType::Object(v) => Err(serde::de::Error::custom(format!(
40            "unsupported value: {v:?}"
41        ))),
42        AnyType::Null => Err(serde::de::Error::custom("unsupported null value")),
43    }
44}
45
46pub fn string_is_null_or_empty(s: &Option<String>) -> bool {
47    s.as_deref().is_none_or(|s| s.is_empty())
48}
49
50pub fn default_client_sdk(platform: &str) -> Option<ClientSdk> {
51    let sdk_name = match platform {
52        "android" => "sentry.java.android",
53        "cocoa" => "sentry.cocoa",
54        "csharp" => "sentry.dotnet",
55        "go" => "sentry.go",
56        "javascript" => "sentry.javascript",
57        "node" => "sentry.javascript.node",
58        "php" => "sentry.php",
59        "python" => "sentry.python",
60        "ruby" => "sentry.ruby",
61        "rust" => "sentry.rust",
62        _ => return None,
63    };
64    Some(ClientSdk {
65        name: sdk_name.into(),
66        version: "".into(),
67    })
68}