Skip to content

Commit ea88a2b

Browse files
committed
add auto update checker
1 parent d3a2f80 commit ea88a2b

12 files changed

Lines changed: 766 additions & 3 deletions

File tree

README.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -111,6 +111,8 @@ pub fn main() !void {
111111
}
112112
```
113113

114+
> **Note:** zigantic automatically checks for updates when using JSON functions. To disable, call `z.disableUpdateCheck()` at the start of your program.
115+
114116
### JSON Parsing with Validation
115117

116118
```zig

docs/.vitepress/config.mts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -49,6 +49,7 @@ export default defineConfig({
4949
{ text: 'Validation Types', link: '/guide/validation-types' },
5050
{ text: 'JSON Parsing', link: '/guide/json-parsing' },
5151
{ text: 'Error Handling', link: '/guide/error-handling' },
52+
{ text: 'Version & Updates', link: '/guide/version-updates' },
5253
],
5354
},
5455
],

docs/api/errors.md

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,3 +119,59 @@ if (!result.isValid()) {
119119
defer allocator.free(json_errors);
120120
}
121121
```
122+
123+
## Version Utilities
124+
125+
```zig
126+
z.getVersion() // "0.0.1"
127+
z.getVersionString() // "v0.0.1"
128+
z.ISSUES_URL // GitHub issues URL
129+
```
130+
131+
## Internal Error Reporting
132+
133+
::: warning
134+
Use only for **library bugs**, NOT for validation errors!
135+
:::
136+
137+
```zig
138+
// Report unexpected internal error
139+
z.reportInternalError("Unexpected null in parser");
140+
141+
// Report with error code
142+
z.reportInternalErrorWithCode(error.OutOfMemory);
143+
```
144+
145+
Output:
146+
147+
```
148+
[ZIGANTIC ERROR] Unexpected null in parser
149+
150+
If you believe this is a bug in zigantic, please report it at:
151+
https://github.com/muhammad-fiaz/zigantic/issues
152+
```
153+
154+
## Update Checking
155+
156+
```zig
157+
// Disable automatic update checking (call before using library)
158+
z.disableUpdateCheck();
159+
160+
// Or use custom config
161+
z.setConfig(.{
162+
.auto_update_check = false,
163+
.show_update_notifications = false,
164+
});
165+
166+
// Manual update check (background)
167+
if (z.checkForUpdates(allocator)) |thread| {
168+
defer thread.join();
169+
}
170+
171+
// Manual update check (synchronous)
172+
var info = try z.checkForUpdatesSync(allocator);
173+
defer info.deinit();
174+
if (info.update_available) {
175+
std.debug.print("Update: {s}\n", .{info.latest_version});
176+
}
177+
```

docs/api/json.md

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,10 @@
22

33
JSON parsing and serialization.
44

5+
::: tip Automatic Updates
6+
JSON functions (`fromJson`, `toJson`, `toJsonPretty`) automatically trigger a background update check on first use. To disable, call `z.disableUpdateCheck()` before using these functions.
7+
:::
8+
59
## Parsing
610

711
### z.fromJson

docs/guide/error-handling.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -115,3 +115,34 @@ var errors2 = z.errors.ErrorList.init(allocator);
115115
// ... add errors ...
116116
try errors1.merge(errors2);
117117
```
118+
119+
## Validation Errors vs Library Bugs
120+
121+
::: tip Important Distinction
122+
**Validation errors** are expected behavior when users provide invalid data. Handle them normally.
123+
124+
**Library bugs** are unexpected internal errors that might indicate a problem with zigantic itself.
125+
:::
126+
127+
### Validation Errors (Expected)
128+
129+
```zig
130+
// This is normal - user provided invalid data
131+
if (z.String(3, 50).init("Jo")) |name| {
132+
std.debug.print("Valid: {s}\n", .{name.get()});
133+
} else |err| {
134+
// Handle normally - this is NOT a library bug
135+
std.debug.print("Error: {s}\n", .{z.errorMessage(err)});
136+
}
137+
```
138+
139+
### Library Bugs (Unexpected)
140+
141+
Only use `reportInternalError` for unexpected situations that might be library bugs:
142+
143+
```zig
144+
// Only for unexpected internal errors
145+
z.reportInternalError("Unexpected null during parsing");
146+
```
147+
148+
This will print a message with the GitHub issues URL for reporting.

