1
use std::fmt;
2

            
3
/// This is a wrapper around git2::Remote.
4
#[derive(Debug, Clone, PartialEq, Eq)]
5
pub struct Remote(String);
6

            
7
impl Remote {
8
    /// Return remote as prefix. If it does not end in a slash, add one.
9
    /// e.g. "origin" -> "origin/"
10
    /// e.g. "origin/" -> "origin/"
11
4
    pub fn as_prefix(&self) -> String {
12
4
        if self.0.ends_with('/') {
13
2
            self.0.clone()
14
        } else {
15
2
            format!("{}/", self.0)
16
        }
17
4
    }
18

            
19
    /// Create a new Remote from anything convertible to String.
20
18
    pub fn new<S: Into<String>>(remote: S) -> Self {
21
18
        Remote(remote.into())
22
18
    }
23
}
24

            
25
impl fmt::Display for Remote {
26
2
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
27
2
        self.0.fmt(f)
28
2
    }
29
}
30

            
31
#[cfg(test)]
32
mod tests {
33
    use super::*;
34

            
35
    #[test]
36
2
    fn test_as_prefix_no_slash() {
37
        // GIVEN a remote without a trailing slash
38
2
        let remote = Remote::new("origin");
39

            
40
        // WHEN we call as_prefix
41
        // THEN it should return the remote with a trailing slash
42
2
        assert_eq!(remote.as_prefix(), "origin/");
43
2
    }
44

            
45
    #[test]
46
2
    fn test_as_prefix_with_slash() {
47
        // GIVEN a remote with a trailing slash
48
2
        let remote = Remote::new("origin/");
49

            
50
        // WHEN we call as_prefix
51
        // THEN it should return the remote unchanged
52
2
        assert_eq!(remote.as_prefix(), "origin/");
53
2
    }
54

            
55
    #[test]
56
2
    fn test_display() {
57
        // GIVEN a remote
58
2
        let remote = Remote::new("origin");
59

            
60
        // WHEN we format it
61
        // THEN it should return the remote name as a string
62
2
        assert_eq!(remote.to_string(), "origin");
63
2
    }
64
}