Combine Alias.zig with Entities.zig

This commit is contained in:
mitteneer 2025-03-25 09:32:13 -04:00
parent 8028e5e42b
commit c13c2b2817
4 changed files with 96 additions and 110 deletions

View file

@ -1,14 +0,0 @@
pub const Alias = struct {
@"sort-name": []const u8,
//sort_name: []const u8,
@"type-id": ?[]const u8 = null,
//type_id: []const u8,
name: []const u8,
locale: ?[]const u8 = null,
type: ?[]const u8 = null,
primary: ?bool = null,
@"begin-date": ?[]const u8 = null,
//begin_date: []const u8,
@"end-date": ?[]const u8 = null,
//end_date: []const u8,
};

View file

@ -1,10 +1,20 @@
//! Types of MusicBrainz Entities described here: https://musicbrainz.org/doc/MusicBrainz_Entity //! Types of MusicBrainz Entities described here: https://musicbrainz.org/doc/MusicBrainz_Entity
//! Fields based on JSON response fields //! Fields based on JSON response fields
const Alias = @import("Alias.zig").Alias;
//const zeit = @import("zeit"); //const zeit = @import("zeit");
const std = @import("std"); const std = @import("std");
pub const Alias = struct {
@"sort-name": []const u8,
@"type-id": ?[]const u8 = null,
name: []const u8,
locale: ?[]const u8 = null,
type: ?[]const u8 = null,
primary: ?bool = null,
@"begin-date": ?[]const u8 = null,
@"end-date": ?[]const u8 = null,
};
pub const Artist = struct { pub const Artist = struct {
id: []const u8, id: []const u8,
name: []const u8, name: []const u8,

View file

@ -1,7 +1,5 @@
//! Types of MusicBrainz Entities described here: https://musicbrainz.org/doc/MusicBrainz_Entity //! Types of MusicBrainz Entities described here: https://musicbrainz.org/doc/MusicBrainz_Entity
//! Fields based on JSON response fields //! Fields based on JSON response fields
const Alias = @import("Alias.zig").Alias;
const std = @import("std"); const std = @import("std");
pub const Artist = struct { pub const Artist = struct {
@ -46,6 +44,7 @@ pub const Recording = struct {
} else return false; // a provides no information } else return false; // a provides no information
} }
}; };
pub const Release = struct { pub const Release = struct {
id: []const u8, id: []const u8,
title: []const u8, title: []const u8,

View file

@ -25,7 +25,7 @@ pub const Result = struct {
// album "1"), it will instead give you the information from the album the song originally appeared on // album "1"), it will instead give you the information from the album the song originally appeared on
// (in this case, the album "Help!"). // (in this case, the album "Help!").
pub fn getOriginalIDs(self: *const Result, smd: searchMetadata, pbo: parseOption) ?[]const u8 { pub fn getID(self: *const Result, smd: searchMetadata, pbo: parseOption) ?[]const u8 {
if (self.count == 0) return null; if (self.count == 0) return null;
switch (pbo) { switch (pbo) {
.song => |method| { .song => |method| {
@ -86,107 +86,98 @@ pub const Result = struct {
} }
return self.recordings[0].@"artist-credit"[0].artist.id; return self.recordings[0].@"artist-credit"[0].artist.id;
}, },
.all => unreachable,
} }
} }
pub fn getIDs(self: *const Result, smd: searchMetadata, pbo: parseByOptions) ?searchIDs { //pub fn getIDs(self: *const Result, smd: searchMetadata, pbo: parseByOptions) ?searchIDs {
if (self.count == 0) return null; // if (self.count == 0) return null;
const sorted_recordings = self.recordings; // const sorted_recordings = self.recordings;
std.mem.sort(Recording, sorted_recordings, {}, Recording.lessThan); // std.mem.sort(Recording, sorted_recordings, {}, Recording.lessThan);
for (sorted_recordings) |rc| { // for (sorted_recordings) |rc| {
switch (pbo.parse) { // switch (pbo.parse) {
.song => { // .song => {
const output = searchIDs{ // const output = searchIDs{
.id = switch (pbo.specifyBy) { // .id = switch (pbo.specifyBy) {
.song => song_song: { // .song => song_song: {
if (std.mem.eql(u8, rc.title, smd.tn)) { // if (std.mem.eql(u8, rc.title, smd.tn)) {
break :song_song rc.id; // break :song_song rc.id;
//break :rc_loop; // //break :rc_loop;
} else break :song_song ""; // } else break :song_song "";
}, // },
.album => song_album: { // .album => song_album: {
for (rc.releases) |rl| { // for (rc.releases) |rl| {
if (std.mem.eql(u8, rl.title, smd.rn)) { // if (std.mem.eql(u8, rl.title, smd.rn)) {
break :song_album rc.id; // break :song_album rc.id;
//break :rc_loop; // //break :rc_loop;
} // }
} // }
break :song_album ""; // break :song_album "";
}, // },
.artist => song_artist: { // .artist => song_artist: {
for (rc.@"artist-credit") |ac| { // for (rc.@"artist-credit") |ac| {
if (std.mem.eql(u8, ac.name, smd.an)) { // if (std.mem.eql(u8, ac.name, smd.an)) {
break :song_artist rc.id; // break :song_artist rc.id;
//break :rc_loop; // //break :rc_loop;
} // }
} // }
break :song_artist ""; // break :song_artist "";
}, // },
.all => unreachable, // I'll do this later // .all => unreachable, // I'll do this later
}, // },
}; // };
return output; // return output;
}, // },
.album => { // .album => {
const output = searchIDs{ .id = blk: { // const output = searchIDs{ .id = blk: {
const sorted_releases = rc.releases; // const sorted_releases = rc.releases;
std.mem.sort(Release, sorted_releases, {}, Release.lessThan); // std.mem.sort(Release, sorted_releases, {}, Release.lessThan);
for (sorted_releases) |rl| { // for (sorted_releases) |rl| {
switch (pbo.specifyBy) { // switch (pbo.specifyBy) {
.song, .album => { // .song, .album => {
if (std.mem.eql(u8, rl.title, smd.rn)) break :blk rl.id; // if (std.mem.eql(u8, rl.title, smd.rn)) break :blk rl.id;
}, // },
.artist => { // .artist => {
for (rl.@"artist-credit") |ac| { // for (rl.@"artist-credit") |ac| {
if (std.mem.eql(u8, ac.name, smd.an)) break :blk rl.id; // if (std.mem.eql(u8, ac.name, smd.an)) break :blk rl.id;
} // }
}, // },
.all => unreachable, // .all => unreachable,
} // }
} // }
break :blk ""; // break :blk "";
} }; // } };
return output; // return output;
}, // },
.artist => { // .artist => {
const output = searchIDs{ .id = switch (pbo.specifyBy) { // const output = searchIDs{ .id = switch (pbo.specifyBy) {
.song, .artist => blk: { // .song, .artist => blk: {
for (rc.@"artist-credit") |ac| { // for (rc.@"artist-credit") |ac| {
if (std.mem.eql(u8, ac.name, smd.an)) break :blk ac.artist.id; // if (std.mem.eql(u8, ac.name, smd.an)) break :blk ac.artist.id;
} // }
break :blk ""; // break :blk "";
}, // },
.album => blk: { // .album => blk: {
for (rc.releases) |rl| { // for (rc.releases) |rl| {
for (rl.@"artist-credit") |ac| { // for (rl.@"artist-credit") |ac| {
if (std.mem.eql(u8, ac.name, smd.an)) break :blk ac.artist.id; // if (std.mem.eql(u8, ac.name, smd.an)) break :blk ac.artist.id;
} // }
} // }
break :blk ""; // break :blk "";
}, // },
.all => unreachable, // .all => unreachable,
} }; // } };
return output; // return output;
}, // },
.all => { // }
var output = searchIDs{ .ids = undefined }; // }
if (self.getIDs(smd, .{ .parse = .song })) |ids| output.ids.song_id = ids.id; // return null;
if (self.getIDs(smd, .{ .parse = .album })) |ids| output.ids.album_id = ids.id; //}
if (self.getIDs(smd, .{ .parse = .artist })) |ids| output.ids.artist_id = ids.id;
return output;
},
}
}
return null;
}
}; };
pub const parseOption = union(enum) { pub const parseOption = union(enum) {
song: enum { canon, orig }, song: enum { canon, orig },
album: enum { canon, orig }, album: enum { canon, orig },
artist, artist,
all,
}; };
pub const parseByOptions = struct { parse: parseOption = .song, specifyBy: parseOption = .song }; pub const parseByOptions = struct { parse: parseOption = .song, specifyBy: parseOption = .song };