docs/guide/getting-started.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -56,14 +56,18 @@ pub fn build(b: *std.Build) void {
5656
### Step 3: Import and use
5757

5858
```zig
59+
const std = @import("std");
5960
const z = @import("zigantic");
6061
6162
pub fn main() !void {
63+
// Just use the library - no initialization needed!
6264
const name = try z.String(1, 50).init("Alice");
6365
std.debug.print("Hello, {s}!\n", .{name.get()});
6466
}
6567
```
6668

69+
> **Note:** zigantic automatically checks for updates when you use JSON functions. To disable this, call `z.disableUpdateCheck()` before using the library. See [Version & Updates](/guide/version-updates) for details.
70+
6771
## Your First Validation
6872

6973
Let's validate some data:
@@ -185,4 +189,5 @@ zig build example # Run basic example
185189
- **[Validation Types](/guide/validation-types)** - Complete type reference
186190
- **[JSON Parsing](/guide/json-parsing)** - Parse and serialize JSON
187191
- **[Error Handling](/guide/error-handling)** - Handle errors with codes
192+
- **[Version & Updates](/guide/version-updates)** - Automatic updates and error reporting
188193
- **[API Reference](/api/types)** - Full API documentation

docs/guide/version-updates.md

Lines changed: 211 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,211 @@
1+
# Version & Updates
2+
3+
zigantic includes automatic version checking and error reporting features to help you stay up-to-date and report issues easily.
4+
5+
## Automatic Update Checking
6+
7+
zigantic **automatically** checks for updates in the background when you first use JSON functions (`fromJson`, `toJson`, `toJsonPretty`). No initialization is needed!
8+
9+
When a new version is available, you'll see a log message:
10+
11+
```
12+
info: [UPDATE] A newer release of zigantic is available: v0.1.0 (current 0.0.1)
13+
```
14+
15+
### Basic Usage (Updates Enabled by Default)
16+
17+
```zig
18+
const std = @import("std");
19+
const z = @import("zigantic");
20+
21+
pub fn main() !void {
22+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
23+
defer _ = gpa.deinit();
24+
25+
// Just use the library - update check happens automatically!
26+
const name = try z.String(1, 50).init("Alice");
27+
std.debug.print("Hello, {s}!\n", .{name.get()});
28+
29+
// Update check triggers on first JSON use
30+
const User = struct { name: z.String(1, 50) };
31+
var result = try z.fromJson(User, "{\"name\": \"Bob\"}", gpa.allocator());
32+
defer result.deinit();
33+
}
34+
```
35+
36+
## Disabling Automatic Updates
37+
38+
To disable automatic update checking, call `disableUpdateCheck()` **before** using any library functions:
39+
40+
```zig
41+
const std = @import("std");
42+
const z = @import("zigantic");
43+
44+
pub fn main() !void {
45+
// Disable update checking (must be called before any other z.* functions)
46+
z.disableUpdateCheck();
47+
48+
// Now use the library normally - no update checks will occur
49+
const name = try z.String(1, 50).init("Alice");
50+
std.debug.print("Hello, {s}!\n", .{name.get()});
51+
}
52+
```
53+
54+
### Custom Configuration
55+
56+
For more control, use `setConfig()`:
57+
58+
```zig
59+
const z = @import("zigantic");
60+
61+
pub fn main() !void {
62+
// Customize configuration
63+
z.setConfig(.{
64+
.auto_update_check = false, // Disable update checking
65+
.show_update_notifications = false, // Disable notifications
66+
});
67+
68+
// Use library...
69+
}
70+
```
71+
72+
### Configuration Options
73+
74+
| Option | Type | Default | Description |
75+
| --------------------------- | ------ | ------- | ------------------------------------- |
76+
| `auto_update_check` | `bool` | `true` | Enable/disable automatic update check |
77+
| `show_update_notifications` | `bool` | `true` | Show update notifications in log |
78+
79+
## Library Version
80+
81+
Get the current version of zigantic:
82+
83+
```zig
84+
const z = @import("zigantic");
85+
86+
const ver = z.getVersion(); // "0.0.1"
87+
const full = z.getVersionString(); // "v0.0.1"
88+
```
89+
90+
## Manual Update Checking
91+
92+
You can also check for updates manually:
93+
94+
### Background Check (Non-blocking)
95+
96+
```zig
97+
const z = @import("zigantic");
98+
99+
pub fn main() !void {
100+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
101+
defer _ = gpa.deinit();
102+
103+
// Start background update check
104+
if (z.checkForUpdates(gpa.allocator())) |thread| {
105+
// Optional: wait for check to complete on shutdown
106+
defer thread.join();
107+
}
108+
}
109+
```
110+
111+
### Synchronous Check
112+
113+
```zig
114+
const z = @import("zigantic");
115+
116+
pub fn main() !void {
117+
var gpa = std.heap.GeneralPurposeAllocator(.{}){};
118+
defer _ = gpa.deinit();
119+
120+
// Check for updates synchronously
121+
var info = try z.checkForUpdatesSync(gpa.allocator());
122+
defer info.deinit();
123+
124+
if (info.update_available) {
125+
std.debug.print("Update available: {s} (current: {s})\n", .{
126+
info.latest_version,
127+
info.current_version,
128+
});
129+
}
130+
}
131+
```
132+
133+
## Internal Error Reporting
134+
135+
::: warning
136+
The error reporting functions are for **library bugs only** - unexpected internal errors that might indicate a bug in zigantic itself.
137+
138+
**Do NOT use these for validation errors!** Validation errors (like "value is too short") are expected behavior when users provide invalid data.
139+
:::
140+
141+
### When to Use Error Reporting
142+
143+
Use `reportInternalError` only when you encounter an unexpected situation in your own code that might be caused by a bug in zigantic:
144+
145+
```zig
146+
const z = @import("zigantic");
147+
148+
fn myLibraryFunction() !void {
149+
// Some unexpected internal error occurred
150+
// This might be a bug in zigantic's internals
151+
z.reportInternalError("Unexpected null value in parsed result");
152+
return error.InternalError;
153+
}
154+
```
155+
156+
This prints:
157+
158+
```
159+
[ZIGANTIC ERROR] Unexpected null value in parsed result
160+
161+
If you believe this is a bug in zigantic, please report it at:
162+
https://github.com/muhammad-fiaz/zigantic/issues
163+
```
164+
165+
### Validation Errors (Do NOT Report)
166+
167+
Validation errors are **expected behavior** - don't report them as bugs:
168+
169+
```zig
170+
const z = @import("zigantic");
171+
172+
pub fn main() void {
173+
// This is normal - validation errors are expected!
174+
if (z.String(3, 50).init("Jo")) |name| {
175+
std.debug.print("Valid: {s}\n", .{name.get()});
176+
} else |err| {
177+
// Handle normally - DO NOT call reportError()
178+
std.debug.print("Validation failed: {s}\n", .{z.errorMessage(err)});
179+
}
180+
}
181+
```
182+
183+
### Get Issues URL
184+
185+
```zig
186+
const z = @import("zigantic");
187+
188+
const url = z.ISSUES_URL; // "https://github.com/muhammad-fiaz/zigantic/issues"
189+
```
190+
191+
## API Reference
192+
193+
### Functions
194+
195+
| Function | Description |
196+
| ---------------------------------- | ------------------------------------------------------- |
197+
| `disableUpdateCheck()` | Disable automatic update checking |
198+
| `setConfig(config)` | Set custom configuration |
199+
| `getConfig()` | Get current configuration |
200+
| `getVersion()` | Get version string (e.g., "0.0.1") |
201+
| `getVersionString()` | Get full version (e.g., "v0.0.1") |
202+
| `checkForUpdates(allocator)` | Manually check for updates in background |
203+
| `checkForUpdatesSync(allocator)` | Manually check for updates synchronously |
204+
| `reportInternalError(msg)` | Report internal library bug (not for validation errors) |
205+
| `reportInternalErrorWithCode(err)` | Report internal library bug with error code |
206+
207+
### Constants
208+
209+
| Constant | Description |
210+
| ------------ | ------------------------------------ |
211+
| `ISSUES_URL` | GitHub issues URL for reporting bugs |

docs/index.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -33,9 +33,9 @@ features:
3333
- icon: 🔄
3434
title: JSON Parsing
3535
details: Parse and serialize JSON with automatic validation, nested struct support, and default values.
36-
- icon:
37-
title: 102 Tests
38-
details: Comprehensive test coverage ensuring reliability and correctness across all features.
36+
- icon: 🔔
37+
title: Automatic Updates
38+
details: Checks for library updates automatically in the background. Easily disable with one function call.
3939
---
4040

4141
<style>

0 commit comments

Comments
 (0)