fix(wireguard): Improvements

This commit is contained in:
Myzel394 2024-08-14 18:28:06 +02:00
parent d456f4a569
commit d3aa99b5f5
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185
3 changed files with 16 additions and 21 deletions

View File

@ -1,12 +0,0 @@
package wireguard
type malformedLineError struct{}
func (e *malformedLineError) Error() string {
return "Malformed line"
}
type lineError struct {
Line uint32
Err error
}

View File

@ -1,6 +1,7 @@
package wireguard package wireguard
import ( import (
"config-lsp/common"
"regexp" "regexp"
"slices" "slices"
"strings" "strings"
@ -21,11 +22,16 @@ type wireguardParser struct {
CommentLines map[uint32]struct{} CommentLines map[uint32]struct{}
} }
func createWireguardParser() wireguardParser { func (p *wireguardParser) clear() {
return wireguardParser{ p.Sections = []wireguardSection{}
Sections: []wireguardSection{}, p.CommentLines = map[uint32]struct{}{}
CommentLines: map[uint32]struct{}{},
} }
func createWireguardParser() wireguardParser {
parser := wireguardParser{}
parser.clear()
return parser
} }
type lineType string type lineType string
@ -53,8 +59,8 @@ func getLineType(line string) lineType {
return LineTypeProperty return LineTypeProperty
} }
func (p *wireguardParser) parseFromString(input string) []lineError { func (p *wireguardParser) parseFromString(input string) []common.ParseError {
errors := []lineError{} errors := []common.ParseError{}
lines := strings.Split( lines := strings.Split(
input, input,
"\n", "\n",
@ -81,7 +87,7 @@ func (p *wireguardParser) parseFromString(input string) []lineError {
err := collectedProperties.AddLine(currentLineNumber, line) err := collectedProperties.AddLine(currentLineNumber, line)
if err != nil { if err != nil {
errors = append(errors, lineError{ errors = append(errors, common.ParseError{
Line: currentLineNumber, Line: currentLineNumber,
Err: err, Err: err,
}) })

View File

@ -1,6 +1,7 @@
package wireguard package wireguard
import ( import (
docvalues "config-lsp/doc-values"
"config-lsp/utils" "config-lsp/utils"
"regexp" "regexp"
"strings" "strings"
@ -42,7 +43,7 @@ func createWireguardProperty(line string) (*wireguardProperty, error) {
if indexes == nil { if indexes == nil {
// weird, should not happen // weird, should not happen
return nil, &malformedLineError{} return nil, &docvalues.MalformedLineError{}
} }
return &wireguardProperty{ return &wireguardProperty{
@ -58,7 +59,7 @@ func createWireguardProperty(line string) (*wireguardProperty, error) {
indexes := linePattern.FindStringSubmatchIndex(line) indexes := linePattern.FindStringSubmatchIndex(line)
if indexes == nil || len(indexes) == 0 { if indexes == nil || len(indexes) == 0 {
return nil, &malformedLineError{} return nil, &docvalues.MalformedLineError{}
} }
keyStart := uint32(indexes[2]) keyStart := uint32(indexes[2])