Skip to content

Commit 9c29ca0

Browse files
committed
Add TryFrom<&str> for &RelativePath
1 parent a2b9719 commit 9c29ca0

File tree

5 files changed

+37
-18
lines changed

5 files changed

+37
-18
lines changed

Diff for: gix-negotiate/tests/baseline/mod.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -71,7 +71,7 @@ fn run() -> crate::Result {
7171
// }
7272
for tip in lookup_names(&["HEAD"]).into_iter().chain(
7373
refs.iter()?
74-
.prefixed(b"refs/heads")?
74+
.prefixed(b"refs/heads".try_into().unwrap())?
7575
.filter_map(Result::ok)
7676
.map(|r| r.target.into_id()),
7777
) {

Diff for: gix-path/src/relative_path.rs

+19
Original file line numberDiff line numberDiff line change
@@ -44,6 +44,25 @@ pub enum Error {
4444
IllegalUtf8(#[from] crate::Utf8Error),
4545
}
4646

47+
impl<'a> TryFrom<&'a str> for &'a RelativePath {
48+
type Error = Error;
49+
50+
fn try_from(value: &'a str) -> Result<Self, Self::Error> {
51+
use std::path::Path;
52+
53+
let path: &std::path::Path = Path::new(value);
54+
let options: Options = Default::default();
55+
56+
for component in path.components() {
57+
let component = os_str_into_bstr(component.as_os_str())?;
58+
59+
gix_validate::path::component(component, None, options)?;
60+
}
61+
62+
RelativePath::new_unchecked(BStr::new(value.as_bytes()))
63+
}
64+
}
65+
4766
impl<'a> TryFrom<&'a BStr> for &'a RelativePath {
4867
type Error = Error;
4968

Diff for: gix-ref/tests/refs/file/store/iter.rs

+13-13
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ mod with_namespace {
2626
let ns_two = gix_ref::namespace::expand("bar")?;
2727
let namespaced_refs = store
2828
.iter()?
29-
.prefixed(ns_two.as_bstr())?
29+
.prefixed(ns_two.as_bstr().try_into().unwrap())?
3030
.map(Result::unwrap)
3131
.map(|r: gix_ref::Reference| r.name)
3232
.collect::<Vec<_>>();
@@ -45,7 +45,7 @@ mod with_namespace {
4545
);
4646
assert_eq!(
4747
store
48-
.loose_iter_prefixed(ns_two.as_bstr())?
48+
.loose_iter_prefixed(ns_two.as_bstr().try_into().unwrap())?
4949
.map(Result::unwrap)
5050
.map(|r| r.name.into_inner())
5151
.collect::<Vec<_>>(),
@@ -90,7 +90,7 @@ mod with_namespace {
9090
assert_eq!(
9191
store
9292
.iter()?
93-
.prefixed(ns_one.as_bstr())?
93+
.prefixed(ns_one.as_bstr().try_into().unwrap())?
9494
.map(Result::unwrap)
9595
.map(|r: gix_ref::Reference| (
9696
r.name.as_bstr().to_owned(),
@@ -324,7 +324,7 @@ fn loose_iter_with_prefix_wont_allow_absolute_paths() -> crate::Result {
324324
#[cfg(windows)]
325325
let abs_path = r"c:\hello";
326326

327-
match store.loose_iter_prefixed(abs_path) {
327+
match store.loose_iter_prefixed(abs_path.try_into().unwrap()) {
328328
Ok(_) => unreachable!("absolute paths aren't allowed"),
329329
Err(err) => assert_eq!(err.to_string(), "prefix must be a relative path, like 'refs/heads/'"),
330330
}
@@ -335,7 +335,7 @@ fn loose_iter_with_prefix_wont_allow_absolute_paths() -> crate::Result {
335335
fn loose_iter_with_prefix() -> crate::Result {
336336
let prefix_with_slash = b"refs/heads/";
337337
let actual = store()?
338-
.loose_iter_prefixed(prefix_with_slash)?
338+
.loose_iter_prefixed(prefix_with_slash.try_into().unwrap())?
339339
.collect::<Result<Vec<_>, _>>()
340340
.expect("no broken ref in this subset")
341341
.into_iter()
@@ -363,7 +363,7 @@ fn loose_iter_with_prefix() -> crate::Result {
363363
fn loose_iter_with_partial_prefix_dir() -> crate::Result {
364364
let prefix_without_slash = b"refs/heads";
365365
let actual = store()?
366-
.loose_iter_prefixed(prefix_without_slash)?
366+
.loose_iter_prefixed(prefix_without_slash.try_into().unwrap())?
367367
.collect::<Result<Vec<_>, _>>()
368368
.expect("no broken ref in this subset")
369369
.into_iter()
@@ -390,7 +390,7 @@ fn loose_iter_with_partial_prefix_dir() -> crate::Result {
390390
#[test]
391391
fn loose_iter_with_partial_prefix() -> crate::Result {
392392
let actual = store()?
393-
.loose_iter_prefixed(b"refs/heads/d".as_bstr())?
393+
.loose_iter_prefixed(b"refs/heads/d".as_bstr().try_into().unwrap())?
394394
.collect::<Result<Vec<_>, _>>()
395395
.expect("no broken ref in this subset")
396396
.into_iter()
@@ -534,7 +534,7 @@ fn overlay_iter_with_prefix_wont_allow_absolute_paths() -> crate::Result {
534534
#[cfg(windows)]
535535
let abs_path = r"c:\hello";
536536

537-
match store.iter()?.prefixed(abs_path) {
537+
match store.iter()?.prefixed(abs_path.try_into().unwrap()) {
538538
Ok(_) => unreachable!("absolute paths aren't allowed"),
539539
Err(err) => assert_eq!(err.to_string(), "prefix must be a relative path, like 'refs/heads/'"),
540540
}
@@ -548,7 +548,7 @@ fn overlay_prefixed_iter() -> crate::Result {
548548
let store = store_at("make_packed_ref_repository_for_overlay.sh")?;
549549
let ref_names = store
550550
.iter()?
551-
.prefixed(b"refs/heads/")?
551+
.prefixed(b"refs/heads/".try_into().unwrap())?
552552
.map(|r| r.map(|r| (r.name.as_bstr().to_owned(), r.target)))
553553
.collect::<Result<Vec<_>, _>>()?;
554554
let c1 = hex_to_id("134385f6d781b7e97062102c6a483440bfda2a03");
@@ -571,7 +571,7 @@ fn overlay_partial_prefix_iter() -> crate::Result {
571571
let store = store_at("make_packed_ref_repository_for_overlay.sh")?;
572572
let ref_names = store
573573
.iter()?
574-
.prefixed(b"refs/heads/m")? // 'm' is partial
574+
.prefixed(b"refs/heads/m".try_into().unwrap())? // 'm' is partial
575575
.map(|r| r.map(|r| (r.name.as_bstr().to_owned(), r.target)))
576576
.collect::<Result<Vec<_>, _>>()?;
577577
let c1 = hex_to_id("134385f6d781b7e97062102c6a483440bfda2a03");
@@ -589,7 +589,7 @@ fn overlay_partial_prefix_iter_reproduce_1934() -> crate::Result {
589589

590590
let ref_names = store
591591
.iter()?
592-
.prefixed(b"refs/d")?
592+
.prefixed(b"refs/d".try_into().unwrap())?
593593
.map(|r| r.map(|r| (r.name.as_bstr().to_owned(), r.target)))
594594
.collect::<Result<Vec<_>, _>>()?;
595595
assert_eq!(
@@ -610,7 +610,7 @@ fn overlay_partial_prefix_iter_when_prefix_is_dir() -> crate::Result {
610610

611611
let ref_names = store
612612
.iter()?
613-
.prefixed(b"refs/prefix/feature")?
613+
.prefixed(b"refs/prefix/feature".try_into().unwrap())?
614614
.map(|r| r.map(|r| (r.name.as_bstr().to_owned(), r.target)))
615615
.collect::<Result<Vec<_>, _>>()?;
616616
assert_eq!(
@@ -623,7 +623,7 @@ fn overlay_partial_prefix_iter_when_prefix_is_dir() -> crate::Result {
623623

624624
let ref_names = store
625625
.iter()?
626-
.prefixed(b"refs/prefix/feature/")?
626+
.prefixed(b"refs/prefix/feature/".try_into().unwrap())?
627627
.map(|r| r.map(|r| (r.name.as_bstr().to_owned(), r.target)))
628628
.collect::<Result<Vec<_>, _>>()?;
629629
assert_eq!(

Diff for: gix-ref/tests/refs/file/worktree.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -290,7 +290,7 @@ mod writable {
290290
assert_eq!(
291291
store
292292
.iter()?
293-
.prefixed(b"refs/stacks/")?
293+
.prefixed(b"refs/stacks/".try_into().unwrap())?
294294
.map(Result::unwrap)
295295
.map(|r| (r.name.to_string(), r.target.to_string()))
296296
.collect::<Vec<_>>(),
@@ -571,7 +571,7 @@ mod writable {
571571
assert_eq!(
572572
store
573573
.iter()?
574-
.prefixed(b"refs/stacks/")?
574+
.prefixed(b"refs/stacks/".try_into().unwrap())?
575575
.map(Result::unwrap)
576576
.map(|r| (r.name.to_string(), r.target.to_string()))
577577
.collect::<Vec<_>>(),

Diff for: gix-ref/tests/refs/namespace.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,13 +3,13 @@ fn into_namespaced_prefix() {
33
assert_eq!(
44
gix_ref::namespace::expand("foo")
55
.unwrap()
6-
.into_namespaced_prefix("prefix"),
6+
.into_namespaced_prefix("prefix".try_into().unwrap()),
77
"refs/namespaces/foo/prefix",
88
);
99
assert_eq!(
1010
gix_ref::namespace::expand("foo")
1111
.unwrap()
12-
.into_namespaced_prefix("prefix/"),
12+
.into_namespaced_prefix("prefix/".try_into().unwrap()),
1313
"refs/namespaces/foo/prefix/",
1414
);
1515
}

0 commit comments

Comments
 (0)