README.md

# TypeID Gleam

[![Package Version](https://img.shields.io/hexpm/v/typeid_gleam)](https://hex.pm/packages/typeid_gleam)
[![Hex Docs](https://img.shields.io/badge/hex-docs-ffaff3)](https://hexdocs.pm/typeid_gleam/)

[TypeID (v0.3)](https://github.com/jetify-com/typeid) implementation in Gleam.

## Installation

```sh
gleam add typeid_gleam@1
```

## Usage

```gleam
import typeid.{type TypeId}

type User {
  // User is passed into TypeId as a phantom type
  User(id: TypeId(User), name: String)
}

type Post

pub fn main() {
  let assert Ok(id) = typeid.new("user")
  let string_id = typeid.to_string(id) // "user_01h455vb4pex5vsknk084sn02q"

  let assert Ok(id) = typeid.parse(string_id)
  typeid.prefix(id) // "user"
  typeid.suffix(id) // "01h455vb4pex5vsknk084sn02q"

  let user_json = "{ \"id\": \"user_01h455vb4pex5vsknk084sn02q\", \"name\": \"Lucy\" }"

  let decoder = {
    // prefix is passed here to ensure the id is of type User
    use id <- decode.field("id", typeid.decode("user"))
    use name <- decode.field("name", decode.string)
    decode.success(User(id:, name:))
  }

  let assert Ok(user) = json.parse(user_json, decoder)
  user.name // "Lucy"
  typeid.prefix(user.id) // "user"
  typeid.suffix(user.id) // "01h455vb4pex5vsknk084sn02q"
  
  post_id_to_string(user.id) // won't compile because of the phantom type
}

fn post_id_to_string(id: TypeId(Post)) -> String {
  typeid.to_string(id) 
}
```

Further documentation can be found at <https://hexdocs.pm/typeid_gleam>.

## Development

```sh
gleam test  # Run the tests
```