Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
[#57] Add a libra module
Fixes #57.
  • Loading branch information
nrinaudo committed Jan 25, 2018
1 parent c14a9e7 commit df4ac01
Show file tree
Hide file tree
Showing 6 changed files with 139 additions and 2 deletions.
21 changes: 19 additions & 2 deletions build.sbt
Expand Up @@ -15,7 +15,7 @@ lazy val root = Project(id = "kantan-regex", base = file("."))
|import kantan.regex.refined._
""".stripMargin
)
.aggregate(cats, core, docs, enumeratum, generic, jodaTime, laws, refined, scalaz)
.aggregate(cats, core, docs, enumeratum, generic, jodaTime, laws, libra, refined, scalaz)
.aggregateIf(java8Supported)(java8)
.dependsOn(core, generic, refined)

Expand All @@ -25,7 +25,7 @@ lazy val docs = project
inAnyProject -- inProjectsIf(!java8Supported)(java8)
)
.enablePlugins(DocumentationPlugin)
.dependsOn(cats, core, enumeratum, generic, jodaTime, refined, scalaz)
.dependsOn(cats, core, enumeratum, generic, jodaTime, libra, refined, scalaz)
.dependsOnIf(java8Supported)(java8)

// - core projects -----------------------------------------------------------------------------------------------------
Expand Down Expand Up @@ -179,3 +179,20 @@ lazy val enumeratum = project
"org.scalatest" %% "scalatest" % Versions.scalatest % "test"
)
)

// - Libra project -----------------------------------------------------------------------------------------------------
// ---------------------------------------------------------------------------------------------------------------------
lazy val libra = project
.settings(
moduleName := "kantan.regex-libra",
name := "libra"
)
.enablePlugins(PublishedPlugin)
.dependsOn(core, laws % "test")
.settings(
libraryDependencies ++= Seq(
"com.nrinaudo" %% "kantan.codecs-libra" % Versions.kantanCodecs,
"com.nrinaudo" %% "kantan.codecs-libra-laws" % Versions.kantanCodecs % "test",
"org.scalatest" %% "scalatest" % Versions.scalatest % "test"
)
)
3 changes: 3 additions & 0 deletions docs/src/main/tut/index.md
Expand Up @@ -41,6 +41,9 @@ libraryDependencies += "com.nrinaudo" %% "kantan.regex-refined" % "@VERSION@"

// Provides enumeratum decoders.
libraryDependencies += "com.nrinaudo" %% "kantan.regex-enumeratum" % "@VERSION@"

// Provides libra decoders.
libraryDependencies += "com.nrinaudo" %% "kantan.regex-libra" % "@VERSION@"
```

## Motivation
Expand Down
36 changes: 36 additions & 0 deletions docs/src/main/tut/libra.md
@@ -0,0 +1,36 @@
---
layout: tutorial
title: "Libra module"
section: tutorial
sort_order: 15
---
kantan.regex comes with a [libra](https://github.com/to-ithaca/libra) module that can be used
by adding the following dependency to your `build.sbt`:

```scala
libraryDependencies += "com.nrinaudo" %% "kantan.regex-libra" % "@VERSION@"
```

You then need to import the corresponding package:

```tut:silent
import kantan.regex.libra._
```

And that's pretty much it. You can now decode refined types directly.

Let's first set our types up:

```tut:silent
import libra._
import libra.si._
import kantan.regex.implicits._
type Duration = QuantityOf[Int, Time, Second]
```

We can then simply write the following:

```tut
"[123]".evalRegex[Duration](rx"\[([+-]?\d+)\]", 1).toList
```
21 changes: 21 additions & 0 deletions libra/src/main/scala/kantan/regex/libra/package.scala
@@ -0,0 +1,21 @@
/*
* Copyright 2016 Nicolas Rinaudo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package kantan.regex

import kantan.codecs.libra.DecoderInstances

package object libra extends DecoderInstances
39 changes: 39 additions & 0 deletions libra/src/test/scala/kantan/regex/libra/LibraCodecTests.scala
@@ -0,0 +1,39 @@
/*
* Copyright 2016 Nicolas Rinaudo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package kantan.regex
package libra

import _root_.libra.Quantity
import arbitrary._
import laws.discipline._
import shapeless.HNil

class LibraCodecTests extends DisciplineSuite {

checkAll("GroupDecoder[Quantity[Double, HNil]]", GroupDecoderTests[Quantity[Double, HNil]].decoder[String, Float])
checkAll("GroupDecoder[Quantity[Double, HNil]]", SerializableTests[GroupDecoder[Quantity[Double, HNil]]].serializable)

checkAll("MatchDecoder[Quantity[Double, HNil]]", MatchDecoderTests[Quantity[Double, HNil]].decoder[String, Float])
checkAll("MatchDecoder[Quantity[Double, HNil]]", SerializableTests[MatchDecoder[Quantity[Double, HNil]]].serializable)

checkAll("GroupDecoder[Quantity[Int, HNil]]", GroupDecoderTests[Quantity[Int, HNil]].decoder[String, Float])
checkAll("GroupDecoder[Quantity[Int, HNil]]", SerializableTests[GroupDecoder[Quantity[Int, HNil]]].serializable)

checkAll("MatchDecoder[Quantity[Int, HNil]]", MatchDecoderTests[Quantity[Int, HNil]].decoder[String, Float])
checkAll("MatchDecoder[Quantity[Int, HNil]]", SerializableTests[MatchDecoder[Quantity[Int, HNil]]].serializable)

}
21 changes: 21 additions & 0 deletions libra/src/test/scala/kantan/regex/libra/arbitrary.scala
@@ -0,0 +1,21 @@
/*
* Copyright 2016 Nicolas Rinaudo
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/

package kantan.regex
package libra

object arbitrary
extends kantan.regex.laws.discipline.ArbitraryInstances with kantan.codecs.libra.laws.discipline.ArbitraryInstances

0 comments on commit df4ac01

Please sign in to comment.