Skip to content

Commit f513b84

Browse files
Add various helpers (#2)
Useful for Almond
1 parent fd8d60b commit f513b84

File tree

6 files changed

+73
-10
lines changed

6 files changed

+73
-10
lines changed

directive-handler/src/main/scala/scala/cli/directivehandler/DirectiveHandler.scala

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,26 @@ trait DirectiveHandler[+T] { self =>
5151
.map(_.map(f))
5252
}
5353

54+
def ignore: DirectiveHandler[IgnoredDirective] =
55+
new DirectiveHandler[IgnoredDirective] {
56+
def name: String = self.name
57+
def description: String = "[ignored]"
58+
override def descriptionMd: String = "ignored"
59+
def usage: String = ""
60+
override def usageMd: String = ""
61+
override def examples: Seq[String] = Nil
62+
override def tags: Seq[String] = Nil
63+
64+
def keys: Seq[String] = self.keys
65+
66+
private lazy val fieldNames = keys.toSet
67+
68+
def handleValues(
69+
scopedDirective: ScopedDirective
70+
): Either[DirectiveException, ProcessedDirective[IgnoredDirective]] =
71+
Right(ProcessedDirective(Some(IgnoredDirective(scopedDirective)), Nil))
72+
}
73+
5474
}
5575

5676
object DirectiveHandler extends DirectiveHandlerMacros {

directive-handler/src/main/scala/scala/cli/directivehandler/DirectiveHandlerMacros.scala2.scala

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -144,11 +144,7 @@ object DirectiveHandlerMacros {
144144
valuesByScope
145145
.map {
146146
case (scopeOpt, values) =>
147-
parser.value.parse(
148-
scopedDirective.directive.values,
149-
scopedDirective.cwd,
150-
scopedDirective.maybePath
151-
).map { r =>
147+
parser.value.parse(scopedDirective).map { r =>
152148
scopeOpt -> (field[K](r) :: actualDefault(default).tail)
153149
}
154150
}

directive-handler/src/main/scala/scala/cli/directivehandler/DirectiveHandlerMacros.scala3.scala

Lines changed: 1 addition & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -280,11 +280,7 @@ object DirectiveHandlerMacros {
280280
valuesByScope
281281
.map {
282282
case (scopeOpt, values) =>
283-
$parser.parse(
284-
$scopedDirective.directive.values,
285-
$scopedDirective.cwd,
286-
$scopedDirective.maybePath
287-
).map { r =>
283+
$parser.parse($scopedDirective).map { r =>
288284
scopeOpt -> ${
289285
genNew(List(newArgs.updated(idx, '{ r }).map(_.asTerm)))
290286
.asExprOf[T]

directive-handler/src/main/scala/scala/cli/directivehandler/DirectiveHandlers.scala

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,15 @@ final case class DirectiveHandlers[T](handlers: Seq[DirectiveHandler[T]]) {
3030
.left.map(CompositeDirectiveException(_))
3131
}
3232

33+
def map[U](f: T => U): DirectiveHandlers[U] =
34+
copy(handlers = handlers.map(_.map(f)))
35+
36+
def mapDirectives[U](f: DirectiveHandler[T] => DirectiveHandler[U]): DirectiveHandlers[U] =
37+
copy(handlers = handlers.map(f))
38+
39+
def ++(other: DirectiveHandlers[T]): DirectiveHandlers[T] =
40+
if (other.handlers.isEmpty) this
41+
else if (handlers.isEmpty) other
42+
else DirectiveHandlers(handlers ++ other.handlers)
43+
3344
}

directive-handler/src/main/scala/scala/cli/directivehandler/DirectiveValueParser.scala

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,14 @@ package scala.cli.directivehandler
33
import com.virtuslab.using_directives.custom.model.{BooleanValue, EmptyValue, StringValue, Value}
44

55
abstract class DirectiveValueParser[+T] {
6+
7+
def parse(scopedDirective: ScopedDirective): Either[DirectiveException, T] =
8+
parse(
9+
scopedDirective.directive.values,
10+
scopedDirective.cwd,
11+
scopedDirective.maybePath
12+
)
13+
614
def parse(
715
values: Seq[Value[_]],
816
scopePath: ScopePath,
@@ -17,6 +25,8 @@ object DirectiveValueParser {
1725

1826
private final class Mapped[T, +U](underlying: DirectiveValueParser[T], f: T => U)
1927
extends DirectiveValueParser[U] {
28+
override def parse(scopedDirective: ScopedDirective): Either[DirectiveException, U] =
29+
underlying.parse(scopedDirective).map(f)
2030
def parse(
2131
values: Seq[Value[_]],
2232
scopePath: ScopePath,
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package scala.cli.directivehandler
2+
3+
import com.virtuslab.using_directives.custom.model.Value
4+
5+
final case class IgnoredDirective(directive: ScopedDirective)
6+
7+
object IgnoredDirective {
8+
9+
implicit val valueParser: DirectiveValueParser[IgnoredDirective] =
10+
new DirectiveValueParser[IgnoredDirective] {
11+
override def parse(scopedDirective: ScopedDirective)
12+
: Either[DirectiveException, IgnoredDirective] =
13+
Right(IgnoredDirective(scopedDirective))
14+
def parse(
15+
values: Seq[Value[_]],
16+
scopePath: ScopePath,
17+
path: Either[String, os.Path]
18+
): Either[DirectiveException, IgnoredDirective] =
19+
Right(
20+
IgnoredDirective(
21+
ScopedDirective(
22+
StrictDirective("", values),
23+
path,
24+
scopePath
25+
)
26+
)
27+
)
28+
}
29+
30+
}

0 commit comments

Comments
 (0)