Skip to content

Commit f237e2e

Browse files
Add DirectiveHandlers helper
1 parent 8b06920 commit f237e2e

File tree

1 file changed

+33
-0
lines changed

1 file changed

+33
-0
lines changed
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
package scala.cli.directivehandler
2+
3+
import scala.cli.directivehandler.EitherSequence._
4+
5+
final case class DirectiveHandlers[T](handlers: Seq[DirectiveHandler[T]]) {
6+
7+
private lazy val handlersMap = handlers.flatMap(h => h.keys.map(_ -> h)).toMap
8+
9+
def parse(
10+
input: String,
11+
path: Either[String, os.Path],
12+
scopePath: ScopePath
13+
): Either[DirectiveException, Seq[ProcessedDirective[T]]] = {
14+
15+
val directives = ExtractedDirectives.from(input.toCharArray, path)
16+
.fold(e => throw e, identity)
17+
.directives
18+
.map(dir => ScopedDirective(dir, path, scopePath))
19+
20+
directives
21+
.map { dir =>
22+
handlersMap.get(dir.directive.key) match {
23+
case Some(h) =>
24+
h.handleValues(dir)
25+
case None =>
26+
Left(dir.unusedDirectiveError)
27+
}
28+
}
29+
.sequence
30+
.left.map(CompositeDirectiveException(_))
31+
}
32+
33+
}

0 commit comments

Comments
 (0)