-
Notifications
You must be signed in to change notification settings - Fork 155
/
string.scala
56 lines (38 loc) · 2.22 KB
/
string.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
package eu.timepit.refined
import eu.timepit.refined.InferenceRuleAlias.==>
import eu.timepit.refined.string._
import shapeless.Witness
object string extends StringPredicates with StringInferenceRules {
/** Predicate that checks if a `String` ends with the suffix `S`. */
trait EndsWith[S]
/** Predicate that checks if a `String` matches the regular expression `R`. */
trait MatchesRegex[R]
/** Predicate that checks if a `String` is a valid regular expression. */
trait Regex
/** Predicate that checks if a `String` starts with the prefix `S`. */
trait StartsWith[S]
/** Predicate that checks if a `String` is a valid URI. */
trait Uri
/** Predicate that checks if a `String` is a valid URL. */
trait Url
}
trait StringPredicates {
implicit def endsWithPredicate[R <: String](implicit wr: Witness.Aux[R]): Predicate[EndsWith[R], String] =
Predicate.instance(_.endsWith(wr.value), t => s""""$t".endsWith("${wr.value}")""")
implicit def matchesRegexPredicate[R <: String](implicit wr: Witness.Aux[R]): Predicate[MatchesRegex[R], String] =
Predicate.instance(_.matches(wr.value), t => s""""$t".matches("${wr.value}")""")
implicit val regexPredicate: Predicate[Regex, String] =
Predicate.fromPartial(_.r, t => s"""isValidRegex("$t")""")
implicit def startsWithPredicate[R <: String](implicit wr: Witness.Aux[R]): Predicate[StartsWith[R], String] =
Predicate.instance(_.startsWith(wr.value), t => s""""$t".startsWith("${wr.value}")""")
implicit val uriPredicate: Predicate[Uri, String] =
Predicate.fromPartial(new java.net.URI(_), t => s"""isValidUri("$t")""")
implicit val urlPredicate: Predicate[Url, String] =
Predicate.fromPartial(new java.net.URL(_), t => s"""isValidUrl("$t")""")
}
trait StringInferenceRules {
implicit def endsWithInference[A <: String, B <: String](implicit wa: Witness.Aux[A], wb: Witness.Aux[B]): EndsWith[A] ==> EndsWith[B] =
InferenceRule(wa.value.endsWith(wb.value), s"endsWithInference(${wa.value}, ${wb.value})")
implicit def startsWithInference[A <: String, B <: String](implicit wa: Witness.Aux[A], wb: Witness.Aux[B]): StartsWith[A] ==> StartsWith[B] =
InferenceRule(wa.value.startsWith(wb.value), s"startsWithInference(${wa.value}, ${wb.value})")
}