aboutsummaryrefslogtreecommitdiff
path: root/src/main/scala/testaceous/parameters.scala
blob: 9a062e5e9c0e198761bf2e06475372e0c178f0a0 (plain) (blame)
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
/*
  Testaceous, version 1.0.0. Copyright 2010-2017 Jon Pretty, Propensive Ltd.

  The primary distribution site is
  
    http://testaceo.us/

  Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in
  compliance with the License. You may obtain a copy of the License at
  
    http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software distributed under the License is
  distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and limitations under the License.
 */

package testaceous

import scala.util.Try
import scala.annotation._

case class ParamUsage(map: ParamMap, used: Set[String]) {
  def -(key: String): ParamUsage = copy(used = used + key)
  def --(keys: Set[String]): ParamUsage = copy(used = used ++ keys)
  def unexpected = map.groups.filterNot { p =>
    used contains p.key()
  }
}

case class ParamMap(args: String*) {

  def ++(pm2: ParamMap) = ParamMap(pm2.args ++ args: _*)

  case class Part(no: Int, start: Int, end: Int) {
    def apply() = args(no).substring(start, end)
  }

  case class Parameter(key: Part, values: Vector[Part] = Vector()) {
    override def toString = {
      val prefix = if (key().length == 1) "-" else "--"
      s"$prefix${key()} ${values.map(_ ()) mkString " "}"
    }
  }

  val groups: Set[Parameter] = parseArgs().to[Set]

  @tailrec
  private def parseArgs(gs: List[Parameter] = Nil, n: Int = 0, off: Int = 0): List[Parameter] = {
    if (n == args.length) gs
    else if (args(n) startsWith "--") {
      val idx = args(n).indexOf('=')
      if (idx < off) parseArgs(Parameter(Part(n, 2, args(n).length)) :: gs, n + 1)
      else parseArgs(Parameter(Part(n, 2, idx)) :: gs, n, idx + 1)
    } else if (args(n) startsWith "-") {
      if (off == 0) parseArgs(gs, n, 1)
      else if (args(n).length == off + 1) parseArgs(Parameter(Part(n, off, off + 1)) :: gs, n + 1)
      else parseArgs(Parameter(Part(n, off, off + 1)) :: gs, n, off + 1)
    } else {
      if (gs.isEmpty) parseArgs(gs, n + 1)
      else parseArgs(gs.head.copy(values = gs.head.values :+ Part(n, 0, args(n).length)) :: gs.tail, n + 1)
    }
  }

  def find(key: String): Option[Parameter] = groups.find(_.key() == key)

  def apply(names: Vector[String]): Option[Parameter] = names match {
    case Vector() => None
    case h +: t => find(h) orElse apply(t)
  }

  def isEmpty = args.isEmpty

  override def toString = groups.mkString
}

sealed class ParamException(msg: String) extends Exception(msg)

case class MissingParam(name: String) extends ParamException(s"the parameter --$name was missing")

case class InvalidValue(value: String, name: String)
    extends ParamException(s"the value '$value' is not valid for the parameter --$name")

case class UnexpectedParam(param: String) extends ParamException(s"found unexpected parameter '$param'")

@implicitNotFound("Can not combine elements of type ${A} and ${B}")
trait Construct[-A <: Params, -B <: Params] { construct =>
  type And <: Params
  type Or <: Params

  def and(a: A, b: B): ProductParams[And]
  def or(a: A, b: B): CoproductParams[Or]

  def swap: Construct[B, A] { type And = construct.And; type Or = construct.Or } =
    new Construct[B, A] {
      type And = construct.And
      type Or = construct.Or

      def and(a: B, b: A): ProductParams[And] = construct.and(b, a)
      def or(a: B, b: A): CoproductParams[Or] = construct.or(b, a)
    }
}

trait Construct_1 {

  implicit def general[A <: Params, B <: Params]: Construct[A, B] { type And = A with B; type Or = A with B } = {

    new Construct[A, B] {
      type And = A with B
      type Or = A with B

      def and(a: A, b: B) = ProductParams[A with B](Set(a, b))
      def or(a: A, b: B) = CoproductParams[A with B](Vector(a, b))
    }
  }
}

object Construct extends Construct_1 {

  implicit def leftProduct[A <: Params, B <: SimpleParam[_]]: Construct[ProductParams[A], B] {
    type And = A with B; type Or = ProductParams[A] with B
  } = {

    new Construct[ProductParams[A], B] {
      type And = A with B
      type Or = ProductParams[A] with B

      def and(a: ProductParams[A], b: B) = ProductParams[A with B](a.elements + b)

      def or(a: ProductParams[A], b: B) =
        CoproductParams[ProductParams[A] with B](Vector(a, b))
    }
  }

  implicit def rightProduct[A <: SimpleParam[_], B <: Params]: Construct[A, ProductParams[B]] {
    type And = B with A; type Or = ProductParams[B] with A
  } = leftProduct[B, A].swap

