Skip to content

Commit e703a41

Browse files
committed
Replace From by TryFrom
1 parent e46643e commit e703a41

File tree

8 files changed

+45
-23
lines changed

8 files changed

+45
-23
lines changed

gix-object/src/object/convert.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -72,7 +72,7 @@ impl From<tree::EntryRef<'_>> for tree::Entry {
7272
let tree::EntryRef { mode, filename, oid } = other;
7373
tree::Entry {
7474
mode,
75-
filename: filename.to_owned().into(),
75+
filename: filename.to_owned().try_into().expect("TODO"),
7676
oid: oid.into(),
7777
}
7878
}

gix-object/src/tree/editor.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -33,6 +33,8 @@ pub enum Error {
3333
EmptyPathComponent,
3434
#[error(transparent)]
3535
FindExistingObject(#[from] crate::find::existing_object::Error),
36+
#[error(transparent)]
37+
RepositoryPathBuf(#[from] crate::tree::repository_path_buf::Error),
3638
}
3739

3840
/// Lifecycle
@@ -324,7 +326,7 @@ impl Editor<'_> {
324326
cursor.entries.insert(
325327
insertion_idx,
326328
tree::Entry {
327-
filename: name.into(),
329+
filename: name.try_into()?,
328330
mode: if is_last { kind.into() } else { EntryKind::Tree.into() },
329331
oid: if is_last { id } else { id.kind().null() },
330332
},

gix-object/src/tree/mod.rs

+29-9
Original file line numberDiff line numberDiff line change
@@ -378,21 +378,41 @@ impl std::ops::Index<std::ops::RangeTo<usize>> for RepositoryPathPuf {
378378
}
379379
}
380380

381-
impl From<&str> for RepositoryPathPuf {
382-
fn from(value: &str) -> Self {
383-
Self { inner: value.into() }
381+
///
382+
pub mod repository_path_buf {
383+
/// The error used in [`RepositoryPathPuf`](super::RepositoryPathPuf).
384+
#[derive(Debug, thiserror::Error)]
385+
#[allow(missing_docs)]
386+
pub enum Error {}
387+
}
388+
389+
impl TryFrom<&str> for RepositoryPathPuf {
390+
type Error = repository_path_buf::Error;
391+
392+
fn try_from(value: &str) -> Result<Self, Self::Error> {
393+
// TODO
394+
// Check whether the documented constraints are met, return `Err` if not.
395+
Ok(Self { inner: value.into() })
384396
}
385397
}
386398

387-
impl From<&BStr> for RepositoryPathPuf {
388-
fn from(value: &BStr) -> Self {
389-
Self { inner: value.into() }
399+
impl TryFrom<&BStr> for RepositoryPathPuf {
400+
type Error = repository_path_buf::Error;
401+
402+
fn try_from(value: &BStr) -> Result<Self, Self::Error> {
403+
// TODO
404+
// Check whether the documented constraints are met, return `Err` if not.
405+
Ok(Self { inner: value.into() })
390406
}
391407
}
392408

393-
impl From<BString> for RepositoryPathPuf {
394-
fn from(value: BString) -> Self {
395-
Self { inner: value }
409+
impl TryFrom<BString> for RepositoryPathPuf {
410+
type Error = repository_path_buf::Error;
411+
412+
fn try_from(value: BString) -> Result<Self, Self::Error> {
413+
// TODO
414+
// Check whether the documented constraints are met, return `Err` if not.
415+
Ok(Self { inner: value })
396416
}
397417
}
398418

gix-object/tests/object/encode/mod.rs

+3-3
Original file line numberDiff line numberDiff line change
@@ -111,12 +111,12 @@ mod tree {
111111
let mut tree = gix_object::Tree::empty();
112112
tree.entries.push(tree::Entry {
113113
mode: EntryKind::Blob.into(),
114-
filename: "".into(),
114+
filename: "".try_into().expect("TODO"),
115115
oid: gix_hash::Kind::Sha1.null(),
116116
});
117117
tree.entries.push(tree::Entry {
118118
mode: EntryKind::Tree.into(),
119-
filename: "something\nwith\newlines\n".into(),
119+
filename: "something\nwith\newlines\n".try_into().expect("TODO"),
120120
oid: gix_hash::ObjectId::empty_tree(gix_hash::Kind::Sha1),
121121
});
122122
tree.write_to(&mut std::io::sink())
@@ -128,7 +128,7 @@ mod tree {
128128
let mut tree = gix_object::Tree::empty();
129129
tree.entries.push(tree::Entry {
130130
mode: EntryKind::Blob.into(),
131-
filename: "hi\0ho".into(),
131+
filename: "hi\0ho".try_into().expect("TODO"),
132132
oid: gix_hash::Kind::Sha1.null(),
133133
});
134134
let err = tree.write_to(&mut std::io::sink()).unwrap_err();

gix-object/tests/object/tree/editor.rs

+5-5
Original file line numberDiff line numberDiff line change
@@ -41,7 +41,7 @@ fn from_empty_cursor() -> crate::Result {
4141
cursor.get(Some("empty-dir-via-cursor")),
4242
Some(&Entry {
4343
mode: EntryKind::Tree.into(),
44-
filename: "empty-dir-via-cursor".into(),
44+
filename: "empty-dir-via-cursor".try_into().expect("TODO"),
4545
oid: gix_hash::ObjectId::empty_tree(gix_hash::Kind::Sha1),
4646
}),
4747
);
@@ -69,7 +69,7 @@ fn from_empty_cursor() -> crate::Result {
6969
edit.get(["some", "deeply", "nested", "path"]),
7070
Some(&Entry {
7171
mode: EntryKind::Tree.into(),
72-
filename: "path".into(),
72+
filename: "path".try_into().expect("TODO"),
7373
oid: gix_hash::Kind::Sha1.null(),
7474
}),
7575
"the directory leading to the removed one is still present"
@@ -470,7 +470,7 @@ fn from_empty_add() -> crate::Result {
470470
edit.get(["a", "b"]),
471471
Some(&Entry {
472472
mode: EntryKind::Tree.into(),
473-
filename: "b".into(),
473+
filename: "b".try_into().expect("TODO"),
474474
oid: hex_to_id("4b825dc642cb6eb9a060e54bf8d69288fbee4904"),
475475
}),
476476
"before writing, entries are still present, just like they were written"
@@ -479,7 +479,7 @@ fn from_empty_add() -> crate::Result {
479479
edit.get(["a", "b", "c"]),
480480
Some(&Entry {
481481
mode: EntryKind::Tree.into(),
482-
filename: "c".into(),
482+
filename: "c".try_into().expect("TODO"),
483483
oid: gix_hash::ObjectId::empty_tree(gix_hash::Kind::Sha1),
484484
}),
485485
);
@@ -503,7 +503,7 @@ fn from_empty_add() -> crate::Result {
503503
edit.get(Some("a")),
504504
Some(&Entry {
505505
mode: EntryKind::Tree.into(),
506-
filename: "a".into(),
506+
filename: "a".try_into().expect("TODO"),
507507
oid: hex_to_id("850bf83c26003cb0541318718bc9217c4a5bde6d"),
508508
}),
509509
"but the top-level tree is still available and can yield its entries, as written with proper ids"

gix-object/tests/object/tree/iter.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -124,7 +124,7 @@ mod lookup_entry {
124124
pub(super) fn entry(filename: &str, mode: tree::EntryKind, oid: gix_hash::ObjectId) -> Option<tree::Entry> {
125125
Some(tree::Entry {
126126
mode: mode.into(),
127-
filename: filename.into(),
127+
filename: filename.try_into().ok()?,
128128
oid,
129129
})
130130
}

gix-odb/tests/odb/memory.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ fn without_memory() -> crate::Result {
1919
let mut tree = tree.to_owned();
2020
tree.entries.push(tree::Entry {
2121
mode: tree::EntryKind::Blob.into(),
22-
filename: "z-for-sorting_another-file-with-same-content".into(),
22+
filename: "z-for-sorting_another-file-with-same-content".try_into().expect("TODO"),
2323
oid: existing,
2424
});
2525
let new_tree_id = odb.write(&tree)?;
@@ -62,7 +62,7 @@ fn with_memory() -> crate::Result {
6262
let mut tree = tree.to_owned();
6363
tree.entries.push(tree::Entry {
6464
mode: tree::EntryKind::Blob.into(),
65-
filename: "z-for-sorting_another-file-with-same-content".into(),
65+
filename: "z-for-sorting_another-file-with-same-content".try_into().expect("TODO"),
6666
oid: existing,
6767
});
6868
let new_tree_id = odb.write(&tree)?;

gix/examples/init-repo-and-commit.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -32,7 +32,7 @@ fn main() -> anyhow::Result<()> {
3232
let entry = tree::Entry {
3333
mode: tree::EntryKind::Blob.into(),
3434
oid: blob_id,
35-
filename: "hello.txt".into(),
35+
filename: "hello.txt".try_into()?,
3636
};
3737

3838
tree.entries.push(entry);

0 commit comments

Comments
 (0)