Skip to content

Commit af78db6

Browse files
committed
chore: implement TryFrom for PatchName
1 parent 508192c commit af78db6

File tree

1 file changed

+18
-9
lines changed

1 file changed

+18
-9
lines changed

src/config.rs

Lines changed: 18 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -240,10 +240,18 @@ impl FromStr for BranchName {
240240
/// File name of a patch
241241
#[nutype(
242242
validate(predicate = |p| !p.as_os_str().is_empty()),
243-
derive(Hash, Eq, PartialEq, Debug, AsRef, Deserialize, Clone, FromStr)
243+
derive(Hash, Eq, PartialEq, Debug, AsRef, Deserialize, Clone, FromStr, TryFrom)
244244
)]
245245
pub struct PatchName(PathBuf);
246246

247+
impl TryFrom<&str> for PatchName {
248+
type Error = PatchNameError;
249+
250+
fn try_from(value: &str) -> Result<Self, Self::Error> {
251+
PatchName::try_new(PathBuf::from(value))
252+
}
253+
}
254+
247255
impl Display for PatchName {
248256
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
249257
write!(f, "{}", self.as_ref().display())
@@ -258,8 +266,9 @@ impl Display for PatchName {
258266
)]
259267
pub struct Commit(String);
260268

261-
/// Does not check if the commit hash exists, just checks if it is potentially
262-
/// valid A commit hash can consist of `a-f` and `0-9` characters
269+
/// Does not check if the commit exists, just checks if it is potentially valid
270+
///
271+
/// A commit hash can consist of `a-f` and `0-9` characters
263272
pub fn is_valid_commit_hash(hash: &str) -> bool {
264273
hash.chars().all(|ch| ch.is_ascii_hexdigit())
265274
}
@@ -375,8 +384,8 @@ patches = ['remove-tab']"#;
375384
pretty_assertions::assert_eq!(
376385
conf,
377386
Config {
378-
local_branch: BranchName::try_new("patchy".to_string()).unwrap(),
379-
patches: indexset![PatchName::try_new("remove-tab".into()).unwrap()],
387+
local_branch: "patchy".try_into().unwrap(),
388+
patches: indexset!["remove-tab".try_into().unwrap()],
380389
pull_requests: vec![
381390
PullRequest {
382391
number: 10000.try_into().unwrap(),
@@ -388,17 +397,17 @@ patches = ['remove-tab']"#;
388397
},
389398
PullRequest {
390399
number: 454.try_into().unwrap(),
391-
commit: Some(Commit::try_new("a1b2c3").unwrap())
400+
commit: Some("a1b2c3".try_into().unwrap())
392401
},
393402
PullRequest {
394403
number: 1.try_into().unwrap(),
395-
commit: Some(Commit::try_new("a1b2c3").unwrap())
404+
commit: Some("a1b2c3".try_into().unwrap())
396405
},
397406
],
398407
branches: vec![],
399408
remote_branch: Branch {
400-
name: BranchName::try_new("master").unwrap(),
401-
commit: Some(Commit::try_new("a1b2c4").unwrap())
409+
name: "master".try_into().unwrap(),
410+
commit: Some("a1b2c4".try_into().unwrap())
402411
},
403412
repo: "helix-editor/helix".to_string()
404413
}

0 commit comments

Comments
 (0)