Skip to content

Commit 512ced7

Browse files
author
antoine
committed
initial commit
0 parents  commit 512ced7

27 files changed

+1090
-0
lines changed

.gitignore

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
target
2+
3+
.DS_Store
4+
.idea
5+
6+
.bloop
7+
.metals
8+
9+
project/target
10+
project/.bloop
11+
12+
*/target

.scalafmt.conf

+5
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
version = 2.0.0-RC5
2+
3+
maxColumn = 120
4+
5+
align.openParenCallSite = false

build.sbt

+12
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
name := "UrlDSL"
2+
3+
version := "0.1"
4+
5+
scalaVersion := "2.13.1"
6+
7+
scalacOptions ++= Seq("-feature", "-deprecation")
8+
9+
libraryDependencies ++= Seq(
10+
"org.scalatest" %% "scalatest" % "3.1.0" % "test",
11+
"org.scalacheck" %% "scalacheck" % "1.14.1" % "test"
12+
)

project/build.properties

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
sbt.version = 1.3.6

project/plugins.sbt

+1
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
addSbtPlugin("org.scalameta" % "sbt-scalafmt" % "2.0.0")
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
package urldsl.errors
2+
import urldsl.vocabulary.Segment
3+
4+
/**
5+
* Error type with only one instance, for when you only care about knowing whether the error exists.
6+
*/
7+
sealed trait DummyError
8+
9+
object DummyError {
10+
11+
final val dummyError: DummyError = new DummyError {}
12+
13+
implicit final lazy val dummyErrorIsParamMatchingError: ParamMatchingError[DummyError] =
14+
new ParamMatchingError[DummyError] {
15+
def missingParameterError(paramName: String): DummyError = dummyError
16+
17+
def fromThrowable(throwable: Throwable): DummyError = dummyError
18+
}
19+
20+
implicit final lazy val dummyErrorIsPathMatchingError: PathMatchingError[DummyError] =
21+
new PathMatchingError[DummyError] {
22+
def malformed(str: String): DummyError = dummyError
23+
24+
def endOfSegmentRequired(remainingSegments: List[Segment]): DummyError = dummyError
25+
26+
def wrongValue(expected: String, actual: String): DummyError = dummyError
27+
28+
def missingSegment: DummyError = dummyError
29+
30+
def fromThrowable(throwable: Throwable): DummyError = dummyError
31+
}
32+
33+
implicit final lazy val dummyErrorIsFromThrowable: ErrorFromThrowable[DummyError] = (_: Throwable) => dummyError
34+
35+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,13 @@
1+
package urldsl.errors
2+
3+
/**
4+
* You can implement this trait for your own error type A, and give a implicit instance in order to use the
5+
* functionality requiring it, e.g., in [[urldsl.vocabulary.FromString]].
6+
*
7+
* @tparam A tye type of your error.
8+
*/
9+
trait ErrorFromThrowable[A] {
10+
11+
def fromThrowable(throwable: Throwable): A
12+
13+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,14 @@
1+
package urldsl.errors
2+
3+
/**
4+
* You can implement this trait for your own error type `A`, and provide an implicit instance in the companion object of
5+
* `A` in order to use all pre-defined [[urldsl.language.QueryParameters]].
6+
*
7+
* @example
8+
* See implementations of [[DummyError]] and [[SimpleParamMatchingError]]
9+
*
10+
* @tparam A the type of your error.
11+
*/
12+
trait ParamMatchingError[A] {
13+
def missingParameterError(paramName: String): A
14+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,21 @@
1+
package urldsl.errors
2+
3+
import urldsl.vocabulary.Segment
4+
5+
/**
6+
* You can implement this trait for your own error type `A`, a giving an implicit instance in the companion object of
7+
* `A` so that it is available for all pre-defined [[urldsl.language.PathSegment]].
8+
*
9+
* @example
10+
* See implementations of [[DummyError]] or [[SimpleParamMatchingError]]
11+
*
12+
* @tparam A type of the error.
13+
*/
14+
trait PathMatchingError[A] {
15+
16+
def malformed(str: String): A
17+
def endOfSegmentRequired(remainingSegments: List[Segment]): A
18+
def wrongValue(expected: String, actual: String): A
19+
def missingSegment: A
20+
21+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
package urldsl.errors
2+
3+
sealed trait SimpleParamMatchingError
4+
5+
/**
6+
* An implementation of [[ParamMatchingError]] that simply wraps the trigger of the error inside its components.
7+
*/
8+
object SimpleParamMatchingError {
9+
10+
case class MissingParameterError(paramName: String) extends SimpleParamMatchingError
11+
case class FromThrowable(throwable: Throwable) extends SimpleParamMatchingError
12+
13+
implicit lazy val itIsParamMatchingError: ParamMatchingError[SimpleParamMatchingError] =
14+
(paramName: String) => MissingParameterError(paramName)
15+
16+
implicit lazy val simpleParamMatchingErrorIsFromThrowable: ErrorFromThrowable[SimpleParamMatchingError] =
17+
(throwable: Throwable) => FromThrowable(throwable)
18+
19+
}
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
package urldsl.errors
2+
3+
import urldsl.vocabulary.Segment
4+
5+
sealed trait SimplePathMatchingError
6+
7+
object SimplePathMatchingError {
8+
9+
final case class MalformedInt(str: String) extends SimplePathMatchingError
10+
final case class EndOfSegmentRequired(remainingSegments: Seq[Segment]) extends SimplePathMatchingError
11+
final case class WrongValue(expected: String, received: String) extends SimplePathMatchingError
12+
final case object MissingSegment extends SimplePathMatchingError
13+
final case class SimpleError(reason: String) extends SimplePathMatchingError
14+
15+
implicit lazy val pathMatchingError: PathMatchingError[SimplePathMatchingError] =
16+
new PathMatchingError[SimplePathMatchingError] {
17+
def malformed(str: String): SimplePathMatchingError = MalformedInt(str)
18+
19+
def endOfSegmentRequired(remainingSegments: List[Segment]): SimplePathMatchingError =
20+
EndOfSegmentRequired(remainingSegments)
21+
22+
def wrongValue(expected: String, actual: String): SimplePathMatchingError = WrongValue(expected, actual)
23+
24+
def missingSegment: SimplePathMatchingError = MissingSegment
25+
}
26+
27+
implicit lazy val errorFromThrowable: ErrorFromThrowable[SimplePathMatchingError] = (throwable: Throwable) =>
28+
SimpleError(throwable.getMessage)
29+
30+
}

0 commit comments

Comments
 (0)