git_object/object/
convert.rs

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