Skip to content

Commit

Permalink
Add BranchRef helpers (#29)
Browse files Browse the repository at this point in the history
For #21
  • Loading branch information
9999years authored Oct 15, 2024
1 parent dee5e77 commit 09d4255
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 0 deletions.
16 changes: 16 additions & 0 deletions src/git/refs/branch.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::fmt::Display;
use std::ops::Deref;
use std::str::FromStr;

use miette::miette;

Expand Down Expand Up @@ -32,6 +33,13 @@ impl BranchRef {
BranchRef::Remote(ref_name) => ref_name.branch_name(),
}
}

pub fn as_local(&self) -> LocalBranchRef {
match self {
BranchRef::Local(local) => local.clone(),
BranchRef::Remote(remote) => remote.as_local(),
}
}
}

impl PartialEq<Ref> for BranchRef {
Expand Down Expand Up @@ -72,6 +80,14 @@ impl TryFrom<Ref> for BranchRef {
}
}

impl FromStr for BranchRef {
type Err = miette::Report;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ref::from_str(s)?.try_into()
}
}

impl From<LocalBranchRef> for BranchRef {
fn from(value: LocalBranchRef) -> Self {
Self::Local(value)
Expand Down
9 changes: 9 additions & 0 deletions src/git/refs/local_branch.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
use std::fmt::Debug;
use std::fmt::Display;
use std::ops::Deref;
use std::str::FromStr;

use miette::miette;

Expand Down Expand Up @@ -58,6 +59,14 @@ impl TryFrom<Ref> for LocalBranchRef {
}
}

impl FromStr for LocalBranchRef {
type Err = miette::Report;

fn from_str(s: &str) -> Result<Self, Self::Err> {
Ref::from_str(s)?.try_into()
}
}

impl<S> From<S> for LocalBranchRef
where
S: AsRef<str>,
Expand Down

0 comments on commit 09d4255

Please sign in to comment.