darkwing/server/utils/
mod.rs

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
//! Utility modules and macros for server-side functionality.

pub mod jwt_utils;
pub mod metrics;
pub mod php_like;

/// Returns the intended release for Sentry as an `Option<Cow<'static, str>>`.
///
/// This can be used with `ClientOptions` to set the release name. It uses
/// the information supplied by cargo and vergen to calculate a release.
///
/// The release name is formatted as:
/// `{package_name}@{version}-{build_timestamp}`
///
/// # Examples
///
/// ```
/// # #[macro_use] extern crate sentry;
/// # fn main() {
/// let _sentry = sentry::init(sentry::ClientOptions {
///     release: sentry::release_name!(),
///     ..Default::default()
/// });
/// # }
/// ```
///
/// # Safety
///
/// This macro uses unsafe code to handle static initialization and string
/// lifetime conversion. The unsafe operations are carefully controlled and only
/// used for static initialization.
#[macro_export]
macro_rules! release_name {
  () => {{
    use std::sync::Once;
    static mut INIT: Once = Once::new();
    static mut RELEASE: Option<String> = None;
    unsafe {
      INIT.call_once(|| {
        RELEASE = option_env!("CARGO_PKG_NAME").and_then(|name| {
          option_env!("CARGO_PKG_VERSION").and_then(|version| {
            option_env!("VERGEN_BUILD_TIMESTAMP")
              .map(|timestamp| format!("{}@{}-{}", name, version, timestamp))
          })
        });
      });
      RELEASE.as_ref().map(|x| {
        let release: &'static str = ::std::mem::transmute(x.as_str());
        ::std::borrow::Cow::Borrowed(release)
      })
    }
  }};
}

/// Unwraps an Option, panicking with `unreachable!()` if the value is None.
///
/// This macro is intended for use in situations where the Option is guaranteed
/// to contain a value based on prior logic, and a None value would indicate
/// a programming error.
///
/// # Examples
///
/// ```
/// # use your_crate_name::unreachable_if_none;
/// let value: Option<i32> = Some(42);
/// let unwrapped = unreachable_if_none!(value);
/// assert_eq!(unwrapped, 42);
/// ```
///
/// # Panics
///
/// Panics with `unreachable!()` if the Option is None.
#[macro_export]
macro_rules! unreachable_if_none {
  ($value:expr) => {
    match $value {
      Some(inner) => inner,
      None => unreachable!(),
    }
  };
}

/// Similar to `unreachable_if_none!`, but clones the Option and its contents
/// before unwrapping.
///
/// This macro is useful when working with references inside Options that need
/// to be cloned.
///
/// # Examples
///
/// ```
/// # use your_crate_name::unreachable_if_none_ref;
/// let value: Option<String> = Some("hello".to_string());
/// let cloned = unreachable_if_none_ref!(value);
/// assert_eq!(cloned, "hello");
/// ```
///
/// # Panics
///
/// Panics with `unreachable!()` if the Option is None.
#[macro_export]
macro_rules! unreachable_if_none_ref {
  ($value:expr) => {
    match $value.clone() {
      Some(ref inner) => inner.clone(),
      None => unreachable!(),
    }
  };
}

/// Unwraps a Result, capturing any error with Sentry and panicking if it's an
/// Err.
///
/// This macro is intended for use in situations where an error is unexpected
/// and should be treated as a critical failure, but should still be reported to
/// Sentry before the program terminates.
///
/// # Examples
///
/// ```
/// # use your_crate_name::unreachable_if_err;
/// let result: Result<i32, &str> = Ok(42);
/// let unwrapped = unreachable_if_err!(result);
/// assert_eq!(unwrapped, 42);
/// ```
///
/// # Panics
///
/// Panics with `unreachable!()` after capturing the error with Sentry if the
/// Result is an Err.
#[macro_export]
macro_rules! unreachable_if_err {
  ($value:expr) => {
    match $value {
      Ok(value) => value,
      Err(err) => {
        sentry::capture_error(&err);
        unreachable!()
      }
    }
  };
}