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
#![forbid(missing_docs)]
#![doc = include_str!("../Readme.md")]

use std::{
    path::{Path, PathBuf},
    str::FromStr,
};

use url::{ParseError, Url};

pub mod third_party;

/// A path to a resource, either local or remote.
#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
pub struct ResourcePath {
    /// The network path to the resource.
    pub remote: Url,
    /// The local path to the resource.
    pub local: PathBuf,
}

impl ResourcePath {
    /// Create a new resource path to link remote and local object.
    pub fn new<N, L>(remote: N, local: L) -> Result<Self, ParseError>
    where
        N: AsRef<str>,
        L: AsRef<Path>,
    {
        Ok(Self { remote: Url::from_str(remote.as_ref())?, local: local.as_ref().to_path_buf() })
    }
    /// Creates a new resource path.
    pub fn with_local<P: AsRef<Path>>(mut self, local: P) -> Self {
        self.local = local.as_ref().to_path_buf();
        self
    }
    /// Creates a new resource path.
    pub fn with_remote<P: AsRef<str>>(mut self, remote: P) -> Result<Self, ParseError> {
        self.remote = Url::from_str(remote.as_ref())?;
        Ok(self)
    }
}