Skip to content

Commit

Permalink
fix: #7
Browse files Browse the repository at this point in the history
  • Loading branch information
kassane committed Mar 28, 2024
1 parent a4eaae1 commit e571483
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions imports/version.zig
Original file line number Diff line number Diff line change
@@ -1,38 +1,35 @@
const std = @import("std");

pub const Version = struct {
major: ?u32 = null,
minor: ?u32 = null,
patch: ?u32 = null,
pub fn get() Version {
var final_version: Version = .{
.major = 0,
.minor = 0,
.patch = 0,
};
var final_version: Version = .{};
const idf_version = std.mem.span(@import("sys").esp_get_idf_version());

if (!std.mem.startsWith(u8, idf_version, "v"))
return final_version;

// skip [0] == 'v' and remove [idf_version.len - 4] == "-dev"
var it = if (std.mem.endsWith(u8, idf_version, "-dev"))
std.mem.split(u8, idf_version[1 .. idf_version.len - 4], ".")
else
std.mem.split(u8, idf_version[1..], ".");

final_version.major = std.fmt.parseUnsigned(u32, it.first(), 10) catch |err|
@panic(@errorName(err));
var strip = std.mem.split(u8, idf_version, "-");
var it = std.mem.tokenize(u8, strip.first(), ".");

while (it.next()) |token| {
if (!std.ascii.isDigit(token[0]))
continue;
const digit = std.fmt.parseUnsigned(u32, token, 10) catch |err|
@panic(@errorName(err));
if (digit != final_version.minor and final_version.minor == 0) {
// skip [0] == 'v'
const digit = if (std.mem.startsWith(u8, token, "v"))
std.fmt.parseUnsigned(u32, token[1..], 10) catch |err|
@panic(@errorName(err))
else
std.fmt.parseUnsigned(u32, token, 10) catch |err|
@panic(@errorName(err));

if (final_version.major == null) {
final_version.major = digit;
} else if (final_version.minor == null) {
final_version.minor = digit;
continue;
}
if (digit != final_version.patch)
} else if (final_version.patch == null) {
final_version.patch = digit;
}
}

return final_version;
Expand All @@ -44,12 +41,16 @@ pub const Version = struct {
if (!std.mem.startsWith(u8, idf_version, "v"))
return idf_version
else
return std.fmt.allocPrint(allocator, "{d}.{d}.{d}", .{
return std.fmt.allocPrint(allocator, "v{d}.{d}.{d}", .{
self.major.?,
self.minor.?,
self.patch.?,
}) catch |err|
@panic(@errorName(err));
}
const std = @import("std");
};

test "Valid version" {
const testing = std.testing;
try testing.expectEqual(Version.get().toString(testing.allocator), "v5.1.3");
}

0 comments on commit e571483

Please sign in to comment.