chore: reorganize some code

This commit is contained in:
Rekai Nyangadzayi Musuka 2022-10-21 05:12:56 -03:00
parent 1a4a2a56a3
commit 1a56f957c1
2 changed files with 24 additions and 24 deletions

View File

@ -13,12 +13,12 @@ alloc: Allocator,
addr_latch: u32, addr_latch: u32,
pub fn init(alloc: Allocator, maybe_path: ?[]const u8) !Self { pub fn init(alloc: Allocator, maybe_path: ?[]const u8) !Self {
var buf: ?[]u8 = null; const buf: ?[]u8 = if (maybe_path) |path| blk: {
if (maybe_path) |path| {
const file = try std.fs.cwd().openFile(path, .{}); const file = try std.fs.cwd().openFile(path, .{});
defer file.close(); defer file.close();
buf = try file.readToEndAlloc(alloc, try file.getEndPos());
} break :blk try file.readToEndAlloc(alloc, try file.getEndPos());
} else null;
return Self{ return Self{
.buf = buf, .buf = buf,

View File

@ -18,7 +18,7 @@ pub const Backup = struct {
buf: []u8, buf: []u8,
alloc: Allocator, alloc: Allocator,
kind: BackupKind, kind: Kind,
title: [12]u8, title: [12]u8,
save_path: ?[]const u8, save_path: ?[]const u8,
@ -26,7 +26,15 @@ pub const Backup = struct {
flash: Flash, flash: Flash,
eeprom: Eeprom, eeprom: Eeprom,
pub fn init(allocator: Allocator, kind: BackupKind, title: [12]u8, path: ?[]const u8) !Self { const Kind = enum {
Eeprom,
Sram,
Flash,
Flash1M,
None,
};
pub fn init(allocator: Allocator, kind: Kind, title: [12]u8, path: ?[]const u8) !Self {
log.info("Kind: {}", .{kind}); log.info("Kind: {}", .{kind});
const buf_size: usize = switch (kind) { const buf_size: usize = switch (kind) {
@ -53,7 +61,7 @@ pub const Backup = struct {
return backup; return backup;
} }
pub fn guessKind(rom: []const u8) ?BackupKind { pub fn guessKind(rom: []const u8) ?Kind {
for (backup_kinds) |needle| { for (backup_kinds) |needle| {
const needle_len = needle.str.len; const needle_len = needle.str.len;
@ -201,21 +209,13 @@ pub const Backup = struct {
} }
}; };
const BackupKind = enum {
Eeprom,
Sram,
Flash,
Flash1M,
None,
};
const Needle = struct { const Needle = struct {
const Self = @This(); const Self = @This();
str: []const u8, str: []const u8,
kind: BackupKind, kind: Backup.Kind,
fn init(str: []const u8, kind: BackupKind) Self { fn init(str: []const u8, kind: Backup.Kind) Self {
return .{ return .{
.str = str, .str = str,
.kind = kind, .kind = kind,
@ -230,7 +230,7 @@ const SaveError = error{
const Flash = struct { const Flash = struct {
const Self = @This(); const Self = @This();
state: FlashState, state: State,
id_mode: bool, id_mode: bool,
set_bank: bool, set_bank: bool,
@ -239,6 +239,12 @@ const Flash = struct {
bank: u1, bank: u1,
const State = enum {
Ready,
Set,
Command,
};
fn init() Self { fn init() Self {
return .{ return .{
.state = .Ready, .state = .Ready,
@ -293,12 +299,6 @@ const Flash = struct {
} }
}; };
const FlashState = enum {
Ready,
Set,
Command,
};
const Eeprom = struct { const Eeprom = struct {
const Self = @This(); const Self = @This();