nero-0.3.1: Lens-based HTTP toolkit

Safe HaskellSafe
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.

class Prefixed a where Source

This Prism' strips/prepends a prefix.

>>> ("/hello/there"::Text) ^? prefixed "/hello/"
Just "there"
>>> prefixed "/hello/" # ("there"::Text)
"/hello/there"

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

>>> ("hello"::Text) ^? prefixed "hello"
Just ""

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

>>> prefixed "hello" # (mempty::Text)
"hello"

An empty Match matches to itself regardless of the pattern.

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

Methods

prefixed :: Text -> Prism' a a Source

Instances

Prefixed Text Source 
Prefixed Match Source

Like Text instance but for the head of a Match

class Suffixed a where Source

This Prism' strips/appends a suffix.

>>> ("/hello/there"::Text) ^? suffixed "there"
Just "/hello/"
>>> suffixed "there" # ("/hello/"::Text)
"/hello/there"

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

>>> ("hello"::Text) ^? suffixed "hello"
Just ""

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

>>> suffixed "hello" # (mempty::Text)
"hello"

An empty Match matches to itself regardless of the pattern.

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

Methods

suffixed :: Text -> Prism' a a Source

Instances

Suffixed Text Source 
Suffixed Match Source

Like Text instance but for the head of a Match

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 []

split :: Text -> Fold Text Match Source

This is the composition of match and sep. Use this to avoid lifting a Match explicitly. Notice that, unlike sep, this is not reversible.

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 ()

Results handling

class Target a where Source

Prism' between a Match and a target type.