Skip to content

Commit

Permalink
zig- update in-app files to 0.9.0
Browse files Browse the repository at this point in the history
  • Loading branch information
nektro committed Dec 25, 2021
1 parent 0f16676 commit 8f0ddb3
Show file tree
Hide file tree
Showing 14 changed files with 55 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/cookies.zig
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ const http = @import("apple_pie");

pub const Jar = std.StringHashMap(string);

pub fn parse(alloc: *std.mem.Allocator, headers: http.Request.Headers) !Jar {
pub fn parse(alloc: std.mem.Allocator, headers: http.Request.Headers) !Jar {
var map = Jar.init(alloc);
const h = headers.get("Cookie");
if (h == null) return map;
Expand Down
10 changes: 5 additions & 5 deletions src/db/Package.zig
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ pub const Package = struct {

pub const table_name = "packages";

pub fn create(alloc: *std.mem.Allocator, owner: User, name: string, remote: Remote, rm_id: string, rm_name: string, desc: string, license: string, star_count: u64) !Package {
pub fn create(alloc: std.mem.Allocator, owner: User, name: string, remote: Remote, rm_id: string, rm_name: string, desc: string, license: string, star_count: u64) !Package {
db.mutex.lock();
defer db.mutex.unlock();

Expand All @@ -51,19 +51,19 @@ pub const Package = struct {

usingnamespace _internal.ByKeyGen(Package);

pub fn latest(alloc: *std.mem.Allocator) ![]const Package {
pub fn latest(alloc: std.mem.Allocator) ![]const Package {
return try db.collect(alloc, Package, "select * from packages order by id desc limit 15", .{});
}

pub fn topStarred(alloc: *std.mem.Allocator) ![]const Package {
pub fn topStarred(alloc: std.mem.Allocator) ![]const Package {
return try db.collect(alloc, Package, "select * from packages order by star_count desc limit 15", .{});
}

pub fn versions(self: Package, alloc: *std.mem.Allocator) ![]const Version {
pub fn versions(self: Package, alloc: std.mem.Allocator) ![]const Version {
return try Version.byKeyAll(alloc, .p_for, self.uuid);
}

pub fn setLatest(self: Package, alloc: *std.mem.Allocator, vers: Version) !void {
pub fn setLatest(self: Package, alloc: std.mem.Allocator, vers: Version) !void {
try self.updateColumn(alloc, .latest_version, try std.fmt.allocPrint(alloc, "{}", .{vers}));
}
};
18 changes: 9 additions & 9 deletions src/db/Remote.zig
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ pub const Remote = struct {

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 {
pub fn byKey(alloc: std.mem.Allocator, comptime key: std.meta.FieldEnum(Remote), value: extras.FieldType(Remote, key)) !?Remote {
for (try all(alloc)) |item| {
const a = @field(item, @tagName(key));
if (@TypeOf(value) == string and std.mem.eql(u8, a, value)) {
Expand All @@ -60,12 +60,12 @@ pub const Remote = struct {
return null;
}

pub fn all(alloc: *std.mem.Allocator) ![]const Remote {
pub fn all(alloc: std.mem.Allocator) ![]const Remote {
if (all_remotes.len > 0) return all_remotes;
return db.collect(alloc, Remote, "select * from " ++ table_name, .{});
}

pub fn create(alloc: *std.mem.Allocator, ty: Type, domain: string) !Remote {
pub fn create(alloc: std.mem.Allocator, ty: Type, domain: string) !Remote {
db.mutex.lock();
defer db.mutex.unlock();

Expand All @@ -76,7 +76,7 @@ pub const Remote = struct {
});
}

pub fn listUserRepos(self: Remote, alloc: *std.mem.Allocator, user: User) ![]const Repo {
pub fn listUserRepos(self: Remote, alloc: std.mem.Allocator, user: User) ![]const Repo {
var list = std.ArrayList(Repo).init(alloc);
const pkgs = try user.packages(alloc);

Expand All @@ -96,7 +96,7 @@ pub const Remote = struct {
return list.toOwnedSlice();
}

fn apiRequest(self: Remote, alloc: *std.mem.Allocator, user: ?User, endpoint: string) !?json.Value {
fn apiRequest(self: Remote, alloc: std.mem.Allocator, user: ?User, endpoint: string) !?json.Value {
const url = try std.mem.concat(alloc, u8, &.{ try self.apiRoot(), endpoint });
defer alloc.free(url);

Expand Down Expand Up @@ -130,7 +130,7 @@ pub const Remote = struct {
};
}

pub fn getRepo(self: Remote, alloc: *std.mem.Allocator, repo: string) !RepoDetails {
pub fn getRepo(self: Remote, alloc: std.mem.Allocator, repo: string) !RepoDetails {
return self.parseDetails(
alloc,
(try self.apiRequest(
Expand All @@ -143,7 +143,7 @@ pub const Remote = struct {
);
}

fn parseDetails(self: Remote, alloc: *std.mem.Allocator, raw: json.Value) !RepoDetails {
pub 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).?}),
Expand All @@ -166,7 +166,7 @@ pub const Remote = struct {
},
};

pub fn installWebhook(self: Remote, alloc: *std.mem.Allocator, user: User, rm_id: string, rm_name: string, hookurl: string) !?json.Value {
pub fn installWebhook(self: Remote, alloc: std.mem.Allocator, user: User, rm_id: string, rm_name: string, hookurl: string) !?json.Value {
_ = rm_id;
return switch (self.type) {
.github => try self.apiPost(alloc, user, try std.mem.concat(alloc, u8, &.{ "/repos/", rm_name, "/hooks" }), GithubWebhookData{
Expand All @@ -181,7 +181,7 @@ pub const Remote = struct {
};
}

fn apiPost(self: Remote, alloc: *std.mem.Allocator, user: ?User, endpoint: string, data: anytype) !?json.Value {
fn apiPost(self: Remote, alloc: std.mem.Allocator, user: ?User, endpoint: string, data: anytype) !?json.Value {
const url = try std.mem.concat(alloc, u8, &.{ try self.apiRoot(), endpoint });
defer alloc.free(url);

Expand Down
8 changes: 4 additions & 4 deletions src/db/Time.zig
Original file line number Diff line number Diff line change
Expand Up @@ -7,13 +7,13 @@ pub const Time = struct {

const Self = @This();

pub fn formatAlloc(self: Self, alloc: *std.mem.Allocator, comptime fmt: string) !string {
pub fn formatAlloc(self: Self, alloc: std.mem.Allocator, comptime fmt: string) !string {
return self.ctx.formatAlloc(alloc, fmt);
}

pub const BaseType = string;

pub fn readField(alloc: *std.mem.Allocator, value: BaseType) !Self {
pub fn readField(alloc: std.mem.Allocator, value: BaseType) !Self {
_ = alloc;
return Self{
.ctx = time.DateTime.init(
Expand All @@ -27,12 +27,12 @@ pub const Time = struct {
};
}

pub fn bindField(self: Self, alloc: *std.mem.Allocator) !BaseType {
pub fn bindField(self: Self, alloc: std.mem.Allocator) !BaseType {
// modified RFC3339
return try self.formatAlloc(alloc, "YYY-MM-DD HH:mm:ss");
}

pub fn toString(self: Self, alloc: *std.mem.Allocator) !string {
pub fn toString(self: Self, alloc: std.mem.Allocator) !string {
// RFC1123
return try self.formatAlloc(alloc, "ddd, DD MMM YYY HH:mm:ss z");
}
Expand Down
6 changes: 3 additions & 3 deletions src/db/User.zig
Original file line number Diff line number Diff line change
Expand Up @@ -20,7 +20,7 @@ pub const User = struct {

pub const table_name = "users";

pub fn create(alloc: *std.mem.Allocator, provider: u64, snowflake: string, name: string) !User {
pub fn create(alloc: std.mem.Allocator, provider: u64, snowflake: string, name: string) !User {
db.mutex.lock();
defer db.mutex.unlock();

Expand All @@ -37,11 +37,11 @@ pub const User = struct {

pub const findPackageBy = _internal.FindByGen(User, Package, .owner, .uuid).first;

pub fn packages(self: User, alloc: *std.mem.Allocator) ![]const Package {
pub fn packages(self: User, alloc: std.mem.Allocator) ![]const Package {
return try Package.byKeyAll(alloc, .owner, self.uuid);
}

pub fn remote(self: User, alloc: *std.mem.Allocator) !Remote {
pub fn remote(self: User, alloc: std.mem.Allocator) !Remote {
for (try Remote.all(alloc)) |item| {
if (item.id == self.provider) {
return item;
Expand Down
10 changes: 5 additions & 5 deletions src/db/Version.zig
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ pub const Version = struct {

pub const table_name = "versions";

pub fn create(alloc: *std.mem.Allocator, pkg: Package, commit: string, unpackedsize: u64, totalsize: u64, files: []const string, tarsize: u64, tarhash: string, deps: []const zigmod.Dep, rootdeps: []const zigmod.Dep, builddeps: []const zigmod.Dep) !Version {
pub fn create(alloc: std.mem.Allocator, pkg: Package, commit: string, unpackedsize: u64, totalsize: u64, files: []const string, tarsize: u64, tarhash: string, deps: []const zigmod.Dep, rootdeps: []const zigmod.Dep, builddeps: []const zigmod.Dep) !Version {
db.mutex.lock();
defer db.mutex.unlock();

Expand All @@ -58,7 +58,7 @@ pub const Version = struct {

usingnamespace _internal.ByKeyGen(Version);

pub fn latest(alloc: *std.mem.Allocator) ![]const Version {
pub fn latest(alloc: std.mem.Allocator) ![]const Version {
return try db.collect(alloc, Version, "select * from versions order by id desc limit 15", .{});
}

Expand All @@ -68,7 +68,7 @@ pub const Version = struct {
try writer.print("v{d}.{d}", .{ self.real_major, self.real_minor });
}

pub fn setVersion(self: Version, alloc: *std.mem.Allocator, approver: User, major: u32, minor: u32) !void {
pub fn setVersion(self: Version, alloc: std.mem.Allocator, approver: User, major: u32, minor: u32) !void {
try self.updateColumn(alloc, .approved_by, try approver.uuid.toString(alloc));
try self.updateColumn(alloc, .real_major, major);
try self.updateColumn(alloc, .real_minor, minor);
Expand All @@ -81,7 +81,7 @@ const DepList = struct {
const Self = @This();
pub const BaseType = string;

pub fn readField(alloc: *std.mem.Allocator, value: BaseType) !Self {
pub fn readField(alloc: std.mem.Allocator, value: BaseType) !Self {
var res = std.ArrayList(zigmod.Dep).init(alloc);
var iter = std.mem.split(u8, value, "\n");
while (iter.next()) |line| {
Expand All @@ -103,7 +103,7 @@ const DepList = struct {
return DepList{ .deps = res.toOwnedSlice() };
}

pub fn bindField(self: Self, alloc: *std.mem.Allocator) !BaseType {
pub fn bindField(self: Self, alloc: std.mem.Allocator) !BaseType {
var res = std.ArrayList(u8).init(alloc);
defer res.deinit();
const w = res.writer();
Expand Down
2 changes: 1 addition & 1 deletion src/db/_db.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ pub const Package = @import("./Package.zig").Package;
pub const Version = @import("./Version.zig").Version;
pub const Time = @import("./Time.zig").Time;

pub fn connect(alloc: *std.mem.Allocator, path: string) !void {
pub fn connect(alloc: std.mem.Allocator, path: string) !void {
const abspath = try std.fs.path.resolve(alloc, &.{path});
const nulpath = try extras.addSentinel(alloc, u8, abspath, 0);
db.* = try Engine.connect(nulpath);
Expand Down
18 changes: 9 additions & 9 deletions src/db/_internal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ const _db = @import("./_db.zig");

pub fn ByKeyGen(comptime T: type) type {
return struct {
pub fn byKey(alloc: *std.mem.Allocator, comptime key: std.meta.FieldEnum(T), value: extras.FieldType(T, key)) !?T {
pub fn byKey(alloc: std.mem.Allocator, comptime key: std.meta.FieldEnum(T), value: extras.FieldType(T, key)) !?T {
return try db.first(
alloc,
T,
Expand All @@ -22,7 +22,7 @@ pub fn ByKeyGen(comptime T: type) type {
);
}

pub fn byKeyAll(alloc: *std.mem.Allocator, comptime key: std.meta.FieldEnum(T), value: extras.FieldType(T, key)) ![]const T {
pub fn byKeyAll(alloc: std.mem.Allocator, comptime key: std.meta.FieldEnum(T), value: extras.FieldType(T, key)) ![]const T {
return try db.collect(
alloc,
T,
Expand All @@ -31,7 +31,7 @@ pub fn ByKeyGen(comptime T: type) type {
);
}

pub fn updateColumn(self: T, alloc: *std.mem.Allocator, comptime key: std.meta.FieldEnum(T), value: extras.FieldType(T, key)) !void {
pub fn updateColumn(self: T, alloc: std.mem.Allocator, comptime key: std.meta.FieldEnum(T), value: extras.FieldType(T, key)) !void {
return try db.exec(
alloc,
"update " ++ T.table_name ++ " set " ++ @tagName(key) ++ " = ? where id = ?",
Expand All @@ -47,7 +47,7 @@ pub fn ByKeyGen(comptime T: type) type {
pub fn FindByGen(comptime S: type, comptime H: type, searchCol: std.meta.FieldEnum(H), selfCol: std.meta.FieldEnum(S)) type {
const querystub = "select * from " ++ H.table_name ++ " where " ++ @tagName(searchCol) ++ " = ?";
return struct {
pub fn first(self: S, alloc: *std.mem.Allocator, comptime key: std.meta.FieldEnum(H), value: extras.FieldType(H, key)) !?H {
pub fn first(self: S, alloc: std.mem.Allocator, comptime key: std.meta.FieldEnum(H), value: extras.FieldType(H, key)) !?H {
const query = querystub ++ " and " ++ @tagName(key) ++ " = ?";
return try db.first(
alloc,
Expand Down Expand Up @@ -101,7 +101,7 @@ fn Merge(comptime T: type) type {
return Struct(fields);
}

pub fn insert(alloc: *std.mem.Allocator, value: anytype) !std.meta.Child(@TypeOf(value)) {
pub fn insert(alloc: std.mem.Allocator, value: anytype) !std.meta.Child(@TypeOf(value)) {
const T = std.meta.Child(@TypeOf(value));
@field(value, "id") = try nextId(alloc, T);
comptime var parens: string = "";
Expand All @@ -113,18 +113,18 @@ pub fn insert(alloc: *std.mem.Allocator, value: anytype) !std.meta.Child(@TypeOf
return value.*;
}

fn nextId(alloc: *std.mem.Allocator, comptime T: type) !u64 {
fn nextId(alloc: std.mem.Allocator, comptime T: type) !u64 {
const n = try db.first(alloc, u64, "select id from " ++ T.table_name ++ " order by id desc limit 1", .{});
return (n orelse 0) + 1;
}

pub fn createTableT(alloc: *std.mem.Allocator, comptime T: type) !void {
pub fn createTableT(alloc: std.mem.Allocator, comptime T: type) !void {
const tI = @typeInfo(T).Struct;
const fields = tI.fields;
try createTable(alloc, T.table_name, comptime colToCol(fields[0]), comptime fieldsToCols(fields[1..]));
}

fn createTable(alloc: *std.mem.Allocator, comptime name: string, comptime pk: [2]string, comptime cols: []const [2]string) !void {
fn createTable(alloc: std.mem.Allocator, comptime name: string, comptime pk: [2]string, comptime cols: []const [2]string) !void {
if (try db.doesTableExist(alloc, name)) {} else {
std.log.scoped(.db).info("creating table '{s}' with primary column '{s}'", .{ name, pk[0] });
try db.exec(alloc, comptime std.fmt.comptimePrint("create table {s}({s} {s})", .{ name, pk[0], pk[1] }), .{});
Expand Down Expand Up @@ -169,7 +169,7 @@ fn typeToSqliteType(comptime T: type) string {
}

/// workaround for https://github.com/ziglang/zig/issues/6706
pub fn safeJoin(alloc: *std.mem.Allocator, separator: string, slices: []const string) !string {
pub fn safeJoin(alloc: std.mem.Allocator, separator: string, slices: []const string) !string {
var res = std.ArrayList(u8).init(alloc);
defer res.deinit();
try res.append('w');
Expand Down
2 changes: 1 addition & 1 deletion src/docker.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const std = @import("std");

const strings = @import("./strings.zig");

pub fn amInside(alloc: *std.mem.Allocator) !bool {
pub fn amInside(alloc: std.mem.Allocator) !bool {
const max = std.math.maxInt(usize);
const c = try std.fs.cwd().readFileAlloc(alloc, "/proc/1/cgroup", max);
var it = std.mem.split(u8, c, "\n");
Expand Down
2 changes: 1 addition & 1 deletion src/git.zig
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ const std = @import("std");
const string = []const u8;

/// Returns the result of running `git rev-parse HEAD`
pub fn rev_HEAD(alloc: *std.mem.Allocator, dir: std.fs.Dir) !string {
pub fn rev_HEAD(alloc: std.mem.Allocator, dir: std.fs.Dir) !string {
const max = std.math.maxInt(usize);
const dirg = try dir.openDir(".git", .{});
const h = std.mem.trim(u8, try dirg.readFileAlloc(alloc, "HEAD", max), "\n");
Expand Down
6 changes: 3 additions & 3 deletions src/handler/_handler.zig
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ const _dashboard = @import("./dashboard.zig");
const _import = @import("./import.zig");
const _do_import = @import("./do_import.zig");

pub fn init(alloc: *std.mem.Allocator) !void {
pub fn init(alloc: std.mem.Allocator) !void {
var secret_seed: [std.rand.DefaultCsprng.secret_seed_length]u8 = undefined;
std.crypto.random.bytes(&secret_seed);
var csprng = std.rand.DefaultCsprng.init(secret_seed);
Expand Down Expand Up @@ -103,13 +103,13 @@ pub fn saveInfo(response: *http.Response, request: http.Request, idp: oauth2.Pro
const alloc = request.arena;
const r = (try db.Remote.byKey(alloc, .domain, idp.domain())) orelse unreachable;
const u = (try r.findUserBy(alloc, .snowflake, id)) orelse try db.User.create(alloc, r.id, id, name);
const ulid = try std.mem.dupe(_internal.access_tokens.allocator, u8, try u.uuid.toString(alloc));
const ulid = try _internal.access_tokens.allocator.dupe(u8, try u.uuid.toString(alloc));

try response.headers.put("Set-Cookie", try std.fmt.allocPrint(alloc, "jwt={s}", .{
try _internal.JWT.encodeMessage(alloc, ulid),
}));
try _internal.cleanMaps();
try _internal.access_tokens.put(ulid, try std.mem.dupe(_internal.access_tokens.allocator, u8, val.get("access_token").?.String));
try _internal.access_tokens.put(ulid, try _internal.access_tokens.allocator.dupe(u8, val.get("access_token").?.String));
try _internal.token_liveness.put(ulid, std.time.timestamp());
try _internal.token_expires.put(ulid, val.getT("expires_in", .Int) orelse std.time.s_per_day);
}
Expand Down
10 changes: 5 additions & 5 deletions src/handler/_internal.zig
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ pub var token_liveness: std.StringHashMap(i64) = undefined;
pub var token_expires: std.StringHashMap(i64) = undefined;
pub var last_check: i64 = 0;

pub fn writePageResponse(alloc: *std.mem.Allocator, response: *http.Response, request: http.Request, comptime name: string, data: anytype) !void {
pub fn writePageResponse(alloc: std.mem.Allocator, response: *http.Response, request: http.Request, comptime name: string, data: anytype) !void {
_ = request;
try response.headers.put("Content-Type", "text/html");

Expand Down Expand Up @@ -62,7 +62,7 @@ pub const JWT = struct {
return q.get("jwt");
}

pub fn encodeMessage(alloc: *std.mem.Allocator, msg: string) !string {
pub fn encodeMessage(alloc: std.mem.Allocator, msg: string) !string {
return try jwt.encodeMessage(alloc, .HS256, msg, .{ .key = jwt_secret });
}
};
Expand Down Expand Up @@ -113,7 +113,7 @@ pub fn cleanMaps() !void {
}
}

pub fn mergeSlices(alloc: *std.mem.Allocator, comptime T: type, side_a: []const T, side_b: []const T) ![]const T {
pub fn mergeSlices(alloc: std.mem.Allocator, comptime T: type, side_a: []const T, side_b: []const T) ![]const T {
var list = std.ArrayList(T).init(alloc);
defer list.deinit();
try list.ensureTotalCapacity(side_a.len + side_b.len);
Expand All @@ -123,14 +123,14 @@ pub fn mergeSlices(alloc: *std.mem.Allocator, comptime T: type, side_a: []const
}

/// workaround for https://github.com/ziglang/zig/issues/10317
pub fn dirSize(alloc: *std.mem.Allocator, path: string) !usize {
pub fn dirSize(alloc: std.mem.Allocator, path: string) !usize {
var dir = try std.fs.cwd().openDir(path, .{ .iterate = true });
defer dir.close();
return try extras.dirSize(alloc, dir);
}

/// workaround for https://github.com/ziglang/zig/issues/10317
pub fn fileList(alloc: *std.mem.Allocator, path: string) ![]const string {
pub fn fileList(alloc: std.mem.Allocator, path: string) ![]const string {
var dir = try std.fs.cwd().openDir(path, .{ .iterate = true });
defer dir.close();
return try extras.fileList(alloc, dir);
Expand Down
Loading

0 comments on commit 8f0ddb3

Please sign in to comment.