resource_path/
lib.rs

1use std::{
2    path::{Path, PathBuf},
3    str::FromStr,
4};
5
6use url::{ParseError, Url};
7
8pub mod third_party;
9
10/// A path to a resource, either local or remote.
11#[derive(Clone, Eq, PartialEq, Ord, PartialOrd, Hash)]
12pub struct ResourcePath {
13    /// The network path to the resource.
14    pub remote: Url,
15    /// The local path to the resource.
16    pub local: PathBuf,
17}
18
19impl ResourcePath {
20    /// Create a new resource path to link remote and local object.
21    pub fn new<N, L>(remote: N, local: L) -> Result<Self, ParseError>
22    where
23        N: AsRef<str>,
24        L: AsRef<Path>,
25    {
26        Ok(Self { remote: Url::from_str(remote.as_ref())?, local: local.as_ref().to_path_buf() })
27    }
28    /// Creates a new resource path.
29    pub fn with_local<P: AsRef<Path>>(mut self, local: P) -> Self {
30        self.local = local.as_ref().to_path_buf();
31        self
32    }
33    /// Creates a new resource path.
34    pub fn with_remote<P: AsRef<str>>(mut self, remote: P) -> Result<Self, ParseError> {
35        self.remote = Url::from_str(remote.as_ref())?;
36        Ok(self)
37    }
38}