fix(wireguard): Improve parser

This commit is contained in:
Myzel394 2024-08-13 23:29:52 +02:00
parent 2db9371fd0
commit 440252c352
No known key found for this signature in database
GPG Key ID: DEC4AAB876F73185

View File

@ -18,7 +18,14 @@ type characterLocation struct {
type wireguardParser struct { type wireguardParser struct {
Sections []wireguardSection Sections []wireguardSection
// Used to identify where not to show diagnostics // Used to identify where not to show diagnostics
CommentLines []uint32 CommentLines map[uint32]struct{}
}
func createWireguardParser() wireguardParser {
return wireguardParser{
Sections: []wireguardSection{},
CommentLines: map[uint32]struct{}{},
}
} }
type lineType string type lineType string
@ -57,6 +64,7 @@ func (p *wireguardParser) parseFromString(input string) []lineError {
collectedProperties := wireguardProperties{} collectedProperties := wireguardProperties{}
var lastPropertyLine *uint32 var lastPropertyLine *uint32
var earliestPropertyLine *uint32
for index, line := range lines { for index, line := range lines {
currentLineNumber := uint32(len(lines) - index - 1) currentLineNumber := uint32(len(lines) - index - 1)
@ -64,7 +72,7 @@ func (p *wireguardParser) parseFromString(input string) []lineError {
switch lineType { switch lineType {
case LineTypeComment: case LineTypeComment:
p.CommentLines = append(p.CommentLines, currentLineNumber) p.CommentLines[currentLineNumber] = struct{}{}
case LineTypeEmpty: case LineTypeEmpty:
continue continue
@ -80,6 +88,8 @@ func (p *wireguardParser) parseFromString(input string) []lineError {
continue continue
} }
earliestPropertyLine = &currentLineNumber
if lastPropertyLine == nil { if lastPropertyLine == nil {
lastPropertyLine = &currentLineNumber lastPropertyLine = &currentLineNumber
} }
@ -108,10 +118,68 @@ func (p *wireguardParser) parseFromString(input string) []lineError {
} }
} }
if len(collectedProperties) > 0 {
p.Sections = append(p.Sections, wireguardSection{
StartLine: *earliestPropertyLine,
EndLine: *lastPropertyLine,
Name: nil,
Properties: collectedProperties,
})
}
// Since we parse the content from bottom to top, // Since we parse the content from bottom to top,
// we need to reverse the order // we need to reverse the order
slices.Reverse(p.CommentLines)
slices.Reverse(p.Sections) slices.Reverse(p.Sections)
return errors return errors
} }
func (p *wireguardParser) getTypeByLine(line uint32) lineType {
// Check if line is a comment
if _, found := p.CommentLines[line]; found {
return LineTypeComment
}
// Check if line is a section
for _, section := range p.Sections {
if section.StartLine <= line && section.EndLine >= line {
if section.StartLine == line && section.Name != nil {
return LineTypeHeader
}
// Check for properties
for propertyLineNumber := range section.Properties {
if propertyLineNumber == line {
return LineTypeProperty
}
}
}
}
return LineTypeEmpty
}
// Get the section that the line belongs to
// Example:
// [Interface]
// Address = 10.0.0.1
//
// <line here>
// [Peer]
//
// This would return the section [Interface]
func (p *wireguardParser) getBelongingSectionByLine(line uint32) *wireguardSection {
// Create a copy
sections := append([]wireguardSection{}, p.Sections...)
slices.Reverse(sections)
for _, section := range sections {
if section.StartLine <= line && section.Name != nil {
return &section
}
}
// Global section
return nil
}