muzigbrainz/src/Entities.zig

153 lines
5.9 KiB
Zig

//! Types of MusicBrainz Entities described here: https://musicbrainz.org/doc/MusicBrainz_Entity
//! Fields based on JSON response fields
const Alias = @import("Alias.zig").Alias;
//const zeit = @import("zeit");
const std = @import("std");
pub const Artist = struct {
id: []const u8,
name: []const u8,
@"sort-name": []const u8,
//sort_name: []const u8,
aliases: ?[]Alias = null,
disambiguation: ?[]const u8 = null,
};
pub const ArtistCredit = struct {
joinphrase: ?[]const u8 = null,
name: []const u8,
artist: Artist,
};
pub const Recording = struct {
id: []const u8,
score: u8,
title: []const u8,
length: ?u64 = null,
video: ?bool = null,
@"artist-credit": ?[]ArtistCredit = null,
//artist_credit: []ArtistCredit,
@"first-release-date": ?[]const u8 = null,
//dt: ?i64 = null,
//@"first-release-date": ?union(enum) { date: []const u8, dt: zeit.Date } = null,
//first_release_date: []const u8,
releases: ?[]Release = null,
isrcs: ?[][]const u8 = null,
tags: ?[]Tag = null,
disambiguation: ?[]const u8 = null,
pub fn lessThan(context: void, a: Recording, b: Recording) bool {
_ = context; // Idk what this is but it's in the Zig docs
if (a.@"first-release-date") |adtstr| {
if (b.@"first-release-date") |bdtstr| {
const adtyr = std.fmt.parseInt(u32, adtstr[0..4], 10) catch 0; // Each `catch 0` is to avoid returning an error
const bdtyr = std.fmt.parseInt(u32, bdtstr[0..4], 10) catch 0;
if (adtyr != bdtyr) {
return adtyr < bdtyr;
} else if (adtstr.len >= 7) {
if (bdtstr.len < 7) return true; // a provides more information
const adtmn = std.fmt.parseInt(u32, adtstr[5..7], 10) catch 0;
const bdtmn = std.fmt.parseInt(u32, bdtstr[5..7], 10) catch 0;
if (adtmn != bdtmn) {
return adtmn < bdtmn;
} else if (adtstr.len == 10) {
if (bdtstr.len < 10) return true; // a provides more information
const adtdy = std.fmt.parseInt(u32, adtstr[8..10], 10) catch 0;
const bdtdy = std.fmt.parseInt(u32, bdtstr[8..10], 10) catch 0;
return adtdy < bdtdy;
} else return false; // Either b provides more information, or they're the same date
} else return false; // Either b provides more information, or they're the same date
} else return true; // b provides no information
} else return false; // a provides no information
}
};
pub const Release = struct {
id: []const u8,
@"status-id": ?[]const u8 = null,
//status_id: ?[]const u8 = null,
count: u8,
title: []const u8,
@"artist-credit": ?[]ArtistCredit = null,
//artist_credit: []ArtistCredit,
@"release-group": ?ReleaseGroup = null,
//release_group: ReleaseGroup,
@"track-count": u16,
//track_count: u8,
media: ?[]Medium = null,
status: ?[]const u8 = null,
date: ?[]const u8 = null,
country: ?[]const u8 = null,
@"release-events": ?[]ReleaseEvent = null,
//release_events: ?[]ReleaseEvent = null,
disambiguation: ?[]const u8 = null,
pub fn lessThan(context: void, a: Release, b: Release) bool {
_ = context; // Idk what this is but it's in the Zig docs
if (a.date) |adtstr| {
if (b.date) |bdtstr| {
const adtyr = std.fmt.parseInt(u32, adtstr[0..4], 10) catch 0; // Each `catch 0` is to avoid returning an error
const bdtyr = std.fmt.parseInt(u32, bdtstr[0..4], 10) catch 0;
if (adtyr != bdtyr) {
return adtyr < bdtyr;
} else if (adtstr.len >= 7) {
if (bdtstr.len < 7) return true; // a provides more information
const adtmn = std.fmt.parseInt(u32, adtstr[5..7], 10) catch 0;
const bdtmn = std.fmt.parseInt(u32, bdtstr[5..7], 10) catch 0;
if (adtmn != bdtmn) {
return adtmn < bdtmn;
} else if (adtstr.len == 10) {
if (bdtstr.len < 10) return true; // a provides more information
const adtdy = std.fmt.parseInt(u32, adtstr[8..10], 10) catch 0;
const bdtdy = std.fmt.parseInt(u32, bdtstr[8..10], 10) catch 0;
return adtdy < bdtdy;
} else return false; // Either b provides more information, or they're the same date
} else return false; // Either b provides more information, or they're the same date
} else return true; // b provides no information
} else return false; // a provides no information
}
};
pub const Medium = struct {
position: ?u32 = null,
format: ?[]const u8 = null,
track: ?[]Track = null,
@"track-count": u32,
//track_count: u8,
@"track-offset": ?u32 = null,
//track_offset: u8,
};
pub const Track = struct {
id: []const u8,
number: []const u8,
title: []const u8,
length: ?u64 = null,
};
pub const Area = struct {
id: []const u8,
name: []const u8,
@"sort-name": []const u8,
//sort_name: []const u8,
@"iso-3166-1-codes": ?[][]const u8 = null,
//iso_3166_1_codes: [][]const u8,
};
pub const ReleaseEvent = struct {
date: []const u8,
area: Area,
};
pub const ReleaseGroup = struct {
id: []const u8,
@"type-id": ?[]const u8 = null,
//type_id: []const u8,
@"primary-type-id": ?[]const u8 = null,
//primary_type_id: []const u8,
title: []const u8,
@"primary-type": ?[]const u8 = null,
//primary_type: []const u8,
@"secondary-types": ?[][]const u8 = null,
@"secondary-type-ids": ?[][]const u8 = null,
};
const Tag = struct {
count: u8,
name: []const u8,
};