deriveables

A package to create deriveable types in Nim

Pure Nim score 15/100 · tests present · no docs generated

Summary

Latest Version 0.1.0
License MIT
CI Status Failing
Downloads 0
Last Indexed 2026-08-30 07:10

Tags

Authors

  • PMunch

Installation

nimble install deriveables
choosenim install deriveables
git clone https://github.com/PMunch/deriveables

OS Compatibility

Platform Linux macOS Windows FreeBSD OpenBSD NetBSD Android iOS WASM Embedded
deriveables - - - - - - -

Dependencies

Package Version Optional
nim >= 1.6.4 No

Source

Repository https://github.com/PMunch/deriveables
Homepage https://github.com/PMunch/deriveables
Registry Source nimble_official

README

Deriveable types

This module implements deriveable types. The concept is simple, you supply converters to go from one or more types into another type, then you can define procedures with a nice type signature and tell the system to derive the procedure from the converters. This generates a new procedure which takes the base type, runs all the necessarry converters, and calls the initial typed procedure.

Example: In the Jester web framework you get access to the Request object in your routes. From this object you're going to extract the information about the request that you need and then perform the logic of the route. This means that routes typically contain quite a bit of parsing logic. With this library you can create procedures that only does the extraction, then procedures which only does the business logic, and simply tell this library to glue them together.

.. code-block:: nim

import jester, strutils, json, deriveables

type UserID = distinct int User = object name: string

# Define ways to get various things from a request, this would typically be # done with parsing, verification and database access proc getUserId(r: Request): UserId {.deriveable.} = return UserID(r.params["id"].parseInt)

proc getUser(u: UserID): User {.deriveable.} = # Do database lookup for the user ID and return a user object discard

# You could also attach the deriveable pragma to existing procs instead of # the above pragma: # deriveable(getUserId, getUser)

# Define our route bodies purely on our logical types, then derive a # procedure from a set of given types proc showUser(p: User): string {.derive: Request.} = "This is the page for user " & p.name & "" # If you want to automatically convert types to HTML via templates have # a look at PMunch/autotemplate

# Set up our routes, call our logical routes with the request object and # the system will figure out how to derive the actual types routes: get "/user/@id": resp showUser(request)

If a type conversion fails it will throw a DerivingError which just wraps the actual error. This means that it's easy to distinguish errors happening during the conversion and errors happening during the execution of a procedure itself.

This file is automatically generated from the documentation found in deriveables.nim. Use nim doc src/deriveables.nim to get the full documentation.