From 226121ac16c43ad616c847e1cd29c3da61310648 Mon Sep 17 00:00:00 2001 From: Dan Cojocaru Date: Sat, 17 Feb 2024 03:31:44 +0100 Subject: [PATCH] Optimise imports --- src/departure.zig | 4 ++-- src/home.zig | 26 +++++++++++++------------- src/main.zig | 4 ++-- src/state.zig | 14 ++++++-------- 4 files changed, 23 insertions(+), 25 deletions(-) diff --git a/src/departure.zig b/src/departure.zig index 4e6fded..be30b93 100644 --- a/src/departure.zig +++ b/src/departure.zig @@ -1,8 +1,8 @@ const raylib = @import("raylib.zig"); const rl = raylib.rl; -const stateMod = @import("state.zig"); +const AppState = @import("state.zig"); -pub fn render(state: *stateMod.AppState) !void { +pub fn render(state: *AppState) !void { while (raylib.GetKeyPressed()) |key| { switch (key) { rl.KEY_LEFT => { diff --git a/src/home.zig b/src/home.zig index 7a02f97..f945c80 100644 --- a/src/home.zig +++ b/src/home.zig @@ -1,8 +1,8 @@ const std = @import("std"); const raylib = @import("raylib.zig"); const rl = raylib.rl; -const state_mod = @import("state.zig"); -const curl_mod = @import("curl.zig"); +const AppState = @import("state.zig"); +const Curl = @import("curl.zig"); fn curlWriteHandler(ptr: [*]u8, size: usize, nmemb: usize, userdata: *std.ArrayList(u8)) callconv(.C) usize { _ = size; @@ -10,13 +10,13 @@ fn curlWriteHandler(ptr: [*]u8, size: usize, nmemb: usize, userdata: *std.ArrayL return nmemb; } -fn fetchThread(state: *state_mod.AppState) !void { - std.debug.print("Started fetchThread\n", .{}); - defer std.debug.print("Ended fetchThread\n", .{}); +fn fetchThread(state: *AppState) !void { + std.debug.print("[home/fetchThread] Started\n", .{}); + defer std.debug.print("[home/fetchThread] Ended\n", .{}); defer state.home_screen_state.fetch_thread = null; const allocator = state.allocator; var station_name_buf = std.BoundedArray(u8, 200){}; - var curl = curl_mod.init() orelse return; + var curl = Curl.init() orelse return; defer curl.deinit(); const locations_base = "https://v6.db.transport.rest/locations"; var locations_uri = std.Uri.parse(locations_base) catch unreachable; @@ -30,7 +30,7 @@ fn fetchThread(state: *state_mod.AppState) !void { station_name_buf.resize(state.home_screen_state.station_name.items.len) catch continue; std.mem.copyForwards(u8, station_name_buf.slice(), state.home_screen_state.station_name.items); - std.debug.print("[fetchThread] Detected update: {s}\n", .{station_name_buf.slice()}); + std.debug.print("[home/fetchThread] Detected update: {s}\n", .{station_name_buf.slice()}); curl.reset(); @@ -38,7 +38,7 @@ fn fetchThread(state: *state_mod.AppState) !void { defer allocator.free(query); locations_uri.query = query; defer locations_uri.query = null; - std.debug.print("[fetchThread] Making request to: {}\n", .{locations_uri}); + std.debug.print("[home/fetchThread] Making request to: {}\n", .{locations_uri}); const url = try std.fmt.allocPrintZ(allocator, "{}", .{locations_uri}); defer allocator.free(url); @@ -50,17 +50,17 @@ fn fetchThread(state: *state_mod.AppState) !void { _ = curl.setopt(.write_data, .{&result}); const code = curl.perform(); - std.debug.print("[fetchThread] cURL Code: {}\n", .{code}); + std.debug.print("[home/fetchThread] cURL Code: {}\n", .{code}); if (code != 0) continue; - std.debug.print("[fetchThread] Fetched data: (len: {})\n", .{result.items.len}); + std.debug.print("[home/fetchThread] Fetched data: (len: {})\n", .{result.items.len}); const parsed = std.json.parseFromSlice([]const std.json.Value, allocator, result.items, .{}) catch |err| { - std.debug.print("[fetchThread] JSON parse error: {}\n", .{err}); + std.debug.print("[home/fetchThread] JSON parse error: {}\n", .{err}); continue; }; defer parsed.deinit(); - var results = std.ArrayList(state_mod.HSSuggestion).init(allocator); + var results = std.ArrayList(AppState.HSSuggestion).init(allocator); for (parsed.value) |station| { if (station.object.get("name")) |nameValue| { const name = nameValue.string; @@ -85,7 +85,7 @@ fn fetchThread(state: *state_mod.AppState) !void { } } -pub fn render(state: *state_mod.AppState) !void { +pub fn render(state: *AppState) !void { var hs = &state.home_screen_state; if (hs.fetch_thread == null) { diff --git a/src/main.zig b/src/main.zig index 200d7d6..5ecd118 100644 --- a/src/main.zig +++ b/src/main.zig @@ -1,9 +1,9 @@ const std = @import("std"); const raylib = @import("raylib.zig"); const rl = raylib.rl; -const stateMod = @import("state.zig"); const home = @import("home.zig"); const departure = @import("departure.zig"); +const AppState = @import("state.zig"); pub fn main() !void { var gpa = std.heap.GeneralPurposeAllocator(.{}){}; @@ -25,7 +25,7 @@ pub fn main() !void { var station_name_buffer: [100]u8 = .{0} ** 100; var platform_buffer: [20]u8 = .{0} ** 20; var station_id_buffer: [10]u8 = .{0} ** 10; - var appState = stateMod.AppState{ + var appState = AppState{ .allocator = allocator, // .db_font = font, .home_screen_state = .{ diff --git a/src/state.zig b/src/state.zig index fad94c9..8892e13 100644 --- a/src/state.zig +++ b/src/state.zig @@ -26,11 +26,9 @@ pub const DepartureScreenState = struct { loading: bool = false, }; -pub const AppState = struct { - allocator: std.mem.Allocator, - close_app: bool = false, - db_font: ?rl.Font = null, - screen: Screen = .home, - home_screen_state: HomeScreenState, - departure_screen_state: DepartureScreenState, -}; +allocator: std.mem.Allocator, +close_app: bool = false, +db_font: ?rl.Font = null, +screen: Screen = .home, +home_screen_state: HomeScreenState, +departure_screen_state: DepartureScreenState,