regex-wrapper: Types that can only be constructed if they match a regular expression

This is a package candidate release! Here you can preview how this package release will appear once published to the main package index (which can be accomplished via the 'maintain' link below). Please note that once a package has been published to the main package index it cannot be undone! Please consult the package uploading documentation for more information.

[maintain] [Publish]

Provides tooling for working with types whose values must match a regular expression provided in the type.


[Skip to Readme]

Properties

Versions 0.1.0.0, 0.1.0.0, 0.1.0.1
Change log CHANGELOG.md
Dependencies aeson, base (>=4.12.0.0 && <4.13), bytestring, case-insensitive, containers, hashable, regex-tdfa, string-conv, text [details]
License BSD-3-Clause
Copyright (c) 2019 Luke Clifton
Author Luke Clifton
Maintainer lukec@themk.net
Category Data
Bug tracker https://github.com/luke-clifton/regex-wrapper/issues
Source repo head: git clone https://github.com/luke-clifton/regex-wrapper
Uploaded by lukec at 2019-11-11T03:04:18Z

Modules

[Index] [Quick Jump]

Downloads

Maintainer's Corner

Package maintainers

For package maintainers and hackage trustees


Readme for regex-wrapper-0.1.0.0

[back to package description]

Experimental

This library allows you to create types that are guaranteed to contain a string that matches a given regular expression which is expressed at the type level.

newtype User = User (Matched String "^[a-zA-Z0-9]{4,15}$")

parseUser :: String -> Either (RegexError String) User
parseUser = fmap User . parseMatchedEither

prettyUser :: User -> String
prettyUser (User m) = asString m

main :: IO ()
main = do
    l <- getLine
    case parseUser l of
        Right user -> putStrLn $ "Hello, " ++ prettyUser user ++ "!"
        Left error -> putStrLn $ "Could not parse username: " ++ prettyRegexError  error
./prog
bad
Could not parse username: The input "bad" did not match the pattern ^[a-zA-Z0-9]{4,15}$
./prog
good
Hello, good!