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
// Copyright (c) 2022-2023 Yuki Kishimoto
// Copyright (c) 2023-2024 Rust Nostr Developers
// Distributed under the MIT software license

//! Util

use nostr::url::{ParseError, Url};

/// Try into [`Url`]
pub trait TryIntoUrl {
    /// Error
    type Err;
    /// Try into [`Url`]
    fn try_into_url(&self) -> Result<Url, Self::Err>;
}

impl TryIntoUrl for Url {
    type Err = ParseError;
    fn try_into_url(&self) -> Result<Url, Self::Err> {
        Ok(self.clone())
    }
}

impl TryIntoUrl for &Url {
    type Err = ParseError;
    fn try_into_url(&self) -> Result<Url, Self::Err> {
        Ok(<&Url>::clone(self).clone())
    }
}

impl TryIntoUrl for String {
    type Err = ParseError;
    fn try_into_url(&self) -> Result<Url, Self::Err> {
        Url::parse(self)
    }
}

impl TryIntoUrl for &str {
    type Err = ParseError;
    fn try_into_url(&self) -> Result<Url, Self::Err> {
        Url::parse(self)
    }
}