aboutsummaryrefslogtreecommitdiff
path: root/internal/config/diag.go
blob: 45f383a14a010a09494514d9fb369dc6d724d5cd (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
// SPDX-License-Identifier: GPL-3.0-or-later

// Package config reads krino's configuration: the main file krino.conf and
// one file per directory under dirs/. It checks everything it reads and
// reports every problem with its position.
package config

import (
	"fmt"

	"git.labunix.xyz/krino/internal/sexp"
)

// Diag is one problem in a config file.
type Diag struct {
	File string
	Pos  sexp.Pos
	Msg  string
}

func (d *Diag) Error() string {
	if d.Pos.Line == 0 {
		return fmt.Sprintf("%s: %s", d.File, d.Msg)
	}
	return fmt.Sprintf("%s:%d:%d: %s", d.File, d.Pos.Line, d.Pos.Col, d.Msg)
}

// diags collects the problems found in one file.
type diags struct {
	file string
	list []*Diag
}

// at records a problem at n's position; a nil n means the whole file.
func (d *diags) at(n *sexp.Node, format string, args ...any) {
	var pos sexp.Pos
	if n != nil {
		pos = n.Pos
	}
	d.list = append(d.list, &Diag{File: d.file, Pos: pos, Msg: fmt.Sprintf(format, args...)})
}