-
Notifications
You must be signed in to change notification settings - Fork 44
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #350 from alexarchambault/indexed
Add Indexed type
- Loading branch information
Showing
30 changed files
with
419 additions
and
124 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,45 @@ | ||
package caseapp.core | ||
|
||
import caseapp.core.argparser.{ArgParser, Consumed} | ||
|
||
final case class Indexed[+T]( | ||
index: Int, | ||
length: Int, | ||
value: T | ||
) | ||
|
||
object Indexed { | ||
|
||
def apply[T](value: T): Indexed[T] = | ||
Indexed(-1, 0, value) | ||
|
||
def list[T](seq: List[T], startIdx: Int): List[Indexed[T]] = | ||
seq.zipWithIndex.map { | ||
case (elem, idx) => | ||
Indexed(startIdx + idx, 1, elem) | ||
} | ||
|
||
def seq[T](seq: Seq[T], startIdx: Int): Seq[Indexed[T]] = | ||
seq.zipWithIndex.map { | ||
case (elem, idx) => | ||
Indexed(startIdx + idx, 1, elem) | ||
} | ||
|
||
implicit def argParser[T: ArgParser]: ArgParser[Indexed[T]] = | ||
new ArgParser[Indexed[T]] { | ||
private val underlying = ArgParser[T] | ||
def apply(current: Option[Indexed[T]], index: Int, span: Int, value: String) = | ||
underlying(current.map(_.value), index, span, value) | ||
.map(t => Indexed(index, span, t)) | ||
override def apply(current: Option[Indexed[T]], index: Int) = | ||
underlying(current.map(_.value), index) | ||
.map(t => Indexed(index, 1, t)) | ||
override def optional(current: Option[Indexed[T]], index: Int, span: Int, value: String) = { | ||
val (consumed, res) = underlying.optional(current.map(_.value), index, span, value) | ||
val len = if (consumed.value) span else 1 | ||
(consumed, res.map(t => Indexed(index, len, t))) | ||
} | ||
override def isFlag = underlying.isFlag | ||
def description = underlying.description | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.