-
Notifications
You must be signed in to change notification settings - Fork 155
/
string.scala
53 lines (45 loc) · 1.63 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
package eu.timepit.refined.util
import eu.timepit.refined.api.Refined
import eu.timepit.refined.string._
/**
* Module for statically checking constructors of types that can be
* instantiated with `String`s.
*
* @see [[https://github.com/fthomas/refined/blob/master/docs/util_string.md]]
*/
object string {
/**
* Creates a `[[scala.util.matching.Regex]]` from a validated string.
*
* Example: {{{
* scala> import eu.timepit.refined.auto._
* | import eu.timepit.refined.util.string.regex
*
* scala> regex(".*")
* res1: scala.util.matching.Regex = .*
*
* scala> regex("{")
* <console>:47: error: Regex predicate failed: Illegal repetition
* {
* regex("{")
* ^
* }}}
*/
def regex(s: String Refined Regex): scala.util.matching.Regex =
new scala.util.matching.Regex(s.value)
/** Creates a `java.net.URI` from a validated string. */
def uri(s: String Refined Uri): java.net.URI =
new java.net.URI(s.value)
/** Creates a `java.net.URL` from a validated string. */
def url(s: String Refined Url): java.net.URL =
new java.net.URL(s.value)
/** Creates a `java.net.UUID` from a validated string. */
def uuid(s: String Refined Uuid): java.util.UUID =
java.util.UUID.fromString(s.value)
/** Creates a `scala.xml.Elem` from a validated string. */
def xml(s: String Refined Xml): scala.xml.Elem =
scala.xml.XML.loadString(s.value)
/** Creates a `javax.xml.xpath.XPathExpression` from a validated string. */
def xpath(s: String Refined XPath): javax.xml.xpath.XPathExpression =
javax.xml.xpath.XPathFactory.newInstance().newXPath().compile(s.value)
}