MyNixOS website logo
Description

Type safe string transformations.

See README.md in the project github repository.

typed-encoding

Type level annotations, string transformations, and other goodies that make programming strings safer.

Motivation

I have recently spent a lot of time troubleshooting various Base64, quoted-printable, and UTF-8 encoding issues.
I decided to write a library that will help avoiding issues like these.

This library allows to specify and work with types like

-- some data encoded in base 64
mydata :: Enc '["enc-B64"] c ByteString

-- some text (utf8) data encoded in base 64 
myData :: Enc '["enc-B64", "r-UTF8"] c ByteString

It allows to define precise string content annotations like:

ipaddr :: Enc '["r-IpV4"] c Text

and provides ways for

  • encoding
  • decoding
  • recreation (encoding validation)
  • type conversions
  • converting types to encoded strings
  • typesafe conversion of encoded strings to types

Partial and dangerous things like decodeUtf8 are no longer dangerous, ByteString Text conversions become fully reversible. Life is good!

... but this approach seems to be a bit more...

-- upper cased text encoded as base64
example :: Enc '["enc-B64", "do-UPPER"] () T.Text
example = encodeAll . toEncoding () $ "some text goes here"

It becomes a type directed, declarative approach to string transformations.

Transformations can be

  • used with parameters
  • applied or undone partially (if encoding is reversible)

One of more interesting uses of this library are encoding restrictions included in this library.
Example are (arbitrary) bounded alpha-numeric ("r-ban") restrictions.

-- allow only properly formatted phone numbers

type PhoneSymbol = "r-ban:999-999-9999"
phone :: Enc '[PhoneSymbol] () T.Text
phone = ... 

The author often uses typed-encoding with Servant (HttpApiData instances are not included), e.g.:

type LookupByPhone = 
  "customer"
  :> "byphone"
  :> Capture "phone" (Enc '[PhoneSymbol] () T.Text)
  :> Get '[JSON] ([Customer])

or to get type safety over text document using Unix vs Windows line breaks!

Goals and limitations

The main goal is to provide improved type safety for programs that use string encodings and transformations. Not to provide encoding implementation type safety. Encoding and string manipulation libraries are typically well established and tested, type safety is really needed at the usage site, not at the implementation site.

This library approach is to fight issues with (value level) strings using (type level) strings. Using Symbol-s effectively forces us to play the orphan instances game.
One of the long term goals is for this library to provide combinator alternatives to typeclass polymorphism so that the orphan instances are more of a convenience and not the necessity.

Examples

Here are some code examples:

Hackage

https://hackage.haskell.org/package/typed-encoding

Other encoding packages

My approach will be to write specific encodings (e.g. HTTP) or wrap encodings from other packages using separate "bridge" projects.

Currently /typed-encoding/ depends on

  • /base64-bytestring/ because it was my driving example, this is likely to move out to a separate bridge project at some point.

Bridge work:

Tested with

  • stack (1.9.3) lts-14.27 (ghc-8.6.5)
  • stack (2.5.1) lts-16.27 (ghc-8.8.4)
  • needs ghc >= 8.2.2, base >=4.10 for GHC.TypeLits support

Known issues

  • running test suite: cabal has problems with doctest, use stack
    https://github.com/haskell/cabal/issues/6087
Metadata

Version

0.5.2.3

Platforms (75)

    Darwin
    FreeBSD
    Genode
    GHCJS
    Linux
    MMIXware
    NetBSD
    none
    OpenBSD
    Redox
    Solaris
    WASI
    Windows
Show all
  • aarch64-darwin
  • aarch64-genode
  • aarch64-linux
  • aarch64-netbsd
  • aarch64-none
  • aarch64_be-none
  • arm-none
  • armv5tel-linux
  • armv6l-linux
  • armv6l-netbsd
  • armv6l-none
  • armv7a-darwin
  • armv7a-linux
  • armv7a-netbsd
  • armv7l-linux
  • armv7l-netbsd
  • avr-none
  • i686-cygwin
  • i686-darwin
  • i686-freebsd
  • i686-genode
  • i686-linux
  • i686-netbsd
  • i686-none
  • i686-openbsd
  • i686-windows
  • javascript-ghcjs
  • loongarch64-linux
  • m68k-linux
  • m68k-netbsd
  • m68k-none
  • microblaze-linux
  • microblaze-none
  • microblazeel-linux
  • microblazeel-none
  • mips-linux
  • mips-none
  • mips64-linux
  • mips64-none
  • mips64el-linux
  • mipsel-linux
  • mipsel-netbsd
  • mmix-mmixware
  • msp430-none
  • or1k-none
  • powerpc-netbsd
  • powerpc-none
  • powerpc64-linux
  • powerpc64le-linux
  • powerpcle-none
  • riscv32-linux
  • riscv32-netbsd
  • riscv32-none
  • riscv64-linux
  • riscv64-netbsd
  • riscv64-none
  • rx-none
  • s390-linux
  • s390-none
  • s390x-linux
  • s390x-none
  • vc4-none
  • wasm32-wasi
  • wasm64-wasi
  • x86_64-cygwin
  • x86_64-darwin
  • x86_64-freebsd
  • x86_64-genode
  • x86_64-linux
  • x86_64-netbsd
  • x86_64-none
  • x86_64-openbsd
  • x86_64-redox
  • x86_64-solaris
  • x86_64-windows