  implicit def leftCoproduct[A <: Params, B <: SimpleParam[_]]
    : Construct[CoproductParams[A], B] { type And = CoproductParams[A] with B; type Or = A with B } = {

    new Construct[CoproductParams[A], B] {
      type And = CoproductParams[A] with B
      type Or = A with B

      def and(a: CoproductParams[A], b: B) =
        ProductParams[CoproductParams[A] with B](Set(a, b))

      def or(a: CoproductParams[A], b: B) = CoproductParams[A with B](a.elements :+ b)
    }
  }

  implicit def rightCoproduct[A <: SimpleParam[_], B <: Params]
    : Construct[A, CoproductParams[B]] { type And = CoproductParams[B] with A; type Or = B with A } =
    leftCoproduct[B, A].swap
}

case class Suggestions(output: Option[Seq[Vector[String]]]) {
  def orElse(ss: Suggestions) = Suggestions(output orElse ss.output)
}

object SuggestionOutput {
  implicit val defaultOutput: SuggestionOutput = new SuggestionOutput {
    def output(ss: Suggestions) = ()
  }
}
trait SuggestionOutput { def output(ss: Suggestions): Unit }

trait Params { params =>
  type Result

  def parse(args: ParamMap, tabArg: Int = -1)(implicit suggestOutput: SuggestionOutput): Result = {

    val (result, lastArgs, ss) = check(ParamUsage(args, Set()), tabArg, Suggestions(None))

    suggestOutput.output(ss)

    lastArgs.unexpected foreach { p =>
      throw UnexpectedParam(p.key())
    }
    result
  }

  def check(args: ParamUsage, tabArg: Int, ss: Suggestions): (Result, ParamUsage, Suggestions)

  def &[B <: Params](b: B)(implicit con: Construct[params.type, b.type]): ProductParams[con.And] = {

    con.and(this, b)
  }

  def |[B <: Params](b: B)(implicit con: Construct[params.type, b.type]): CoproductParams[con.Or] = {

    con.or(this, b)
  }

  def unary_~ : OptionParams[this.type] = OptionParams(this)

  def by[R](fn: Result => R): Param.Handler[this.type, R] =
    new Param.Handler[this.type, R](this) {
      type From = Result
      def handle(v: From): R = fn(v)
    }
}

case class OptionParams[Ps <: Params](params: Ps) extends Params {
  type Result = Option[params.Result]

  def check(args: ParamUsage, tabArg: Int, ss: Suggestions): (Result, ParamUsage, Suggestions) =
    try {

      val (res, newArgs, newSs) = params.check(args, tabArg, ss)
      (Some(res), newArgs, newSs)
    } catch { case e: Exception => (None, args, ss) }

  override def toString = s"[$params]"
}

case class ProductParams[Ps <: Params](elements: Set[Params]) extends Params {
  type ProductTypes = Ps
  type Result = Product[ProductTypes]

  def check(args: ParamUsage, tabArg: Int, ss: Suggestions): (Result, ParamUsage, Suggestions) = {

    val (finalArgs, finalElems, newSs) = elements.foldLeft((args, Set[(Params, Any)](), ss)) {
      case ((args, es, ss), key) =>
        val (res, nextArgs, newSs) = key.check(args, tabArg, ss)
        (nextArgs, es + (key -> res), ss orElse newSs)
    }

    (new Product[Ps](finalElems.toMap), finalArgs, newSs)
  }

  override def toString = elements.mkString("( ", " & ", " )")
}

case class CoproductParams[Ps <: Params](elements: Vector[Params]) extends Params {
  type CoproductTypes = Ps
  type Result = Coproduct[CoproductTypes]

  def check(args: ParamUsage, tabArg: Int, ss: Suggestions): (Result, ParamUsage, Suggestions) = {
    val elems = elements.to[List].flatMap { k =>
      Try(Option(k.check(args, tabArg, ss)).get).toOption.map(k -> _)
    }

    elems match {
      case (key, (res, args, newSs)) :: Nil => (Coproduct[CoproductTypes](key -> res), args, newSs)
      case Nil => throw MissingParam(toString)
      case _ :: (key, _) :: _ => throw UnexpectedParam(key.toString)
    }
  }

  override def toString = elements.mkString("( ", " | ", " )")
}

object ToSuggestion {

  implicit val stringSuggestion: ToSuggestion[String] = new ToSuggestion[String] {
    def suggestion(value: String): Vector[String] = Vector(value)
  }
}
trait ToSuggestion[T] {
  def suggestion(value: T): Vector[String]
}

