-
Notifications
You must be signed in to change notification settings - Fork 11
Expand file tree
/
Copy pathparse.go
More file actions
94 lines (83 loc) · 3.06 KB
/
Copy pathparse.go
File metadata and controls
94 lines (83 loc) · 3.06 KB
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
package tachograph
import (
"fmt"
"github.com/way-platform/tachograph-go/internal/card"
"github.com/way-platform/tachograph-go/internal/vu"
cardv1 "github.com/way-platform/tachograph-go/proto/gen/go/wayplatform/connect/tachograph/card/v1"
tachographv1 "github.com/way-platform/tachograph-go/proto/gen/go/wayplatform/connect/tachograph/v1"
)
// Parse performs semantic parsing on raw tachograph records with default options.
// If the raw file has been authenticated (via Authenticate), the authentication
// results are propagated to the parsed messages.
//
// This is a convenience function that uses default options:
// - PreserveRawData: true (store raw bytes for round-tripping)
//
// For custom options, use ParseOptions directly:
//
// opts := ParseOptions{PreserveRawData: false}
// file, err := opts.Parse(rawFile)
func Parse(rawFile *tachographv1.RawFile) (*tachographv1.File, error) {
opts := ParseOptions{
PreserveRawData: true,
}
return opts.Parse(rawFile)
}
// ParseOptions configures the parsing process for converting raw tachograph
// files into semantic data structures.
type ParseOptions struct {
// PreserveRawData controls whether raw byte slices are stored in
// the raw_data field of parsed protobuf messages.
//
// If true (default), the raw byte slice for each parsed element
// will be stored in the raw_data field, enabling perfect binary
// round-tripping via Marshal.
//
// If false, raw_data fields will be left empty, reducing memory usage
// but preventing exact binary reconstruction.
PreserveRawData bool
}
// card returns card.ParseOptions configured from ParseOptions.
func (o ParseOptions) card() card.ParseOptions {
return card.ParseOptions{
PreserveRawData: o.PreserveRawData,
}
}
// vu returns vu.ParseOptions configured from ParseOptions.
func (o ParseOptions) vu() vu.ParseOptions {
return vu.ParseOptions{
PreserveRawData: o.PreserveRawData,
}
}
// Parse performs the second parsing pass, converting raw records into semantic
// data structures. If the raw file has been authenticated (via
// AuthenticateOptions.Authenticate), the authentication results are propagated
// to the parsed messages.
func (o ParseOptions) Parse(rawFile *tachographv1.RawFile) (*tachographv1.File, error) {
var file tachographv1.File
switch rawFile.GetType() {
case tachographv1.RawFile_CARD:
cardType := card.InferFileType(rawFile.GetCard())
switch cardType {
case cardv1.CardType_DRIVER_CARD:
driverCard, err := o.card().ParseRawDriverCardFile(rawFile.GetCard())
if err != nil {
return nil, fmt.Errorf("failed to parse driver card: %w", err)
}
file.SetType(tachographv1.File_DRIVER_CARD)
file.SetDriverCard(driverCard)
default:
return nil, fmt.Errorf("unsupported card type: %v", cardType)
}
case tachographv1.RawFile_VEHICLE_UNIT:
vuFile, err := o.vu().ParseRawVehicleUnitFile(rawFile.GetVehicleUnit())
if err != nil {
return nil, err
}
file.SetType(tachographv1.File_VEHICLE_UNIT)
file.SetVehicleUnit(vuFile)
default:
return nil, fmt.Errorf("unknown raw file type: %v", rawFile.GetType())
}
return &file, nil
}