aboutsummaryrefslogtreecommitdiff
path: root/object/parse.go
blob: a4d0d3501ba1f52b793961e72f2d06025e8dc9eb (about) (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
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
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
package object

import (
	"fmt"

	"codeberg.org/lindenii/furgit/object/blob"
	"codeberg.org/lindenii/furgit/object/commit"
	"codeberg.org/lindenii/furgit/object/header"
	"codeberg.org/lindenii/furgit/object/id"
	"codeberg.org/lindenii/furgit/object/typ"
	// "codeberg.org/lindenii/furgit/object/tag"
	// "codeberg.org/lindenii/furgit/object/tree"
)

// SizeMismatchError indicates a mismatch
// between the size expected from the object header
// and the size of the object.
type SizeMismatchError struct {
	Expected int
	Got      int
}

func (sizeMismatchError SizeMismatchError) Error() string {
	return fmt.Sprintf(
		"object: size mismatch: header says %d bytes, but got %d from body",
		sizeMismatchError.Expected, sizeMismatchError.Got,
	)
}

// ParseWithHeader parses a loose object
// in "type size\x00body" format.
//
//nolint:ireturn
func ParseWithHeader(raw []byte, algo id.Algorithm) (Object, error) {
	ty, size, headerLen, err := header.Parse(raw)
	if err != nil {
		return nil, err
	}

	body := raw[headerLen:]
	if uint64(len(body)) != size {
		return nil, SizeMismatchError{Expected: int(size), Got: len(body)}
	}

	return ParseWithoutHeader(ty, body, algo)
}

// ParseWithoutHeader parses a typed object body.
//
//nolint:ireturn
func ParseWithoutHeader(ty typ.Type, body []byte, algo id.Algorithm) (Object, error) {
	switch ty {
	case typ.TypeBlob:
		return blob.Parse(body) //nolint:wrapcheck
	case typ.TypeTree:
		panic("TODO")
	case typ.TypeCommit:
		return commit.Parse(body, algo) //nolint:wrapcheck
	case typ.TypeTag:
		panic("TODO")
	default:
		return nil, typ.ErrInvalidType
	}
}