radicle_source/object.rs
1// This file is part of radicle-surf
2// <https://github.com/radicle-dev/radicle-surf>
3//
4// Copyright (C) 2019-2020 The Radicle Team <dev@radicle.xyz>
5//
6// This program is free software: you can redistribute it and/or modify
7// it under the terms of the GNU General Public License version 3 or
8// later as published by the Free Software Foundation.
9//
10// This program is distributed in the hope that it will be useful,
11// but WITHOUT ANY WARRANTY; without even the implied warranty of
12// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13// GNU General Public License for more details.
14//
15// You should have received a copy of the GNU General Public License
16// along with this program. If not, see <https://www.gnu.org/licenses/>.
17
18use serde::{
19 ser::{SerializeStruct as _, Serializer},
20 Serialize,
21};
22
23pub mod blob;
24pub use blob::{blob, Blob, BlobContent};
25
26pub mod tree;
27pub use tree::{tree, Tree, TreeEntry};
28
29use crate::commit;
30
31/// Git object types.
32///
33/// `shafiul.github.io/gitbook/1_the_git_object_model.html`
34#[derive(Debug, Eq, Ord, PartialOrd, PartialEq)]
35pub enum ObjectType {
36 /// References a list of other trees and blobs.
37 Tree,
38 /// Used to store file data.
39 Blob,
40}
41
42impl Serialize for ObjectType {
43 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
44 where
45 S: Serializer,
46 {
47 match self {
48 Self::Blob => serializer.serialize_unit_variant("ObjectType", 0, "BLOB"),
49 Self::Tree => serializer.serialize_unit_variant("ObjectType", 1, "TREE"),
50 }
51 }
52}
53
54/// Set of extra information we carry for blob and tree objects returned from
55/// the API.
56pub struct Info {
57 /// Name part of an object.
58 pub name: String,
59 /// The type of the object.
60 pub object_type: ObjectType,
61 /// The last commmit that touched this object.
62 pub last_commit: Option<commit::Header>,
63}
64
65impl Serialize for Info {
66 fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
67 where
68 S: Serializer,
69 {
70 let mut state = serializer.serialize_struct("Info", 3)?;
71 state.serialize_field("name", &self.name)?;
72 state.serialize_field("objectType", &self.object_type)?;
73 state.serialize_field("lastCommit", &self.last_commit)?;
74 state.end()
75 }
76}