| 1 |
use std::str::FromStr; |
| 2 |
|
| 3 |
use serde::{Deserialize, Serialize}; |
| 4 |
use zbus::zvariant::Type; |
| 5 |
|
| 6 |
/// A simple URI wrapper that stores the URI as a string with minimal |
| 7 |
/// validation. |
| 8 |
/// |
| 9 |
/// This is a lightweight alternative to `url::Url` for cases where we just need |
| 10 |
/// to store and pass URIs without extensive parsing or manipulation. |
| 11 |
/// |
| 12 |
/// URIs must contain `:` to be valid (e.g., `file:`, `http:`, `https:`). |
| 13 |
/// URIs can't start with `:` to be valid. |
| 14 |
#[derive(Debug, Clone, PartialEq, Eq, Hash, Type)] |
| 15 |
#[zvariant(signature = "s")] |
| 16 |
pub struct Uri(String); |
| 17 |
|
| 18 |
impl Uri { |
| 19 |
/// Parses the URI, doing only two validations: |
| 20 |
/// |
| 21 |
/// - Ensuring it is not an empty string |
| 22 |
/// - That it contains the schema part by checking for `://` |
| 23 |
pub fn parse(uri: &str) -> Result<Self, ParseError> { |
| 24 |
Self::from_str(uri) |
| 25 |
} |
| 26 |
|
| 27 |
/// Get the URI as a string slice. |
| 28 |
pub fn as_str(&self) -> &str { |
| 29 |
&self.0 |
| 30 |
} |
| 31 |
} |
| 32 |
|
| 33 |
impl FromStr for Uri { |
| 34 |
type Err = ParseError; |
| 35 |
|
| 36 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 37 |
if s.is_empty() { |
| 38 |
return Err(ParseError::Empty); |
| 39 |
} |
| 40 |
if !s.contains(":") | s.starts_with(':') { |
| 41 |
return Err(ParseError::MissingScheme); |
| 42 |
} |
| 43 |
Ok(Self(s.to_string())) |
| 44 |
} |
| 45 |
} |
| 46 |
|
| 47 |
impl Serialize for Uri { |
| 48 |
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error> |
| 49 |
where |
| 50 |
S: serde::Serializer, |
| 51 |
{ |
| 52 |
self.as_str().serialize(serializer) |
| 53 |
} |
| 54 |
} |
| 55 |
|
| 56 |
impl<'de> Deserialize<'de> for Uri { |
| 57 |
fn deserialize<D>(deserializer: D) -> Result<Self, D::Error> |
| 58 |
where |
| 59 |
D: serde::Deserializer<'de>, |
| 60 |
{ |
| 61 |
let s = String::deserialize(deserializer)?; |
| 62 |
s.parse() |
| 63 |
.map_err(|e: ParseError| serde::de::Error::custom(e.to_string())) |
| 64 |
} |
| 65 |
} |
| 66 |
|
| 67 |
impl std::fmt::Display for Uri { |
| 68 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 69 |
write!(f, "{}", self.0) |
| 70 |
} |
| 71 |
} |
| 72 |
|
| 73 |
/// Error type for URI parsing. |
| 74 |
#[derive(Debug, Clone, PartialEq, Eq)] |
| 75 |
pub enum ParseError { |
| 76 |
/// The URI string was empty. |
| 77 |
Empty, |
| 78 |
/// The URI is missing a scheme (must contain `://`). |
| 79 |
MissingScheme, |
| 80 |
} |
| 81 |
|
| 82 |
impl std::fmt::Display for ParseError { |
| 83 |
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result { |
| 84 |
match self { |
| 85 |
Self::Empty => write!(f, "URI cannot be empty"), |
| 86 |
Self::MissingScheme => write!(f, "URI must contain a scheme (e.g., file:, http:)"), |
| 87 |
} |
| 88 |
} |
| 89 |
} |
| 90 |
|
| 91 |
impl std::error::Error for ParseError {} |
| 92 |
|
| 93 |
#[cfg(feature = "glib")] |
| 94 |
impl TryFrom<Uri> for glib::Uri { |
| 95 |
type Error = glib::Error; |
| 96 |
|
| 97 |
fn try_from(uri: Uri) -> Result<Self, Self::Error> { |
| 98 |
glib::Uri::parse(uri.as_str(), glib::UriFlags::NONE) |
| 99 |
} |
| 100 |
} |
| 101 |
|
| 102 |
#[cfg(feature = "glib")] |
| 103 |
impl TryFrom<&Uri> for glib::Uri { |
| 104 |
type Error = glib::Error; |
| 105 |
|
| 106 |
fn try_from(uri: &Uri) -> Result<Self, Self::Error> { |
| 107 |
glib::Uri::parse(uri.as_str(), glib::UriFlags::NONE) |
| 108 |
} |
| 109 |
} |