summaryrefslogtreecommitdiff
path: root/vendor/github.com/google/yamlfmt/metadata.go
blob: 82abfc9e5661cd98ce785d2eb0c3b1906c6b789e (plain)
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
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
// Copyright 2024 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
//     http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package yamlfmt

import (
	"errors"
	"fmt"
	"strings"
	"unicode"

	"github.com/google/yamlfmt/internal/collections"
)

const MetadataIdentifier = "!yamlfmt!"

type MetadataType string

const (
	MetadataIgnore MetadataType = "ignore"
)

func IsMetadataType(mdValueStr string) bool {
	mdTypes := collections.Set[MetadataType]{}
	mdTypes.Add(MetadataIgnore)
	return mdTypes.Contains(MetadataType(mdValueStr))
}

type Metadata struct {
	Type    MetadataType
	LineNum int
}

var (
	ErrMalformedMetadata    = errors.New("metadata: malformed string")
	ErrUnrecognizedMetadata = errors.New("metadata: unrecognized type")
)

type MetadataError struct {
	err     error
	path    string
	lineNum int
	lineStr string
}

func (e *MetadataError) Error() string {
	return fmt.Sprintf(
		"%v: %s:%d:%s",
		e.err,
		e.path,
		e.lineNum,
		e.lineStr,
	)
}

func (e *MetadataError) Unwrap() error {
	return e.err
}

func ReadMetadata(content []byte, path string) (collections.Set[Metadata], collections.Errors) {
	metadata := collections.Set[Metadata]{}
	mdErrs := collections.Errors{}
	// This could be `\r\n` but it won't affect the outcome of this operation.
	contentLines := strings.Split(string(content), "\n")
	for i, line := range contentLines {
		mdidIndex := strings.Index(line, MetadataIdentifier)
		if mdidIndex == -1 {
			continue
		}
		mdStr := scanMetadata(line, mdidIndex)
		mdComponents := strings.Split(mdStr, ":")
		if len(mdComponents) != 2 {
			mdErrs = append(mdErrs, &MetadataError{
				path:    path,
				lineNum: i + 1,
				err:     ErrMalformedMetadata,
				lineStr: line,
			})
			continue
		}
		if IsMetadataType(mdComponents[1]) {
			metadata.Add(Metadata{LineNum: i + 1, Type: MetadataType(mdComponents[1])})
		} else {
			mdErrs = append(mdErrs, &MetadataError{
				path:    path,
				lineNum: i + 1,
				err:     ErrUnrecognizedMetadata,
				lineStr: line,
			})
		}
	}
	return metadata, mdErrs
}

func scanMetadata(line string, index int) string {
	mdBytes := []byte{}
	i := index
	for i < len(line) && !unicode.IsSpace(rune(line[i])) {
		mdBytes = append(mdBytes, line[i])
		i++
	}
	return string(mdBytes)
}