Enum git_object::Kind

source ·
pub enum Kind {
    Tree,
    Blob,
    Commit,
    Tag,
}
Expand description

The four types of objects that git differentiates. #[derive(PartialEq, Eq, Debug, Hash, Ord, PartialOrd, Clone, Copy)]

Variants§

§

Tree

§

Blob

§

Commit

§

Tag

Implementations§

Parse a Kind from its serialized loose git objects.

Examples found in repository?
src/lib.rs (line 365)
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
    pub fn loose_header(input: &[u8]) -> Result<(super::Kind, usize, usize), LooseHeaderDecodeError> {
        use LooseHeaderDecodeError::*;
        let kind_end = input.find_byte(0x20).ok_or(InvalidHeader {
            message: "Expected '<type> <size>'",
        })?;
        let kind = super::Kind::from_bytes(&input[..kind_end])?;
        let size_end = input.find_byte(0x0).ok_or(InvalidHeader {
            message: "Did not find 0 byte in header",
        })?;
        let size_bytes = &input[kind_end + 1..size_end];
        let size = btoi::btoi(size_bytes).map_err(|source| ParseIntegerError {
            source,
            message: "Object size in header could not be parsed",
            number: size_bytes.into(),
        })?;
        Ok((kind, size, size_end + 1))
    }
More examples
Hide additional examples
src/tag/decode.rs (line 21)
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
pub fn git_tag<'a, E: ParseError<&'a [u8]> + ContextError<&'a [u8]>>(i: &'a [u8]) -> IResult<&[u8], TagRef<'a>, E> {
    let (i, target) = context("object <40 lowercase hex char>", |i| {
        parse::header_field(i, b"object", parse::hex_hash)
    })(i)?;

    let (i, kind) = context("type <object kind>", |i| {
        parse::header_field(i, b"type", take_while1(is_alphabetic))
    })(i)?;
    let kind = crate::Kind::from_bytes(kind)
        .map_err(|_| nom::Err::Error(E::from_error_kind(i, nom::error::ErrorKind::MapRes)))?;

    let (i, tag_version) = context("tag <version>", |i| {
        parse::header_field(i, b"tag", take_while1(|b| b != NL[0]))
    })(i)?;

    let (i, signature) = context(
        "tagger <signature>",
        opt(|i| parse::header_field(i, b"tagger", parse::signature)),
    )(i)?;
    let (i, (message, pgp_signature)) = all_consuming(message)(i)?;
    Ok((
        i,
        TagRef {
            target,
            name: tag_version.as_bstr(),
            target_kind: kind,
            message,
            tagger: signature,
            pgp_signature,
        },
    ))
}
src/tag/ref_iter.rs (line 84)
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
    fn next_inner(i: &'a [u8], state: &mut State) -> Result<(&'a [u8], Token<'a>), crate::decode::Error> {
        use State::*;
        Ok(match state {
            Target => {
                let (i, target) = context("object <40 lowercase hex char>", |i| {
                    parse::header_field(i, b"object", parse::hex_hash)
                })(i)?;
                *state = TargetKind;
                (
                    i,
                    Token::Target {
                        id: ObjectId::from_hex(target).expect("parsing validation"),
                    },
                )
            }
            TargetKind => {
                let (i, kind) = context("type <object kind>", |i| {
                    parse::header_field(i, b"type", take_while1(is_alphabetic))
                })(i)?;
                let kind = Kind::from_bytes(kind).map_err(|_| {
                    #[allow(clippy::let_unit_value)]
                    {
                        let err = crate::decode::ParseError::from_error_kind(i, nom::error::ErrorKind::MapRes);
                        nom::Err::Error(err)
                    }
                })?;
                *state = Name;
                (i, Token::TargetKind(kind))
            }
            Name => {
                let (i, tag_version) = context("tag <version>", |i| {
                    parse::header_field(i, b"tag", take_while1(|b| b != NL[0]))
                })(i)?;
                *state = Tagger;
                (i, Token::Name(tag_version.as_bstr()))
            }
            Tagger => {
                let (i, signature) = context(
                    "tagger <signature>",
                    opt(|i| parse::header_field(i, b"tagger", parse::signature)),
                )(i)?;
                *state = Message;
                (i, Token::Tagger(signature))
            }
            Message => {
                let (i, (message, pgp_signature)) = all_consuming(decode::message)(i)?;
                debug_assert!(
                    i.is_empty(),
                    "we should have consumed all data - otherwise iter may go forever"
                );
                return Ok((i, Token::Body { message, pgp_signature }));
            }
        })
    }

Return the name of self for use in serialized loose git objects.

Examples found in repository?
src/kind.rs (line 38)
37
38
39
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(std::str::from_utf8(self.as_bytes()).expect("Converting Kind name to utf8"))
    }
