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
// License: see LICENSE file at root directory of main branch

//! # Remote

use core::{
    cmp::Ordering,
};

/// # Remote
///
/// For sorting, local remotes are placed in front.
#[derive(Debug, Eq, PartialEq, Ord, Hash)]
pub struct Remote {

    name: String,
    url: String,

}

impl Remote {

    /// # Makes new instance
    pub (crate) fn new(name: String, url: String) -> Self {
        Self {
            name,
            url,
        }
    }

    /// # Name
    pub fn name(&self) -> &str {
        &self.name
    }

    /// # URL
    pub fn url(&self) -> &str {
        &self.url
    }

    /// # Checks if this remote is local
    fn is_local(&self) -> bool {
        self.url.starts_with("/") || self.url.starts_with("file:///")
    }

}

impl PartialOrd for Remote {

    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
        let self_is_local = self.is_local();
        let other_is_local = other.is_local();

        let result = if (self_is_local && other_is_local) || (self_is_local == false && other_is_local == false) {
            self.name.to_lowercase().cmp(&other.name.to_lowercase())
        } else if self_is_local {
            Ordering::Less
        } else {
            Ordering::Greater
        };

        Some(result)
    }

}