30
30
31
31
package scodec .bits
32
32
33
- import scala .quoted ._
33
+ import scala .quoted .*
34
34
35
35
/**
36
36
* Provides the `hex` string interpolator, which returns `ByteVector` instances from hexadecimal strings.
@@ -41,7 +41,7 @@ import scala.quoted._
41
41
* }}}
42
42
*/
43
43
extension (inline ctx : StringContext ) inline def hex (inline args : Any * ): ByteVector =
44
- $ {Literals .validateHex (' ctx , ' args )}
44
+ $ {Literals .Hex (' ctx , ' args )}
45
45
46
46
/**
47
47
* Provides the `bin` string interpolator, which returns `BitVector` instances from binary strings.
@@ -52,57 +52,41 @@ extension (inline ctx: StringContext) inline def hex (inline args: Any*): ByteVe
52
52
* }}}
53
53
*/
54
54
extension (inline ctx : StringContext ) inline def bin (inline args : Any * ): BitVector =
55
- $ {Literals .validateBin (' ctx , ' args )}
55
+ $ {Literals .Bin (' ctx , ' args )}
56
56
57
- object Literals {
57
+ object Literals :
58
58
59
- trait Validator [A ] {
60
- def validate (s : String ): Option [String ]
61
- def build (s : String )(using Quotes ): Expr [A ]
62
- }
59
+ trait Validator [A ]:
60
+ def validate (s : String )(using Quotes ): Either [String , Expr [A ]]
63
61
64
- def validateHex (strCtxExpr : Expr [StringContext ], argsExpr : Expr [Seq [Any ]])(using Quotes ): Expr [ByteVector ] =
65
- validate(Hex , strCtxExpr, argsExpr)
66
-
67
- def validateBin (strCtxExpr : Expr [StringContext ], argsExpr : Expr [Seq [Any ]])(using Quotes ): Expr [BitVector ] =
68
- validate(Bin , strCtxExpr, argsExpr)
69
-
70
- def validate [A ](validator : Validator [A ], strCtxExpr : Expr [StringContext ], argsExpr : Expr [Seq [Any ]])(using Quotes ): Expr [A ] = {
71
- strCtxExpr.value match {
72
- case Some (sc) => validate(validator, sc.parts, argsExpr)
73
- case None =>
74
- quotes.reflect.report.error(" StringContext args must be statically known" )
75
- ???
76
- }
77
- }
78
-
79
- private def validate [A ](validator : Validator [A ], parts : Seq [String ], argsExpr : Expr [Seq [Any ]])(using Quotes ): Expr [A ] = {
80
- if (parts.size == 1 ) {
81
- val literal = parts.head
82
- validator.validate(literal) match {
83
- case Some (err) =>
84
- quotes.reflect.report.error(err)
85
- ???
62
+ def apply (strCtxExpr : Expr [StringContext ], argsExpr : Expr [Seq [Any ]])(using Quotes ): Expr [A ] =
63
+ strCtxExpr.value match
64
+ case Some (sc) => apply(sc.parts, argsExpr)
86
65
case None =>
87
- validator.build(literal)
88
- }
89
- } else {
90
- quotes.reflect.report.error(" interpolation not supported" , argsExpr)
91
- ???
92
- }
93
- }
66
+ quotes.reflect.report.error(" StringContext args must be statically known" )
67
+ ???
94
68
95
- object Hex extends Validator [ByteVector ] {
96
- def validate (s : String ): Option [String ] =
97
- ByteVector .fromHex(s).fold(Some (" hexadecimal string literal may only contain characters [0-9a-fA-f]" ))(_ => None )
98
- def build (s : String )(using Quotes ): Expr [ByteVector ] =
99
- ' {ByteVector .fromValidHex($ {Expr (s)})},
100
- }
69
+ private def apply (parts : Seq [String ], argsExpr : Expr [Seq [Any ]])(using Quotes ): Expr [A ] =
70
+ if parts.size == 1 then
71
+ val literal = parts.head
72
+ validate(literal) match
73
+ case Left (err) =>
74
+ quotes.reflect.report.error(err)
75
+ ???
76
+ case Right (a) =>
77
+ a
78
+ else
79
+ quotes.reflect.report.error(" interpolation not supported" , argsExpr)
80
+ ???
81
+
82
+ object Hex extends Validator [ByteVector ]:
83
+ def validate (s : String )(using Quotes ): Either [String , Expr [ByteVector ]] =
84
+ ByteVector .fromHex(s) match
85
+ case None => Left (" hexadecimal string literal may only contain characters [0-9a-fA-f]" )
86
+ case Some (_) => Right (' {ByteVector .fromValidHex($ {Expr (s)})})
101
87
102
- object Bin extends Validator [BitVector ] {
103
- def validate (s : String ): Option [String ] =
104
- ByteVector .fromBin(s).fold(Some (" binary string literal may only contain characters [0, 1]" ))(_ => None )
105
- def build (s : String )(using Quotes ): Expr [BitVector ] =
106
- ' {BitVector .fromValidBin($ {Expr (s)})},
107
- }
108
- }
88
+ object Bin extends Validator [BitVector ]:
89
+ def validate (s : String )(using Quotes ): Either [String , Expr [BitVector ]] =
90
+ ByteVector .fromBin(s) match
91
+ case None => Left (" binary string literal may only contain characters [0, 1]" )
92
+ case Some (_) => Right (' {BitVector .fromValidBin($ {Expr (s)})})
0 commit comments