git-snip 0.2.0

Snip local Git branches that do not exist on the remote.
Documentation
use std::fmt;

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

impl Remote {
    /// Return remote as prefix. If it does not end in a slash, add one.
    /// e.g. "origin" -> "origin/"
    /// e.g. "origin/" -> "origin/"
    pub fn as_prefix(&self) -> String {
        if self.0.ends_with('/') {
            self.0.clone()
        } else {
            format!("{}/", self.0)
        }
    }

    /// Create a new Remote from anything convertible to String.
    pub fn new<S: Into<String>>(remote: S) -> Self {
        Remote(remote.into())
    }
}

impl fmt::Display for Remote {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        self.0.fmt(f)
    }
}

#[cfg(test)]
mod tests {
    use super::*;

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

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

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

        // WHEN we call as_prefix
        // THEN it should return the remote unchanged
        assert_eq!(remote.as_prefix(), "origin/");
    }

    #[test]
    fn test_display() {
        // GIVEN a remote
        let remote = Remote::new("origin");

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