{"id":19332200,"url":"https://github.com/4t145/bincode-ts","last_synced_at":"2025-04-22T16:41:28.973Z","repository":{"id":115780920,"uuid":"576873502","full_name":"4t145/bincode-ts","owner":"4t145","description":"A library to decode/encode bincode to JavaScript/TypeScript object.","archived":false,"fork":false,"pushed_at":"2025-01-18T05:50:02.000Z","size":52,"stargazers_count":4,"open_issues_count":0,"forks_count":1,"subscribers_count":1,"default_branch":"master","last_synced_at":"2025-03-29T16:51:12.092Z","etag":null,"topics":["bincode","decoding","encoding","javascript","rust","serde","typescript"],"latest_commit_sha":null,"homepage":"","language":"TypeScript","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":null,"status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/4t145.png","metadata":{"files":{"readme":"readme.md","changelog":null,"contributing":null,"funding":null,"license":null,"code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null,"roadmap":null,"authors":null,"dei":null,"publiccode":null,"codemeta":null}},"created_at":"2022-12-11T09:10:18.000Z","updated_at":"2025-03-11T05:40:15.000Z","dependencies_parsed_at":null,"dependency_job_id":"b9258006-bafc-499e-b539-c4ca270cd0f1","html_url":"https://github.com/4t145/bincode-ts","commit_stats":null,"previous_names":[],"tags_count":0,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4t145%2Fbincode-ts","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4t145%2Fbincode-ts/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4t145%2Fbincode-ts/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/4t145%2Fbincode-ts/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/4t145","download_url":"https://codeload.github.com/4t145/bincode-ts/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":250278843,"owners_count":21404271,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":["bincode","decoding","encoding","javascript","rust","serde","typescript"],"created_at":"2024-11-10T02:44:36.079Z","updated_at":"2025-04-22T16:41:28.954Z","avatar_url":"https://github.com/4t145.png","language":"TypeScript","funding_links":[],"categories":[],"sub_categories":[],"readme":"# BincodeTs\n\nA library to decode/encode [bincode](https://github.com/bincode-org/bincode) to JavaScript object.\n\n## Install\n\n`npm install bincode-ts`\n\n## Usage\n\n```typescript\nimport { RustType, Decoder, Encoder } from \"bincode-ts\";\nconst { u32, Vec, Struct, Str } = RustType;\n// TypeScript type defination\ntype MyStruct = {\n  message: string;\n  code: number;\n  tags: Array\u003cnumber\u003e;\n};\n\n// create a corresponded rust type defination\nconst MyStrcut = Struct\u003cMyStruct\u003e([\n  [\"message\", Str],\n  [\"code\", u32],\n  [\"tags\", Vec(u32)],\n]);\n\n// the js object to encode\nconst data: MyStruct = {\n  message: \"给他一点小小的ts震撼\",\n  code: 200,\n  tags: [20, 5, 8, 6001],\n};\n\n// encode\nconst encoder = new Encoder();\nconst bincode = encoder.init().encodeAs(data, MyStrcut);\nconsole.log(\"bincode is\", bincode.buffer);\n\n// decode\nconst decoder = new Decoder();\nconst decodedData = decoder.load(bincode.buffer).decodeAs(MyStrcut);\nconsole.log(\"decoded object is\", decodedData);\n```\n\n## Rust Type Defination\n\nA rust type defination is a object implements interface `RustType.Type\u003cData\u003e`, where generic parameter `Data` is the corresponded TypeScript type.\n\n### Primitive Types\n\n```typescript\nimport { RustType } from \"bincode-ts\";\n// here are primitive types\nconst { i8, u8, i16, u16, i32, u32, i64, u64, f32, f64, empty } = RustType;\n```\n\nThe `empty` type is corresponded to `()` type in Rust，it will by decoded as `undefined` .\n\n`i64` and `f64` is corresponded to `bigint` is JavaScript.\n### Compound Types\n\n```typescript\nimport { RustType } from \"bincode-ts\";\n// here are compound type constructor functions\nconst { Struct, Tuple, Enum } = RustType;\n```\n\n#### Struct\n\n`RustType.Struct` is a function to create `Struct` type defination, it accepts an array of `[string, RustType.Type]`.\n\n```typescript\n// TypeScript type defination\ntype MyStruct = {\n  message: string;\n  code: number;\n  empty: undefined;\n};\n\n// create a corresponded rust type defination\nconst MyStrcut = Struct\u003cMyStruct\u003e([\n  [\"message\", Str],\n  [\"code\", u32],\n  [\"empty\", empty],\n]);\n```\n\n#### Tuple\n\n`RustType.Tuple` is a function to create `Tuple` type defination, it accepts an array of `RustType.Type`.\n\n```typescript\n// TypeScript type defination\ntype MyTuple = [string, number, boolean];\n\n// create a corresponded rust type defination\nconst MyStrcut = Tuple\u003cMyTuple\u003e([Str, i32, bool]);\n```\n\n#### Enum\n\n`RustType.Enum` is a function to create `Enum` type defination, it's a little bit more complex than previous two. It accepts an array of `[RustType.Type]`.\n\nAn bincode Enum value will be decoed as an EnumData\n```typescript\ntype EnumData\u003cT\u003e = {\n    variant: number,\n    data: T\n}\n```\n\n```typescript\nimport { enumData, EnumData, Variant } from \"bincode-ts\";\nexport namespace MyEnum {\n  // ts type of this enum variants code\n  export enum _ {\n    BigInt,\n    Tuple,\n    Empty = 5,\n  }\n  // variants\n  export type BigInt = Variant\u003c_.BigInt, bigint\u003e;\n  export type Tuple = Variant\u003c_.Tuple, [number, string]\u003e;\n  export type Empty = Variant\u003c_.Empty, undefined\u003e;\n\n  // ts type of this enum\n  export type $ =\n    | BigInt \n    | Tuple \n    | Empty;\n\n  // Corresponed RustType\n  export const Type: Type\u003c$\u003e = Enum\u003c$\u003e({\n    [_.BigInt]: i64,\n    [_.Tuple]: myTuple,\n    [_.Empty]: empty,\n  });\n}\n\n// create js objects of this enum\nlet myEnumData0 = enumData\u003cMyEnum.$, MyEnum.BigInt\u003e(\n  MyEnum._.BigInt,\n  123456789n\n);\n\nlet myEnumData1 = enumData\u003cMyEnum.$, MyEnum.Tuple\u003e(MyEnum._.Tuple, [\n  0,\n  \"hello\",\n]);\n\nlet myEnumData2 = enumData\u003cMyEnum.$, MyEnum.Empty\u003e(MyEnum._.Empty, undefined);\n```\n\n### Array Types\nFunction `Arr` is for `Array` type in Rust, \n```typescript\nconst { Arr } = RustType;\n\n// it creates a type of `[i32; 10]`, and will be decoded as `numebr[]`\nconst MyArr = Arr\u003cnumber\u003e(i32, 10);\n\n```\n\n### Collections\n```typescript\nconst { Vec, HashMap, HashSet } = RustType;\n\n// it implements Type\u003cArray\u003cnumber\u003e\u003e\nconst MyVec = Vec\u003cnumber\u003e(i32);\n\n// it implements Type\u003cMap\u003cstring, bigint\u003e\u003e\nconst MyHashMap = HashMap\u003cstring, bigint\u003e(Str, u64);\n\n// it implements Type\u003cSet\u003cnumber\u003e\u003e\nconst MyHashSet = HashSet\u003cnumber\u003e(i8);\n```\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F4t145%2Fbincode-ts","html_url":"https://awesome.ecosyste.ms/projects/github.com%2F4t145%2Fbincode-ts","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2F4t145%2Fbincode-ts/lists"}