Skip to content

Commit

Permalink
zig/db- add Remote repo details parsing
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro committed Dec 13, 2021
1 parent 2f72a10 commit 26f7abf
Showing 1 changed file with 35 additions and 0 deletions.
35 changes: 35 additions & 0 deletions src/db/Remote.zig
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ pub const Remote = struct {
added: bool,
};

pub const RepoDetails = struct {
id: string,
name: string,
clone_url: string,
description: string,
default_branch: string,
star_count: u32,
};

pub const findUserBy = _internal.FindByGen(Remote, User, .provider, .id).first;

pub fn byKey(alloc: *std.mem.Allocator, comptime key: std.meta.FieldEnum(Remote), value: extras.FieldType(Remote, key)) !?Remote {
Expand Down Expand Up @@ -120,6 +129,32 @@ pub const Remote = struct {
.github => "https://api.github.com",
};
}

pub fn getRepo(self: Remote, alloc: *std.mem.Allocator, repo: string) !RepoDetails {
return self.parseDetails(
alloc,
(try self.apiRequest(
alloc,
null,
try std.mem.join(alloc, "/", switch (self.type) {
.github => &.{ "", "repos", repo },
}),
)) orelse return error.ApiRequestFail,
);
}

fn parseDetails(self: Remote, alloc: *std.mem.Allocator, raw: json.Value) !RepoDetails {
return switch (self.type) {
.github => .{
.id = try std.fmt.allocPrint(alloc, "{}", .{raw.getT("id", .Int).?}),
.name = raw.getT("name", .String).?,
.clone_url = raw.getT("clone_url", .String).?,
.description = raw.getT("description", .String) orelse "",
.default_branch = raw.getT("default_branch", .String).?,
.star_count = @intCast(u32, raw.getT("stargazers_count", .Int).?),
},
};
}
};

fn containsPackage(haystack: []const Package, id: string, name: string) bool {
Expand Down

0 comments on commit 26f7abf

Please sign in to comment.