Skip to content

Commit b71451b

Browse files
committed
add docs generate
1 parent 39f4901 commit b71451b

File tree

3 files changed

+101
-15
lines changed

3 files changed

+101
-15
lines changed

.github/workflows/deploy_docs.yml

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
name: DeployDocs
2+
3+
on:
4+
push:
5+
branches: [main]
6+
7+
# Allows you to run this workflow manually from the Actions tab
8+
workflow_dispatch:
9+
inputs:
10+
logLevel:
11+
description: "Log level"
12+
required: true
13+
default: "warning"
14+
tags:
15+
description: "deploy docs"
16+
17+
# Sets permissions of the GITHUB_TOKEN to allow deployment to GitHub Pages
18+
permissions:
19+
contents: write
20+
pages: write
21+
id-token: write
22+
23+
# Allow only one concurrent deployment, skipping runs queued between the run in-progress and latest queued.
24+
# However, do NOT cancel in-progress runs as we want to allow these production deployments to complete.
25+
concurrency:
26+
group: pages
27+
cancel-in-progress: false
28+
29+
jobs:
30+
# Build job
31+
build:
32+
runs-on: ubuntu-latest
33+
steps:
34+
- name: Checkout
35+
uses: actions/checkout@v3
36+
with:
37+
fetch-depth: 0 # Not needed if lastUpdated is not enabled
38+
- uses: goto-bus-stop/setup-zig@v2
39+
- name: Generate Docs
40+
run: zig build docs
41+
- name: Deploy
42+
uses: peaceiris/actions-gh-pages@v3
43+
with:
44+
github_token: ${{ secrets.GITHUB_TOKEN }}
45+
publish_branch: docs
46+
publish_dir: ./zig-out/docs

build_12.zig

Lines changed: 26 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,7 @@
11
const std = @import("std");
2+
const Build = std.Build;
3+
const Module = Build.Module;
4+
const OptimizeMode = std.builtin.OptimizeMode;
25

36
pub fn build(b: *std.Build) void {
47
const target = b.standardTargetOptions(.{});
@@ -22,6 +25,8 @@ pub fn build(b: *std.Build) void {
2225
},
2326
});
2427

28+
generateDocs(b, optimize, target);
29+
2530
const test_step = b.step("test", "Run unit tests");
2631