case class SimpleParam[T: Param.Extractor](keys: Vector[String]) extends Params { simpleParam =>

  type Result = T

  def toSuggestion(t: T): Vector[String] = Vector()
  def suggestions(s: String): Seq[T] = Seq()
  def checkValue: Option[T] = None

  def suggest(suggestions: T*)(implicit sug: ToSuggestion[T]): SimpleParam[T] =
    suggest { s =>
      suggestions
    }

  def suggest(suggestions: String => Seq[T])(implicit sug: ToSuggestion[T]): SimpleParam[T] = {
    val ss = suggestions
    new SimpleParam[T](keys) {
      override def toSuggestion(value: T): Vector[String] = sug.suggestion(value)
      override def suggestions(s: String) = ss(s)
      override def checkValue = simpleParam.checkValue
    }
  }

  def filter(fn: T => Boolean): SimpleParam[T] = new SimpleParam[T](keys) {
    override def toSuggestion(value: T): Vector[String] = simpleParam.toSuggestion(value)
    override def suggestions(str: String) = simpleParam.suggestions(str).filter(fn)
    override def checkValue = simpleParam.checkValue
  }

  protected val extractor = implicitly[Param.Extractor[T]]

  def check(args: ParamUsage, tabArg: Int, ss: Suggestions): (Result, ParamUsage, Suggestions) = {

    val parameter = args.map(keys) getOrElse { throw MissingParam(keys.head) }

    val res = extractor.extract(parameter.values.map(_ ())) getOrElse {
      throw InvalidValue(parameter.key(), parameter.values.mkString(" "))
    }

    checkValue.foreach { v =>
      if (v != res) throw InvalidValue(keys.head, "invalid")
    }

    val newSs = Suggestions(parameter.values.find(tabArg == _.no).map { p =>
      suggestions(p()).map(toSuggestion)
    })

    (res, args -- keys.to[Set], ss orElse newSs)
  }

  // Also consider `extractor` in `hashCode` and `equals`
  override def hashCode = keys.hashCode ^ extractor.hashCode

  override def equals(that: Any) = that match {
    case that: SimpleParam[_] =>
      keys.to[Set] == that.keys.to[Set] && that.extractor == extractor
    case _ =>
      false
  }

  override def toString =
    keys.map { k =>
      if (k.length == 1) s"-$k" else s"--$k"
    }.mkString("/")

  def of(v: T): SimpleParam[T] = new SimpleParam[T](keys) {
    override def toSuggestion(value: T): Vector[String] = simpleParam.toSuggestion(value)
    override def suggestions(str: String) = simpleParam.suggestions(str)
    override def checkValue = Some(v)
  }
}

object Param {

  object Extractor {
    implicit val stringExtractor: Extractor[String] = new Extractor[String] {
      // FIXME: Add mode parameter, and capture failure types
      def extract(values: Vector[String]): Option[String] = Some(values.mkString(" "))
    }

    implicit val intExtractor: Extractor[Int] = new Extractor[Int] {
      def extract(values: Vector[String]): Option[Int] = values match {
        case Vector(v) =>
          try Some(v.toInt)
          catch { case e: Exception => None }
        case _ => None
      }
    }

    implicit val unitExtractor: Extractor[Unit] = new Extractor[Unit] {
      def extract(values: Vector[String]): Option[Unit] = Some(())
    }
  }

  trait Extractor[T] { def extract(values: Vector[String]): Option[T] }

  abstract class Handler[-K, +H](val params: Params) {
    type From
    def handle(v: From): H
  }

  def apply[T: Extractor](shortName: Char, longName: Symbol): SimpleParam[T] =
    SimpleParam(Vector(shortName.toString, longName.name))

  def apply[T: Extractor](shortName: Char): SimpleParam[T] =
    SimpleParam(Vector(shortName.toString))

  def apply[T: Extractor](longName: Symbol): SimpleParam[T] = SimpleParam(Vector(longName.name))

  def flag(shortName: Char, longName: Symbol): SimpleParam[Unit] =
    SimpleParam(Vector(shortName.toString, longName.name))

  def flag(shortName: Char): SimpleParam[Unit] = SimpleParam(Vector(shortName.toString))
  def flag(longName: Symbol): SimpleParam[Unit] = SimpleParam(Vector(longName.name))
}

@implicitNotFound("product does not contain this value")
trait ProductContainsParam[V, T]

object ProductContainsParam extends ProductContainsParam_1 {
  implicit def optional[V <: OptionParams[_ <: Params], P <: Params]: ProductContainsParam[V, P] = null
}
trait ProductContainsParam_1 {
  implicit def generic[V, T <: V]: ProductContainsParam[V, T] = null
}

@implicitNotFound("coproduct cannot contain this value")
trait CoproductContainsParam[V, T]

object CoproductContainsParam {
  implicit def acceptable[V, T <: V]: CoproductContainsParam[V, T] = null
}

case class Product[T <: Params](tmap: Map[Params, Any]) {
  def apply[V <: Params](value: V)(implicit acc: ProductContainsParam[value.type, T]): value.Result =
    tmap(value).asInstanceOf[value.Result]

  override def toString = tmap.map { case (k, v) => s"$k: $v" }.mkString(", ")
}

case class Coproduct[T <: Params](value: (Params, Any)) {
  def handle[K, R](handlers: Param.Handler[K, R]*)(implicit ev: K <:< T): R = {
    val h = handlers.find(_.params == value._1).get
    h.handle(value._2.asInstanceOf[h.From])
  }

  override def toString = s"${value._1}: ${value._2}"
}