Skip to content

Commit

Permalink
add some comments
Browse files Browse the repository at this point in the history
  • Loading branch information
jinzhongjia committed Jul 27, 2024
1 parent e0f137c commit 0d0a15a
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 0 deletions.
1 change: 1 addition & 0 deletions src/main.zig
Original file line number Diff line number Diff line change
Expand Up @@ -19,5 +19,6 @@ pub fn main() !void {
const args = try std.process.argsAlloc(allocator);
defer std.process.argsFree(allocator, args);

// parse the args and handle command
try command.handle_command(args);
}
1 change: 1 addition & 0 deletions src/tools.zig
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ pub fn get_allocator() std.mem.Allocator {
return allocator;
}

/// get zvm path segment
pub fn get_zvm_path_segment(tmp_allocator: std.mem.Allocator, segment: []const u8) ![]u8 {
return std.fs.path.join(
tmp_allocator,
Expand Down
12 changes: 12 additions & 0 deletions src/versions.zig
Original file line number Diff line number Diff line change
@@ -1,22 +1,31 @@
//! For getting zig version info json from offical website
const std = @import("std");
const config = @import("config.zig");

const uri = std.Uri.parse(config.download_manifest_url) catch unreachable;

pub const VersionList = struct {
// this type will store
const List = std.ArrayList([]const u8);

// store the version message
lists: List,
allocator: std.mem.Allocator,

/// init the VersionList
pub fn init(allocator: std.mem.Allocator) !VersionList {
// create a http client
var client = std.http.Client{ .allocator = allocator };
defer client.deinit();

// we ceate a buffer to store the http response
var buffer: [262144]u8 = undefined; // 256 * 1024 = 262kb

// try open a request
var req = try client.open(.GET, uri, .{ .server_header_buffer = &buffer });
defer req.deinit();

// send request and wait response
try req.send();
try req.wait();

Expand All @@ -26,6 +35,7 @@ pub const VersionList = struct {

const len = try req.readAll(buffer[0..]);

// parse json
const json = try std.json.parseFromSlice(std.json.Value, allocator, buffer[0..len], .{});
defer json.deinit();
const root = json.value;
Expand All @@ -47,10 +57,12 @@ pub const VersionList = struct {
};
}

// get the slice items
pub fn slice(self: *VersionList) [][]const u8 {
return self.lists.items;
}

/// deinit will free memory
pub fn deinit(self: *VersionList) void {
defer self.lists.deinit();
for (self.lists.items) |value| {
Expand Down

0 comments on commit 0d0a15a

Please sign in to comment.