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
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
use std::path::PathBuf;

/// Stores a location and implements some helpers for discerning where its pointing
/// Support types so far:
///   url
///   git https
///   git ssh
///   path (Windows or Linux)
/// Notes:
///   * This does not check for existence and permissions. Simply stores the location and discerns its type
///   * A git HTTPS URL will also return as a URL
#[derive(serde::Serialize, serde::Deserialize, Debug, Clone)]
pub struct Location {
    /// The raw location
    pub location: String,
}

impl Location {
    pub fn new(loc: &str) -> Self {
        Self {
            location: loc.to_string(),
        }
    }

    pub fn url(&self) -> Option<String> {
        if self.location.starts_with("http://") || self.location.starts_with("https://") {
            Some(self.location.clone())
        } else {
            None
        }
    }

    pub fn git(&self) -> Option<String> {
        if self.git_https().is_some() || self.git_ssh().is_some() {
            Some(self.location.clone())
        } else {
            None
        }
    }

    pub fn git_https(&self) -> Option<String> {
        if self.location.starts_with("https://") && self.location.ends_with(".git") {
            Some(self.location.clone())
        } else {
            None
        }
    }

    pub fn git_ssh(&self) -> Option<String> {
        if self.location.starts_with("git@") && self.location.ends_with(".git") {
            Some(self.location.clone())
        } else {
            None
        }
    }

    pub fn path(&self) -> Option<PathBuf> {
        // Anything else, assume its a path and convert it to a PathBuf
        if self.url().is_none() && self.git().is_none() {
            Some(PathBuf::from(&self.location))
        } else {
            None
        }
    }
}