1+ use std:: convert:: TryFrom ;
2+
13use crate :: { tree, Blob , BlobRef , Commit , CommitRef , Object , ObjectRef , Tag , TagRef , Tree , TreeRef } ;
24
3- impl From < TagRef < ' _ > > for Tag {
4- fn from ( other : TagRef < ' _ > ) -> Tag {
5+ impl TryFrom < TagRef < ' _ > > for Tag {
6+ type Error = crate :: decode:: Error ;
7+
8+ fn try_from ( other : TagRef < ' _ > ) -> Result < Tag , Self :: Error > {
59 let TagRef {
610 target,
711 name,
@@ -10,38 +14,42 @@ impl From<TagRef<'_>> for Tag {
1014 tagger,
1115 pgp_signature,
1216 } = other;
13- let tagger = tagger. map ( |raw| {
14- gix_actor:: SignatureRef :: from_bytes :: < ( ) > ( raw. as_ref ( ) )
15- . expect ( "signatures were validated during parsing" )
16- . into ( )
17- } ) ;
18- Tag {
17+ let tagger = tagger
18+ . map ( |raw| {
19+ gix_actor:: SignatureRef :: from_bytes :: < crate :: decode:: ParseError > ( raw. as_ref ( ) )
20+ . map_err ( |err| crate :: decode:: Error :: with_err ( err, raw. as_ref ( ) ) )
21+ } )
22+ . transpose ( ) ?
23+ . map ( Into :: into) ;
24+ Ok ( Tag {
1925 target : gix_hash:: ObjectId :: from_hex ( target) . expect ( "prior parser validation" ) ,
2026 name : name. to_owned ( ) ,
2127 target_kind,
2228 message : message. to_owned ( ) ,
2329 tagger,
2430 pgp_signature : pgp_signature. map ( ToOwned :: to_owned) ,
25- }
31+ } )
2632 }
2733}
2834
29- impl From < CommitRef < ' _ > > for Commit {
30- fn from ( other : CommitRef < ' _ > ) -> Commit {
35+ impl TryFrom < CommitRef < ' _ > > for Commit {
36+ type Error = crate :: decode:: Error ;
37+
38+ fn try_from ( other : CommitRef < ' _ > ) -> Result < Commit , Self :: Error > {
3139 let CommitRef {
3240 tree,
3341 parents,
34- author,
35- committer,
42+ author : author_raw ,
43+ committer : committer_raw ,
3644 encoding,
3745 message,
3846 extra_headers,
3947 } = other;
40- let author = gix_actor:: SignatureRef :: from_bytes :: < ( ) > ( author . as_ref ( ) )
41- . expect ( "signatures were validated during parsing" ) ;
42- let committer = gix_actor:: SignatureRef :: from_bytes :: < ( ) > ( committer . as_ref ( ) )
43- . expect ( "signatures were validated during parsing" ) ;
44- Commit {
48+ let author = gix_actor:: SignatureRef :: from_bytes :: < crate :: decode :: ParseError > ( author_raw . as_ref ( ) )
49+ . map_err ( |err| crate :: decode :: Error :: with_err ( err , author_raw . as_ref ( ) ) ) ? ;
50+ let committer = gix_actor:: SignatureRef :: from_bytes :: < crate :: decode :: ParseError > ( committer_raw . as_ref ( ) )
51+ . map_err ( |err| crate :: decode :: Error :: with_err ( err , committer_raw . as_ref ( ) ) ) ? ;
52+ Ok ( Commit {
4553 tree : gix_hash:: ObjectId :: from_hex ( tree) . expect ( "prior parser validation" ) ,
4654 parents : parents
4755 . iter ( )
@@ -55,7 +63,7 @@ impl From<CommitRef<'_>> for Commit {
5563 . into_iter ( )
5664 . map ( |( k, v) | ( k. into ( ) , v. into_owned ( ) ) )
5765 . collect ( ) ,
58- }
66+ } )
5967 }
6068}
6169
@@ -98,14 +106,16 @@ impl<'a> From<&'a tree::Entry> for tree::EntryRef<'a> {
98106 }
99107}
100108
101- impl From < ObjectRef < ' _ > > for Object {
102- fn from ( v : ObjectRef < ' _ > ) -> Self {
103- match v {
109+ impl TryFrom < ObjectRef < ' _ > > for Object {
110+ type Error = crate :: decode:: Error ;
111+
112+ fn try_from ( v : ObjectRef < ' _ > ) -> Result < Self , Self :: Error > {
113+ Ok ( match v {
104114 ObjectRef :: Tree ( v) => Object :: Tree ( v. into ( ) ) ,
105115 ObjectRef :: Blob ( v) => Object :: Blob ( v. into ( ) ) ,
106- ObjectRef :: Commit ( v) => Object :: Commit ( v . into ( ) ) ,
107- ObjectRef :: Tag ( v) => Object :: Tag ( v . into ( ) ) ,
108- }
116+ ObjectRef :: Commit ( v) => Object :: Commit ( Commit :: try_from ( v ) ? ) ,
117+ ObjectRef :: Tag ( v) => Object :: Tag ( Tag :: try_from ( v ) ? ) ,
118+ } )
109119 }
110120}
111121
0 commit comments