gix_object/object/
convert.rs

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