sentry_core/
intodsn.rs

1use std::borrow::Cow;
2use std::ffi::{OsStr, OsString};
3
4use crate::types::{Dsn, ParseDsnError};
5
6/// Helper trait to convert a string into an `Option<Dsn>`.
7///
8/// This converts a value into a DSN by parsing.  The empty string or
9/// null values result in no DSN being parsed.
10pub trait IntoDsn {
11    /// Converts the value into a `Result<Option<Dsn>, E>`.
12    fn into_dsn(self) -> Result<Option<Dsn>, ParseDsnError>;
13}
14
15impl<I: IntoDsn> IntoDsn for Option<I> {
16    fn into_dsn(self) -> Result<Option<Dsn>, ParseDsnError> {
17        match self {
18            Some(into_dsn) => into_dsn.into_dsn(),
19            None => Ok(None),
20        }
21    }
22}
23
24impl IntoDsn for () {
25    fn into_dsn(self) -> Result<Option<Dsn>, ParseDsnError> {
26        Ok(None)
27    }
28}
29
30impl IntoDsn for &'_ str {
31    fn into_dsn(self) -> Result<Option<Dsn>, ParseDsnError> {
32        if self.is_empty() {
33            Ok(None)
34        } else {
35            self.parse().map(Some)
36        }
37    }
38}
39
40impl IntoDsn for Cow<'_, str> {
41    fn into_dsn(self) -> Result<Option<Dsn>, ParseDsnError> {
42        let x: &str = &self;
43        x.into_dsn()
44    }
45}
46
47impl IntoDsn for &'_ OsStr {
48    fn into_dsn(self) -> Result<Option<Dsn>, ParseDsnError> {
49        self.to_string_lossy().into_dsn()
50    }
51}
52
53impl IntoDsn for OsString {
54    fn into_dsn(self) -> Result<Option<Dsn>, ParseDsnError> {
55        self.as_os_str().into_dsn()
56    }
57}
58
59impl IntoDsn for String {
60    fn into_dsn(self) -> Result<Option<Dsn>, ParseDsnError> {
61        self.as_str().into_dsn()
62    }
63}
64
65impl IntoDsn for &'_ Dsn {
66    fn into_dsn(self) -> Result<Option<Dsn>, ParseDsnError> {
67        Ok(Some(self.clone()))
68    }
69}
70
71impl IntoDsn for Dsn {
72    fn into_dsn(self) -> Result<Option<Dsn>, ParseDsnError> {
73        Ok(Some(self))
74    }
75}