-
Notifications
You must be signed in to change notification settings - Fork 155
/
Resources.scala
99 lines (69 loc) · 2.84 KB
/
Resources.scala
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
package eu.timepit.refined.internal
import eu.timepit.refined.api.Result
import scala.util.{Failure, Success, Try}
object Resources {
val passed = "passed"
val failed = "failed"
val Predicate = "Predicate"
val predicate = Predicate.toLowerCase
val predicates = s"${predicate}s"
val Both = "Both"
val Left = "Left"
val Right = "Right"
def predicateResult(r: Result[_]): String =
s"$Predicate ${toLowerCase(r)}"
def predicateResultDetail(r: Result[_], detail: String): String =
s"${predicateResult(r)}: $detail"
def predicateResultDetailDot(r: Result[_], detail: String): String =
s"${predicateResultDetail(r, detail)}."
def predicateTakingResultDetail(taking: String, r: Result[_], detail: String): String =
s"$Predicate taking $taking ${toLowerCase(r)}: $detail"
def showExprEmptyCollection: String =
"<no element>"
def showResultEmptyCollection: String =
s"$Predicate $failed: empty collection."
def namePredicateResult(name: String, r: Result[_]): String =
s"$name $predicate ${toLowerCase(r)}"
def namePredicateResultMessage(name: String, r: Result[_], maybeThrowable: Try[_]): String = {
val suffix = maybeThrowable match {
case Success(_) => "."
case Failure(e) => s": ${e.getMessage}"
}
namePredicateResult(name, r) + suffix
}
def isValidName[T](name: String, t: T): String =
s"""isValid$name("$t")"""
def toLowerCase(r: Result[_]): String =
r.morph(passed, failed)
// Not
def showResultNotInnerPassed(expr: String): String =
s"$Predicate $expr did not fail."
def showResultNotInnerFailed(expr: String): String =
s"$Predicate $expr did not pass."
// And
def showResultAndBothPassed(expr: String): String =
s"$Both $predicates of $expr $passed."
def showResultAndRightFailed(expr: String, right: String): String =
s"$Right $predicate of $expr $failed: $right"
def showResultAndLeftFailed(expr: String, left: String): String =
s"$Left $predicate of $expr $failed: $left"
def showResultAndBothFailed(expr: String, left: String, right: String): String =
s"$Both $predicates of $expr $failed. $Left: $left $Right: $right"
// Or
def showResultOrBothPassed(expr: String): String =
s"$Both $predicates of $expr $passed."
def showResultOrRightPassed(expr: String): String =
s"$Right $predicate of $expr $passed."
def showResultOrLeftPassed(expr: String): String =
s"$Left $predicate of $expr $passed."
def showResultOrBothFailed(expr: String, left: String, right: String): String =
s"$Both $predicates of $expr $failed. $Left: $left $Right: $right"
//
val refineNonCompileTimeConstant =
"compile-time refinement only works with literals"
def invalidInference(from: String, to: String): String =
s"""type mismatch (invalid inference):
| $from does not imply
| $to
""".stripMargin.trim
}