@@ -2,7 +2,10 @@ package scala.cli.directivehandler
2
2
3
3
import scala .cli .directivehandler .EitherSequence ._
4
4
5
- final case class DirectiveHandlers [T ](handlers : Seq [DirectiveHandler [T ]]) {
5
+ final case class DirectiveHandlers [T ](
6
+ handlers : Seq [DirectiveHandler [T ]],
7
+ customHandler : String => Option [DirectiveHandler [T ]] = (key : String ) => None
8
+ ) {
6
9
7
10
private lazy val handlersMap = handlers.flatMap(h => h.keys.map(_ -> h)).toMap
8
11
@@ -19,7 +22,8 @@ final case class DirectiveHandlers[T](handlers: Seq[DirectiveHandler[T]]) {
19
22
20
23
directives
21
24
.map { dir =>
22
- handlersMap.get(dir.directive.key) match {
25
+ val key = dir.directive.key
26
+ handlersMap.get(key).orElse(customHandler(key)) match {
23
27
case Some (h) =>
24
28
h.handleValues(dir)
25
29
case None =>
@@ -31,14 +35,25 @@ final case class DirectiveHandlers[T](handlers: Seq[DirectiveHandler[T]]) {
31
35
}
32
36
33
37
def map [U ](f : T => U ): DirectiveHandlers [U ] =
34
- copy(handlers = handlers.map(_.map(f)))
38
+ copy(
39
+ handlers = handlers.map(_.map(f)),
40
+ customHandler = key => customHandler(key).map(_.map(f))
41
+ )
35
42
36
43
def mapDirectives [U ](f : DirectiveHandler [T ] => DirectiveHandler [U ]): DirectiveHandlers [U ] =
37
- copy(handlers = handlers.map(f))
44
+ copy(
45
+ handlers = handlers.map(f),
46
+ customHandler = key => customHandler(key).map(f)
47
+ )
38
48
39
49
def ++ (other : DirectiveHandlers [T ]): DirectiveHandlers [T ] =
40
50
if (other.handlers.isEmpty) this
41
51
else if (handlers.isEmpty) other
42
52
else DirectiveHandlers (handlers ++ other.handlers)
43
53
54
+ def addCustomHandler (f : String => Option [DirectiveHandler [T ]]): DirectiveHandlers [T ] =
55
+ copy(
56
+ customHandler = key => customHandler(key).orElse(f(key))
57
+ )
58
+
44
59
}
0 commit comments