gix_object/object/
convert.rs1use crate::{tree, Blob, BlobRef, Commit, CommitRef, Object, ObjectRef, Tag, TagRef, Tree, TreeRef};
2
3impl From<TagRef<'_>> for Tag {
4 fn from(other: TagRef<'_>) -> Tag {
5 let TagRef {
6 target,
7 name,
8 target_kind,
9 message,
10 tagger: signature,
11 pgp_signature,
12 } = other;
13 Tag {
14 target: gix_hash::ObjectId::from_hex(target).expect("prior parser validation"),
15 name: name.to_owned(),
16 target_kind,
17 message: message.to_owned(),
18 tagger: signature.map(Into::into),
19 pgp_signature: pgp_signature.map(ToOwned::to_owned),
20 }
21 }
22}
23
24impl From<CommitRef<'_>> for Commit {
25 fn from(other: CommitRef<'_>) -> Commit {
26 let CommitRef {
27 tree,
28 parents,
29 author,
30 committer,
31 encoding,
32 message,
33 extra_headers,
34 } = other;
35 Commit {
36 tree: gix_hash::ObjectId::from_hex(tree).expect("prior parser validation"),
37 parents: parents
38 .iter()
39 .map(|parent| gix_hash::ObjectId::from_hex(parent).expect("prior parser validation"))
40 .collect(),
41 author: author.into(),
42 committer: committer.into(),
43 encoding: encoding.map(ToOwned::to_owned),
44 message: message.to_owned(),
45 extra_headers: extra_headers
46 .into_iter()
47 .map(|(k, v)| (k.into(), v.into_owned()))
48 .collect(),
49 }
50 }
51}
52
53impl<'a> From<BlobRef<'a>> for Blob {
54 fn from(v: BlobRef<'a>) -> Self {
55 Blob {
56 data: v.data.to_owned(),
57 }
58 }
59}
60
61impl From<TreeRef<'_>> for Tree {
62 fn from(other: TreeRef<'_>) -> Tree {
63 let TreeRef { entries } = other;
64 Tree {
65 entries: entries.into_iter().map(Into::into).collect(),
66 }
67 }
68}
69
70impl From<tree::EntryRef<'_>> for tree::Entry {
71 fn from(other: tree::EntryRef<'_>) -> tree::Entry {
72 let tree::EntryRef { mode, filename, oid } = other;
73 tree::Entry {
74 mode,
75 filename: filename.to_owned(),
76 oid: oid.into(),
77 }
78 }
79}
80
81impl<'a> From<&'a tree::Entry> for tree::EntryRef<'a> {
82 fn from(other: &'a tree::Entry) -> tree::EntryRef<'a> {
83 let tree::Entry { mode, filename, oid } = other;
84 tree::EntryRef {
85 mode: *mode,
86 filename: filename.as_ref(),
87 oid,
88 }
89 }
90}
91
92impl From<ObjectRef<'_>> for Object {
93 fn from(v: ObjectRef<'_>) -> Self {
94 match v {
95 ObjectRef::Tree(v) => Object::Tree(v.into()),
96 ObjectRef::Blob(v) => Object::Blob(v.into()),
97 ObjectRef::Commit(v) => Object::Commit(v.into()),
98 ObjectRef::Tag(v) => Object::Tag(v.into()),
99 }
100 }
101}
102
103impl From<Tag> for Object {
104 fn from(v: Tag) -> Self {
105 Object::Tag(v)
106 }
107}
108
109impl From<Commit> for Object {
110 fn from(v: Commit) -> Self {
111 Object::Commit(v)
112 }
113}
114
115impl From<Tree> for Object {
116 fn from(v: Tree) -> Self {
117 Object::Tree(v)
118 }
119}
120
121impl From<Blob> for Object {
122 fn from(v: Blob) -> Self {
123 Object::Blob(v)
124 }
125}
126
127impl TryFrom<Object> for Tag {
128 type Error = Object;
129
130 fn try_from(value: Object) -> Result<Self, Self::Error> {
131 Ok(match value {
132 Object::Tag(v) => v,
133 _ => return Err(value),
134 })
135 }
136}
137
138impl TryFrom<Object> for Commit {
139 type Error = Object;
140
141 fn try_from(value: Object) -> Result<Self, Self::Error> {
142 Ok(match value {
143 Object::Commit(v) => v,
144 _ => return Err(value),
145 })
146 }
147}
148
149impl TryFrom<Object> for Tree {
150 type Error = Object;
151
152 fn try_from(value: Object) -> Result<Self, Self::Error> {
153 Ok(match value {
154 Object::Tree(v) => v,
155 _ => return Err(value),
156 })
157 }
158}
159
160impl TryFrom<Object> for Blob {
161 type Error = Object;
162
163 fn try_from(value: Object) -> Result<Self, Self::Error> {
164 Ok(match value {
165 Object::Blob(v) => v,
166 _ => return Err(value),
167 })
168 }
169}
170
171impl<'a> From<TagRef<'a>> for ObjectRef<'a> {
172 fn from(v: TagRef<'a>) -> Self {
173 ObjectRef::Tag(v)
174 }
175}
176
177impl<'a> From<CommitRef<'a>> for ObjectRef<'a> {
178 fn from(v: CommitRef<'a>) -> Self {
179 ObjectRef::Commit(v)
180 }
181}
182
183impl<'a> From<TreeRef<'a>> for ObjectRef<'a> {
184 fn from(v: TreeRef<'a>) -> Self {
185 ObjectRef::Tree(v)
186 }
187}
188
189impl<'a> From<BlobRef<'a>> for ObjectRef<'a> {
190 fn from(v: BlobRef<'a>) -> Self {
191 ObjectRef::Blob(v)
192 }
193}
194
195impl<'a> TryFrom<ObjectRef<'a>> for TagRef<'a> {
196 type Error = ObjectRef<'a>;
197
198 fn try_from(value: ObjectRef<'a>) -> Result<Self, Self::Error> {
199 Ok(match value {
200 ObjectRef::Tag(v) => v,
201 _ => return Err(value),
202 })
203 }
204}
205
206impl<'a> TryFrom<ObjectRef<'a>> for CommitRef<'a> {
207 type Error = ObjectRef<'a>;
208
209 fn try_from(value: ObjectRef<'a>) -> Result<Self, Self::Error> {
210 Ok(match value {
211 ObjectRef::Commit(v) => v,
212 _ => return Err(value),
213 })
214 }
215}
216
217impl<'a> TryFrom<ObjectRef<'a>> for TreeRef<'a> {
218 type Error = ObjectRef<'a>;
219
220 fn try_from(value: ObjectRef<'a>) -> Result<Self, Self::Error> {
221 Ok(match value {
222 ObjectRef::Tree(v) => v,
223 _ => return Err(value),
224 })
225 }
226}
227
228impl<'a> TryFrom<ObjectRef<'a>> for BlobRef<'a> {
229 type Error = ObjectRef<'a>;
230
231 fn try_from(value: ObjectRef<'a>) -> Result<Self, Self::Error> {
232 Ok(match value {
233 ObjectRef::Blob(v) => v,
234 _ => return Err(value),
235 })
236 }
237}