-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdecoder.go
More file actions
109 lines (88 loc) · 3.32 KB
/
decoder.go
File metadata and controls
109 lines (88 loc) · 3.32 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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
// Package abn provides automatic detection and decoding capabilities for various encoding formats
package abn
import "errors"
// Common errors
var (
ErrEmptyInput = errors.New("empty input provided")
ErrUnknownEncoding = errors.New("unknown or unsupported encoding format")
ErrInvalidFormat = errors.New("invalid format for detected encoding")
ErrDecodeFailed = errors.New("decoding failed for detected format")
)
// EncodingType represents the type of encoding detected
type EncodingType int
const (
Unknown EncodingType = iota
Base64
Base58
Base52
// Future encoding types can be added here// Base32
// Hex
)
// String returns the string representation of the encoding type
func (et EncodingType) String() string {
switch et {
case Base64:
return "Base64"
case Base58:
return "Base58"
case Base52:
return "Base52"
default:
return "Unknown"
}
}
// DecodeResult contains the result of a decode operation
type DecodeResult struct {
EncodingType EncodingType `json:"encoding_type"`
DecodedData []byte `json:"decoded_data"`
DecodedText string `json:"decoded_text"`
Confidence float64 `json:"confidence"` // Detection confidence (0.0-1.0)
Error error `json:"error,omitempty"`
}
// IsValid returns true if the decode operation was successful
func (dr *DecodeResult) IsValid() bool {
return dr.Error == nil && dr.EncodingType != Unknown
}
// EncodingDetector defines the interface for encoding detection
type EncodingDetector interface {
// Detect analyzes the input and returns possible encoding types with confidence scores
Detect(input string) []DetectionResult
// CanDetect returns true if this detector can handle the given encoding type
CanDetect(encodingType EncodingType) bool
}
// DetectionResult represents a detection result with confidence
type DetectionResult struct {
EncodingType EncodingType
Confidence float64 // 0.0 to 1.0, higher is more confident
}
// Decoder defines the interface for decoding operations
type Decoder interface {
// Decode attempts to decode the input using this decoder's format
Decode(input string) ([]byte, error)
// GetEncodingType returns the encoding type this decoder handles
GetEncodingType() EncodingType
// Validate checks if the input format is valid for this decoder
Validate(input string) error
}
// DecoderRegistry manages available decoders and detectors
type DecoderRegistry interface {
// RegisterDecoder adds a new decoder to the registry
RegisterDecoder(decoder Decoder) error
// RegisterDetector adds a new detector to the registry
RegisterDetector(detector EncodingDetector) error
// GetDecoder returns a decoder for the specified encoding type
GetDecoder(encodingType EncodingType) (Decoder, bool)
// GetDetectors returns all registered detectors
GetDetectors() []EncodingDetector
// ListSupportedTypes returns all supported encoding types
ListSupportedTypes() []EncodingType
}
// AutoDecoder defines the main interface for automatic decoding
type AutoDecoder interface {
// AutoDecode automatically detects and decodes the input
AutoDecode(input string) *DecodeResult
// DecodeWithType attempts to decode using a specific encoding type
DecodeWithType(input string, encodingType EncodingType) *DecodeResult
// DetectEncoding only detects the encoding type without decoding
DetectEncoding(input string) []DetectionResult
}