Skip to content

Commit fedd9e6

Browse files
committed
docs(readme): update and expand documentation
- add project badges and improved introduction - provide detailed installation and usage instructions - include code examples for encoding and decoding - document API overview and testing steps - add contributing guidelines and related projects - clarify license information
1 parent 9e86de8 commit fedd9e6

File tree

1 file changed

+111
-29
lines changed

1 file changed

+111
-29
lines changed

README.md

Lines changed: 111 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -1,47 +1,129 @@
1+
# zig-msgpack
12

2-
# MessagePack for Zig
3-
This is an implementation of [MessagePack](https://msgpack.org/index.html) for [Zig](https://ziglang.org/).
3+
[![CI](https://github.com/zigcc/zig-msgpack/actions/workflows/ci.yml/badge.svg)](https://github.com/zigcc/zig-msgpack/actions/workflows/ci.yml)
44

5-
an article introducing it: [Zig Msgpack](https://blog.nvimer.org/2025/05/03/zig-msgpack/)
5+
A MessagePack implementation for the Zig programming language. This library provides a simple and efficient way to serialize and deserialize data using the MessagePack format.
6+
7+
An article introducing it: [Zig Msgpack](https://blog.nvimer.org/2025/05/03/zig-msgpack/)
68

79
## Features
810

9-
- Supports all MessagePack types(except timestamp)
10-
- Efficient encoding and decoding
11-
- Simple and easy-to-use API
11+
- **Full MessagePack Support:** Implements all MessagePack types (except for the timestamp extension).
12+
- **Efficient:** Designed for high performance with minimal memory overhead.
13+
- **Type-Safe:** Leverages Zig's type system to ensure safety during serialization and deserialization.
14+
- **Simple API:** Offers a straightforward and easy-to-use API for encoding and decoding.
1215

13-
## NOTE
16+
## Installation
1417

15-
The current protocol implementation has been completed, but it has not been fully tested.
16-
Only limited unit testing has been conducted, which does not cover everything.
18+
> For Zig 0.13 and older versions, please use version `0.0.6` of this library.
1719
18-
## Getting Started
20+
For Zig `0.14.0` and `nightly`, follow these steps:
1921

20-
> About 0.13 and previous versions, please use `0.0.6`
22+
1. **Add as a dependency:**
23+
Add the library to your `build.zig.zon` file. You can fetch a specific commit or branch.
2124

22-
### `0.14.0` \ `nightly`
25+
```sh
26+
zig fetch --save https://github.com/zigcc/zig-msgpack/archive/{COMMIT_OR_BRANCH}.tar.gz
27+
```
2328

24-
1. Add to `build.zig.zon`
29+
2. **Configure your `build.zig`:**
30+
Add the `zig-msgpack` module to your executable.
2531

26-
```sh
27-
zig fetch --save https://github.com/zigcc/zig-msgpack/archive/{commit or branch}.tar.gz
28-
# Of course, you can also use git+https to fetch this package!
29-
```
32+
```zig
33+
const std = @import("std");
34+
35+
pub fn build(b: *std.Build) void {
36+
const target = b.standardTargetOptions(.{});
37+
const optimize = b.standardOptimizeOption(.{});
38+
39+
const exe = b.addExecutable(.{
40+
.name = "my-app",
41+
.root_source_file = .{ .path = "src/main.zig" },
42+
.target = target,
43+
.optimize = optimize,
44+
});
3045
31-
2. Config `build.zig`
46+
const msgpack_dep = b.dependency("zig_msgpack", .{
47+
.target = target,
48+
.optimize = optimize,
49+
});
50+
51+
exe.root_module.addImport("msgpack", msgpack_dep.module("msgpack"));
52+
53+
b.installArtifact(exe);
54+
}
55+
```
56+
57+
## Usage
58+
59+
Here is a simple example of how to encode and decode a `Payload`:
3260

3361
```zig
34-
// To standardize development, maybe you should use `lazyDependency()` instead of `dependency()`
35-
const msgpack = b.dependency("zig-msgpack", .{
36-
.target = target,
37-
.optimize = optimize,
38-
});
39-
40-
// add module
41-
exe.root_module.addImport("msgpack", msgpack.module("msgpack"));
62+
const std = @import("std");
63+
const msgpack = @import("msgpack");
64+
const allocator = std.testing.allocator;
65+
66+
pub fn main() !void {
67+
var buffer: [1024]u8 = undefined;
68+
var stream = std.io.fixedBufferStream(&buffer);
69+
70+
var packer = msgpack.Pack(
71+
*std.io.FixedBufferStream([]u8),
72+
*std.io.FixedBufferStream([]u8),
73+
std.io.FixedBufferStream([]u8).WriteError,
74+
std.io.FixedBufferStream([]u8).ReadError,
75+
std.io.FixedBufferStream([]u8).write,
76+
std.io.FixedBufferStream([]u8).read,
77+
).init(&stream, &stream);
78+
79+
// Create a map payload
80+
var map = msgpack.Payload.mapPayload(allocator);
81+
defer map.free(allocator);
82+
83+
try map.mapPut("message", try msgpack.Payload.strToPayload("Hello, MessagePack!", allocator));
84+
try map.mapPut("version", msgpack.Payload.uintToPayload(1));
85+
86+
// Encode
87+
try packer.write(map);
88+
89+
// Reset stream for reading
90+
stream.pos = 0;
91+
92+
// Decode
93+
const decoded_payload = try packer.read(allocator);
94+
defer decoded_payload.free(allocator);
95+
96+
// Use the decoded data
97+
const message = (try decoded_payload.mapGet("message")).?.str.value();
98+
const version = (try decoded_payload.mapGet("version")).?.uint;
99+
100+
std.debug.print("Message: {s}, Version: {d}
101+
", .{ message, version });
102+
}
103+
```
104+
105+
## API Overview
106+
107+
- **`msgpack.Pack`**: The main struct for packing and unpacking MessagePack data. It is initialized with read and write contexts.
108+
- **`msgpack.Payload`**: A union that represents any MessagePack type. It provides methods for creating and interacting with different data types (e.g., `mapPayload`, `strToPayload`, `mapGet`).
109+
110+
## Testing
111+
112+
To run the unit tests for this library, use the following command:
113+
114+
```sh
115+
zig build test
42116
```
43117
44-
## Related projects
118+
## Contributing
119+
120+
Contributions are welcome! Please feel free to open an issue or submit a pull request.
121+
122+
## Related Projects
123+
124+
- [getty-msgpack](https://git.mzte.de/LordMZTE/getty-msgpack)
125+
- [znvim](https://github.com/jinzhongjia/znvim)
126+
127+
## License
45128
46-
- [getty-msgpack](https://git.mzte.de/LordMZTE/getty-msgpack)
47-
- [znvim](https://github.com/jinzhongjia/znvim)
129+
This project is licensed under the MIT License. See the [LICENSE](LICENSE) file for details.

0 commit comments

Comments
 (0)