@ollopa/trust
TypeScript icon, indicating that this package has built-in type declarations

0.0.1 • Public • Published

trust

Utility library to add Rust idioms into

installing

npm install @ollopa/trust
yarn add @ollopa/trust

Add "experimentalDecorators": true, to your tsconfig.json file.

Docs

The purpose of this library is to give Typescript ergonomic Rust idioms.

Please open a RFC issue if an API can be improved in any way.

match

Very open for suggestions on a better API for this.

WIP. Needs range, default, etc.

const token: { type: 'semi' | 'identifier' | 'unknown', value: string } = getToken()

const result = match(token.type,
  // match arms
  ['semi', handleSemi(token)],
  ['identifier', handleIdentifier(token)],
  ['unknown', handleUnknown(token)],
)

match returns an Option because of limitations with typing. For example...

match('some string',
  ['yeet', 'yoot']
)

...will never match to anything and therefore will return an Option.None.

Var

Utility type to create a sum type.

type ASTNode = 
  Var<'int' | 'string' | 'infix', { token: string }> // the `token` data is common across all nodes
  & ( // Enum variants
    Var<'int', { value: number }> | 
    Var<'string', { value: string }> |
    Var<'infix', { operator: '+' | '-', left: ASTNode, right: ASTNode }> 
  )

Package Sidebar

Install

npm i @ollopa/trust

Weekly Downloads

3,654

Version

0.0.1

License

MIT

Unpacked Size

43.4 kB

Total Files

18

Last publish

Collaborators

  • kierangill