ofdb_entities/
url.rs

1//! A replacement of [`url::Url`](https://docs.rs/url/2.2.0/url/struct.Url.html)
2//! in order to reduce WASM file sizes.
3//!
4//! This hopefully will be obsolete as soon as
5//! [rust-url #557](https://github.com/servo/rust-url/issues/557)
6//! is resolved.
7
8use std::{fmt, str::FromStr};
9use thiserror::Error;
10
11#[derive(Debug, Clone, PartialEq, Eq)]
12pub struct Url(String);
13
14impl Url {
15    // https://docs.rs/url/2.2.0/url/struct.Url.html#method.as_str
16    pub fn as_str(&self) -> &str {
17        &self.0
18    }
19    // https://docs.rs/url/2.2.0/url/struct.Url.html#method.into_string
20    pub fn into_string(self) -> String {
21        self.0
22    }
23}
24
25#[derive(Debug, Error)]
26#[error("Unable to parset URL")]
27pub struct ParseError;
28
29impl FromStr for Url {
30    type Err = ParseError;
31    fn from_str(s: &str) -> Result<Self, Self::Err> {
32        // WARNING:
33        // This ignores any checks!!
34        // Use `url::Url::parse` to verify valid URLs
35        Ok(Url(s.to_string()))
36    }
37}
38
39impl fmt::Display for Url {
40    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
41        write!(f, "{}", self.0)
42    }
43}
44
45impl From<Url> for String {
46    fn from(url: Url) -> Self {
47        url.0
48    }
49}