2732
const msgpack_unit_tests = b.addTest(.{
@@ -42,3 +47,24 @@ pub fn build(b: *std.Build) void {
4247
const run_msgpack_rpc_tests = b.addRunArtifact(msgpack_rpc_unit_tests);
4348
test_step.dependOn(&run_msgpack_rpc_tests.step);
4449
}
50+
51+
fn generateDocs(b: *Build, optimize: OptimizeMode, target: Build.ResolvedTarget) void {
52+
const lib = b.addStaticLibrary(.{
53+
.name = "zig-msgpack",
54+
.root_source_file = .{
55+
.path = "src/msgpack.zig",
56+
},
57+
.target = target,
58+
.optimize = optimize,
59+
});
60+
61+
const docs_step = b.step("docs", "Emit docs");
62+
63+
const docs_install = b.addInstallDirectory(.{
64+
.source_dir = lib.getEmittedDocs(),
65+
.install_dir = .prefix,
66+
.install_subdir = "docs",
67+
});
68+
69+
docs_step.dependOn(&docs_install.step);
70+
}

src/msgpack.zig

Lines changed: 29 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,11 @@ const little_endian = switch (current_zig.minor) {
2020
else => @compileError("not support current version zig"),
2121
};
2222

23+
/// the Str Type
2324
pub const Str = struct {
2425
str: []const u8,
26+
27+
/// get Str values
2528
pub fn value(self: Str) []const u8 {
2629
return self.str;
2730
}
@@ -32,8 +35,11 @@ pub fn wrapStr(str: []const u8) Str {
3235
return Str{ .str = str };
3336
}
3437

38+
/// the Bin Type
3539
pub const Bin = struct {
3640
bin: []u8,
41+
42+
/// get bin values
3743
pub fn value(self: Bin) []u8 {
3844
return self.bin;
3945
}
@@ -44,6 +50,7 @@ pub fn wrapBin(bin: []u8) Bin {
4450
return Bin{ .bin = bin };
4551
}
4652

53+
/// the EXT Type
4754
pub const EXT = struct {
4855
type: i8,
4956
data: []u8,
@@ -57,10 +64,14 @@ pub fn wrapEXT(t: i8, data: []u8) EXT {
5764
};
5865
}
5966

60-
// the map of payload
67+
/// the map of payload
6168
pub const Map = std.StringHashMap(Payload);
6269

70+
/// Entity to store msgpack
71+
///
72+
/// Note: The payload and its subvalues must have the same allocator
6373
pub const Payload = union(enum) {
74+
/// the error for Payload
6475
pub const Errors = error{
6576
NotMap,
6677
};
@@ -76,6 +87,7 @@ pub const Payload = union(enum) {
7687
map: Map,
7788
ext: EXT,
7889

90+
/// put a new element to map payload
7991
pub fn mapPut(self: *Payload, key: []const u8, val: Payload) !void {
8092
if (self.* != .map) {
8193
return Errors.NotMap;
@@ -85,36 +97,42 @@ pub const Payload = union(enum) {
8597
try self.map.put(new_key, val);
8698
}
8799

100+
/// get a NIL payload
88101
pub fn nilToPayload() Payload {
89102
return Payload{
90103
.nil = void{},
91104
};
92105
}
93106

107+
/// get a bool payload
94108
pub fn boolToPayload(val: bool) Payload {
95109
return Payload{
96110
.bool = val,
97111
};
98112
}
99113

114+
/// get a int payload
100115
pub fn intToPayload(val: i64) Payload {
101116
return Payload{
102117
.int = val,
103118
};
104119
}
105120

121+
/// get a uint payload
106122
pub fn uintToPayload(val: u64) Payload {
107123
return Payload{
108124
.uint = val,
109125
};
110126
}
111127

128+
/// get a float payload
112129
pub fn floatToPayload(val: f64) Payload {
113130
return Payload{
114131
.float = val,
115132
};
116133
}
117134

135+
/// get a str payload
118136
pub fn strToPayload(val: []const u8, allocator: Allocator) !Payload {
119137
// alloca memory
120138
const new_str = try allocator.alloc(u8, val.len);
@@ -125,6 +143,7 @@ pub const Payload = union(enum) {
125143
};
126144
}
127145

146+
/// get a bin payload
128147
pub fn binToPayload(val: []const u8, allocator: Allocator) !Payload {
129148
// alloca memory
130149
const new_bin = try allocator.alloc(u8, val.len);
@@ -135,21 +154,22 @@ pub const Payload = union(enum) {
135154
};
136155
}
137156

157+
/// get an array payload
138158
pub fn arrPayload(len: usize, allocator: Allocator) !Payload {
139159
const arr = try allocator.alloc(Payload, len);
140160
return Payload{
141161
.arr = arr,
142162
};
143163
}
144164

165+
/// get a map payload
145166
pub fn mapPayload(allocator: Allocator) Payload {
146167
return Payload{
147168
.map = Map.init(allocator),
148169
};
149170
}
150171

151-
// TODO: add map support
152-
172+
/// get an ext payload
153173
pub fn extToPayload(t: i8, data: []const u8, allocator: Allocator) !Payload {
154174
// alloca memory
155175
const new_data = try allocator.alloc(u8, data.len);
@@ -160,6 +180,8 @@ pub const Payload = union(enum) {
160180
};
161181
}
162182

183+
/// free the all memeory for this payload and sub payloads
184+
/// the allocator is payload's allocator
163185
pub fn free(self: *Payload, allocator: Allocator) void {
164186
switch (self.*) {
165187
.str => {
@@ -240,7 +262,7 @@ const Markers = enum(u8) {
240262
NEGATIVE_FIXINT = 0xe0,
241263
};
242264

243-
/// error set
265+
/// A collection of errors that may occur when reading the payload
244266
pub const MsGPackError = error{
245267
STR_DATA_LENGTH_TOO_LONG,
246268
BIN_DATA_LENGTH_TOO_LONG,
@@ -262,7 +284,7 @@ pub const MsGPackError = error{
262284
INTERNAL,
263285
};
264286

265-
/// main function
287+
/// Create an instance of msgpack_pack
266288
pub fn Pack(
267289
comptime WriteContext: type,
268290
comptime ReadContext: type,
@@ -728,7 +750,6 @@ pub fn Pack(
728750
try self.writeExtValue(ext);
729751
}
730752

731-
/// write EXT
732753
fn writeExt(self: Self, ext: EXT) !void {
733754
const len = ext.data.len;
734755
if (len == 1) {
@@ -752,6 +773,7 @@ pub fn Pack(
752773
}
753774
}
754775

776+
/// write payload
755777
pub fn write(self: Self, payload: Payload) !void {
756778
switch (payload) {
757779
.nil => {
@@ -817,13 +839,10 @@ pub fn Pack(
817839

818840
// TODO: add timestamp
819841

820-
//// read
821-
822842
fn readFrom(self: Self, bytes: []u8) !usize {
823843
return readFn(self.read_context, bytes);
824844
}
825845

826-
/// read one byte
827846
fn readByte(self: Self) !u8 {
828847
var res = [1]u8{0};
829848
const len = try self.readFrom(&res);
@@ -835,7 +854,6 @@ pub fn Pack(
835854
return res[0];
836855
}
837856

838-
/// read data
839857
fn readData(self: Self, allocator: Allocator, len: usize) ![]u8 {
840858
const data = try allocator.alloc(u8, len);
841859
errdefer allocator.free(data);
@@ -848,13 +866,11 @@ pub fn Pack(
848866
return data;
849867
}
850868

851-
/// read type marker u8
852869
fn readTypeMarkerU8(self: Self) !u8 {
853870
const val = try self.readByte();
854871
return val;
855872
}
856873

857-
/// convert marker u8 to marker
858874
fn markerU8To(_: Self, marker_u8: u8) Markers {
859875
var val = marker_u8;
860876

@@ -873,7 +889,6 @@ pub fn Pack(
873889
return @enumFromInt(val);
874890
}
875891

876-
/// read type marker
877892
fn readTypeMarker(self: Self) !Markers {
878893
const val = try self.readTypeMarkerU8();
879894
return self.markerU8To(val);
@@ -887,13 +902,11 @@ pub fn Pack(
887902
}
888903
}
889904

890-
/// read bool
891905
fn readBool(self: Self) !bool {
892906
const marker = try self.readTypeMarker();
893907
return self.readBoolValue(marker);
894908
}
895909

896-
/// read positive and negative fixint
897910
fn readFixintValue(_: Self, marker_u8: u8) i8 {
898911
return @bitCast(marker_u8);
899912
}
@@ -1228,6 +1241,7 @@ pub fn Pack(
12281241
}
12291242
}
12301243

1244+
/// read a payload, please use payload.free to free the memory
12311245
pub fn read(self: Self, allocator: Allocator) !Payload {
12321246
var res: Payload = undefined;
12331247

0 commit comments

Comments
 (0)