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,
pub fn init(alloc: Allocator, maybe_path: ?[]const u8) !Self {
var buf: ?[]u8 = null;
if (maybe_path) |path| {
const buf: ?[]u8 = if (maybe_path) |path| blk: {
const file = try std.fs.cwd().openFile(path, .{});
defer file.close();
buf = try file.readToEndAlloc(alloc, try file.getEndPos());
}
break :blk try file.readToEndAlloc(alloc, try file.getEndPos());
} else null;
return Self{
.buf = buf,

View File

@ -18,7 +18,7 @@ pub const Backup = struct {
buf: []u8,
alloc: Allocator,
kind: BackupKind,
kind: Kind,
title: [12]u8,
save_path: ?[]const u8,
@ -26,7 +26,15 @@ pub const Backup = struct {
flash: Flash,
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});
const buf_size: usize = switch (kind) {
@ -53,7 +61,7 @@ pub const Backup = struct {
return backup;
}
pub fn guessKind(rom: []const u8) ?BackupKind {
pub fn guessKind(rom: []const u8) ?Kind {
for (backup_kinds) |needle| {
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 Self = @This();
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 .{
.str = str,
.kind = kind,
@ -230,7 +230,7 @@ const SaveError = error{
const Flash = struct {
const Self = @This();
state: FlashState,
state: State,
id_mode: bool,
set_bank: bool,
@ -239,6 +239,12 @@ const Flash = struct {
bank: u1,
const State = enum {
Ready,
Set,
Command,
};
fn init() Self {
return .{
.state = .Ready,
@ -293,12 +299,6 @@ const Flash = struct {
}
};
const FlashState = enum {
Ready,
Set,
Command,
};
const Eeprom = struct {
const Self = @This();