More examples
Hide additional examples
src/encode.rs (line 24)
22
23
24
25
26
27
28
29
pub fn loose_header(kind: crate::Kind, size: usize) -> smallvec::SmallVec<[u8; 28]> {
    let mut v = smallvec::SmallVec::new();
    check!(v.write_all(kind.as_bytes()));
    check!(v.write_all(SPACE));
    check!(v.write_all(itoa::Buffer::new().format(size).as_bytes()));
    check!(v.write_all(b"\0"));
    v
}
src/tag/write.rs (line 26)
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
    fn write_to(&self, mut out: impl io::Write) -> io::Result<()> {
        encode::trusted_header_id(b"object", &self.target, &mut out)?;
        encode::trusted_header_field(b"type", self.target_kind.as_bytes(), &mut out)?;
        encode::header_field(b"tag", validated_name(self.name.as_ref())?, &mut out)?;
        if let Some(tagger) = &self.tagger {
            encode::trusted_header_signature(b"tagger", &tagger.to_ref(), &mut out)?;
        }

        out.write_all(NL)?;
        if !self.message.is_empty() {
            out.write_all(self.message.as_ref())?;
        }
        if let Some(message) = &self.pgp_signature {
            out.write_all(NL)?;
            out.write_all(message.as_ref())?;
        }
        Ok(())
    }

    fn kind(&self) -> Kind {
        Kind::Tag
    }

    fn size(&self) -> usize {
        b"object".len() + 1 /* space */ + self.target.kind().len_in_hex() + 1 /* nl */
            + b"type".len() + 1 /* space */ + self.target_kind.as_bytes().len() + 1 /* nl */
            + b"tag".len() + 1 /* space */ + self.name.len() + 1 /* nl */
            + self
            .tagger
            .as_ref()
            .map(|t| b"tagger".len() + 1 /* space */ + t.size() + 1 /* nl */)
            .unwrap_or(0)
            + 1 /* nl */ + self.message.len()
            + self.pgp_signature.as_ref().map(|m| 1 /* nl */ + m.len()).unwrap_or(0)
    }
}

impl<'a> crate::WriteTo for TagRef<'a> {
    fn write_to(&self, mut out: impl io::Write) -> io::Result<()> {
        encode::trusted_header_field(b"object", self.target, &mut out)?;
        encode::trusted_header_field(b"type", self.target_kind.as_bytes(), &mut out)?;
        encode::header_field(b"tag", validated_name(self.name)?, &mut out)?;
        if let Some(tagger) = &self.tagger {
            encode::trusted_header_signature(b"tagger", tagger, &mut out)?;
        }

        out.write_all(NL)?;
        if !self.message.is_empty() {
            out.write_all(self.message)?;
        }
        if let Some(message) = self.pgp_signature {
            out.write_all(NL)?;
            out.write_all(message)?;
        }
        Ok(())
    }

    fn kind(&self) -> Kind {
        Kind::Tag
    }

    fn size(&self) -> usize {
        b"object".len() + 1 /* space */ + self.target().kind().len_in_hex() + 1 /* nl */
            + b"type".len() + 1 /* space */ + self.target_kind.as_bytes().len() + 1 /* nl */
            + b"tag".len() + 1 /* space */ + self.name.len() + 1 /* nl */
            + self
                .tagger
                .as_ref()
                .map(|t| b"tagger".len() + 1 /* space */ + t.size() + 1 /* nl */)
                .unwrap_or(0)
            + 1 /* nl */ + self.message.len()
            + self.pgp_signature.as_ref().map(|m| 1 /* nl */ + m.len()).unwrap_or(0)
    }

Trait Implementations§

Returns a copy of the value. Read more
Performs copy-assignment from source. Read more
Formats the value using the given formatter. Read more
Deserialize this value from the given Serde deserializer. Read more
Formats the value using the given formatter. Read more
Feeds this value into the given Hasher. Read more
Feeds a slice of this type into the given Hasher. Read more
This method returns an Ordering between self and other. Read more
Compares and returns the maximum of two values. Read more
Compares and returns the minimum of two values. Read more
Restrict a value to a certain interval. Read more
This method tests for self and other values to be equal, and is used by ==.
This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
This method returns an ordering between self and other values if one exists. Read more
This method tests less than (for self and other) and is used by the < operator. Read more
This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
This method tests greater than (for self and other) and is used by the > operator. Read more
This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
Serialize this value into the given Serde serializer. Read more

Auto Trait Implementations§

Blanket Implementations§

Gets the TypeId of self. Read more
Immutably borrows from an owned value. Read more
Mutably borrows from an owned value. Read more

Returns the argument unchanged.

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

The resulting type after obtaining ownership.
Creates owned data from borrowed data, usually by cloning. Read more
Uses borrowed data to replace owned data, usually by cloning. Read more
Converts the given value to a String. Read more
The type returned in the event of a conversion error.
Performs the conversion.
The type returned in the event of a conversion error.
Performs the conversion.