1use std::path::PathBuf;
2
3use serde::{Deserialize, Serialize};
4
5use crate::error::{Error, Result};
6use crate::id::DeedId;
7
8#[derive(Clone, Debug, PartialEq, Eq, Hash, Serialize, Deserialize)]
10#[serde(rename_all = "camelCase")]
11pub enum Source {
12 Deed(DeedId),
13 Path(PathBuf),
14 Url(String),
15}
16
17impl Source {
18 pub fn url(raw: &str) -> Result<Self> {
19 if !(raw.starts_with("http://")
20 || raw.starts_with("https://")
21 || raw.starts_with("file://"))
22 {
23 return Err(Error::InvalidUrl(raw.into()));
24 }
25 if raw.len() < 8 {
26 return Err(Error::InvalidUrl(raw.into()));
27 }
28 Ok(Self::Url(raw.to_string()))
29 }
30
31 pub fn path(raw: impl Into<PathBuf>) -> Self {
32 Self::Path(raw.into())
33 }
34
35 pub fn deed(id: DeedId) -> Self {
36 Self::Deed(id)
37 }
38}