git_shell/git/
remote.rs

1// License: see LICENSE file at root directory of main branch
2
3//! # Remote
4
5use core::{
6    cmp::Ordering,
7};
8
9/// # Remote
10///
11/// For sorting, local remotes are placed in front.
12#[derive(Debug, Eq, PartialEq, Ord, Hash)]
13pub struct Remote {
14
15    name: String,
16    url: String,
17
18}
19
20impl Remote {
21
22    /// # Makes new instance
23    pub (crate) fn new(name: String, url: String) -> Self {
24        Self {
25            name,
26            url,
27        }
28    }
29
30    /// # Name
31    pub fn name(&self) -> &str {
32        &self.name
33    }
34
35    /// # URL
36    pub fn url(&self) -> &str {
37        &self.url
38    }
39
40    /// # Checks if this remote is local
41    fn is_local(&self) -> bool {
42        self.url.starts_with("/") || self.url.starts_with("file:///")
43    }
44
45}
46
47impl PartialOrd for Remote {
48
49    fn partial_cmp(&self, other: &Self) -> Option<Ordering> {
50        let self_is_local = self.is_local();
51        let other_is_local = other.is_local();
52
53        let result = if (self_is_local && other_is_local) || (self_is_local == false && other_is_local == false) {
54            self.name.to_lowercase().cmp(&other.name.to_lowercase())
55        } else if self_is_local {
56            Ordering::Less
57        } else {
58            Ordering::Greater
59        };
60
61        Some(result)
62    }
63
64}