forked from yeqown/go-qrcode
-
Notifications
You must be signed in to change notification settings - Fork 0
/
encoder_option.go
71 lines (56 loc) · 1.62 KB
/
encoder_option.go
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
package qrcode
type EncodeOption interface {
apply(option *encodingOption)
}
// DefaultEncodingOption with EncMode = EncModeAuto, EcLevel = ErrorCorrectionQuart
func DefaultEncodingOption() *encodingOption {
return &encodingOption{
EncMode: EncModeAuto,
EcLevel: ErrorCorrectionQuart,
}
}
type encodingOption struct {
// Version of target QR code.
Version int
// EncMode specifies which encMode to use
EncMode encMode
// EcLevel specifies which ecLevel to use
EcLevel ecLevel
// PS: The version (which implicitly defines the byte capacity of the qrcode) is dynamically selected at runtime
}
type fnEncodingOption struct {
fn func(*encodingOption)
}
func (f fnEncodingOption) apply(option *encodingOption) {
f.fn(option)
}
func newFnEncodingOption(fn func(*encodingOption)) fnEncodingOption {
return fnEncodingOption{fn: fn}
}
// WithEncodingMode sets the encoding mode.
func WithEncodingMode(mode encMode) EncodeOption {
return newFnEncodingOption(func(option *encodingOption) {
if name := getEncModeName(mode); name == "" {
return
}
option.EncMode = mode
})
}
// WithErrorCorrectionLevel sets the error correction level.
func WithErrorCorrectionLevel(ecLevel ecLevel) EncodeOption {
return newFnEncodingOption(func(option *encodingOption) {
if ecLevel < ErrorCorrectionLow || ecLevel > ErrorCorrectionHighest {
return
}
option.EcLevel = ecLevel
})
}
// WithVersion sets the version of target QR code.
func WithVersion(version int) EncodeOption {
return newFnEncodingOption(func(option *encodingOption) {
if version < 1 || version > _VERSION_COUNT {
return
}
option.Version = version
})
}