1use crate::Error;
2use crate::Result;
3use gitdag::dag::Set;
4use gitdag::dag::Vertex;
5use gitdag::git2::Oid;
6use std::collections::HashMap;
7
8pub trait OidExt {
10 fn to_vertex(&self) -> Vertex;
12}
13
14pub trait VertexExt {
16 fn to_oid(&self) -> Result<Oid>;
18}
19
20pub trait OidIterExt {
22 fn to_set(self) -> Set;
24}
25
26impl OidExt for Oid {
27 fn to_vertex(&self) -> Vertex {
28 Vertex::copy_from(self.as_bytes())
29 }
30}
31
32impl VertexExt for Vertex {
33 fn to_oid(&self) -> Result<Oid> {
34 Ok(Oid::from_bytes(self.as_ref())?)
35 }
36}
37
38impl<T: IntoIterator<Item = Oid>> OidIterExt for T {
39 fn to_set(self) -> Set {
40 Set::from_static_names(self.into_iter().map(|oid| oid.to_vertex()))
41 }
42}
43
44pub(crate) trait Merge {
45 fn merge(&mut self, other: Self);
46}
47
48impl<K: std::cmp::Eq + std::hash::Hash, V> Merge for HashMap<K, V> {
49 fn merge(&mut self, other: Self) {
50 for (k, v) in other {
51 self.insert(k, v);
52 }
53 }
54}
55
56pub trait SetExt {
58 fn to_oids(&self) -> Result<Box<dyn Iterator<Item = Result<Oid>>>>;
60}
61
62impl SetExt for Set {
63 fn to_oids(&self) -> Result<Box<dyn Iterator<Item = Result<Oid>>>> {
64 let iter = self.iter()?.map(|v| match v {
65 Ok(v) => match Oid::from_bytes(v.as_ref()) {
66 Ok(oid) => Ok(oid),
67 Err(e) => Err(Error::from(e)),
68 },
69 Err(e) => Err(Error::from(e)),
70 });
71 Ok(Box::new(iter))
72 }
73}