nero-0.2: Lens-based HTTP toolkit

Safe HaskellSafe-Inferred
LanguageHaskell2010

Nero.Match

Contents

Description

This module should be mostly used for matching the Path of a Request, also known as routing.

Synopsis

Matching

type Match = [Text] Source

This contains matched Text in reverse order to how it was matched.

match :: Getter Text Match Source

This is just to pure with a refined type.

prefixed :: Text -> Prism' Match Match Source

This Prism' strips/prepends a prefix to the first element of a Match.

>>> pure "/hello/there" ^? prefixed "/hello/" . _head
Just "there"
>>> prefixed "/hello/" # pure "there" & view _head
"/hello/there"

If matching the entire source it previews to an empty Text. You might use exact if you are expecting this behavior.

>>> pure "hello" ^? prefixed "hello" . _head
Just ""

This also means that to review an entire source, you need to give it an empty Text.

>>> prefixed "hello" # pure mempty & view _head
"hello"

An empty Match matches to itself regardless of the pattern.

>>> preview (prefixed "hello") (review (prefixed "hello") mempty) <&> is _Empty
Just True

suffixed :: Text -> Prism' Match Match Source

This Prism' strips/appends a suffix to the first value of a Match.

>>> pure "/hello/there" ^? suffixed "there" . _head
Just "/hello/"
>>> suffixed "there" # pure "/hello/" & view _head
"/hello/there"

If matching the entire source it previews to an empty Text. You might use exact if you are expecting this behavior.

>>> pure "hello" ^? suffixed "hello" . _head
Just ""

This also means that to review an entire source, you need to give it an empty Text.

>>> suffixed "hello" # pure mempty & view _head
"hello"

An empty Match matches to itself regardless of the pattern.

>>> preview (suffixed "hello") (review (suffixed "hello") mempty) <&> is _Empty
Just True

exact :: Text -> Prism' Text () Source

This is just an alias to only. Use this to match the entirety of the source. The source mustn't be lifted to a Match.

>>> "hello" ^? exact "hello"
Just ()

sep :: Text -> Prism' Match Match Source

This Prism' splits/joins at the first occurrence of a boundary for the first value of a Match.

>>> pure "hello/out/there" ^? sep "/" <&> toListOf folded
Just ["out/there","hello"]
>>> sep "/" # (pure "out/there" <> pure "hello") & view _head
"hello/out/there"

Notice what happens when there is no source before or after a boundary:

>>> pure "hello/" ^? sep "/"
Just ["","hello"]
>>> (pure "hello/" <> pure "there") ^? sep "/"
Just ["","hello","there"]
>>> pure "/hello" ^? sep "/"
Just ["hello",""]
>>> (pure "/hello" <> pure "there") ^? sep "/"
Just ["hello","","there"]

When the source is identical to the boundary: >>> pure "hello" ^? sep "hello" Just []

Results handling

class Target a where Source

Prism' between a Match and a target type.

Instances