Source file field.ml

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
(*****************************************************************************)
(*                                                                           *)
(*  Copyright (C) 2026 Yves Ndiaye                                           *)
(*                                                                           *)
(* This Source Code Form is subject to the terms of the Mozilla Public       *)
(* License, v. 2.0. If a copy of the MPL was not distributed with this       *)
(* file, You can obtain one at https://mozilla.org/MPL/2.0/.                 *)
(*                                                                           *)
(*****************************************************************************)

let sformat regex insensible key value =
  let key = match insensible with true -> key | false -> "%" ^ key in
  let colon = match regex with true -> "::" | false -> ":" in
  Printf.sprintf "%s%s%s" key colon value

let format = Printf.sprintf "%s:%s"

module String = struct
  module Key = struct
    type t =
      | Title
      | SortName
      | Artist
      | Album
      | AlbumArtist
      | Composer
      | Genre
      | Path
      | FileExtension
      | Arbitrary of string

    let equal : t -> t -> bool = ( = )
    let compare = compare

    let to_string = function
      | Title ->
          Keys.title
      | SortName ->
          Keys.sort_name
      | Artist ->
          Keys.artist
      | Album ->
          Keys.album
      | AlbumArtist ->
          Keys.album_artist
      | Composer ->
          Keys.composer
      | Genre ->
          Keys.genre
      | Path ->
          Keys.path
      | FileExtension ->
          Keys.extension
      | Arbitrary key ->
          let key =
            Printf.sprintf "%s%s" Util.arbitrary_prefix (Util.quote key)
          in
          key

    let default = [ Title; SortName; Artist; Album; AlbumArtist ]

    (* [assocs] is each substring associated with their key. *)
    let assocs =
      [
        (Keys.title, Title);
        (Keys.sort_name, SortName);
        (Keys.artist, Artist);
        (Keys.album, Album);
        (Keys.album_artist, AlbumArtist);
        (Keys.composer, Composer);
        (Keys.genre, Genre);
      ]

    let of_string t = List.assoc_opt t assocs
  end

  type 'a t = { regex : bool; insensible : bool; field : Key.t; value : 'a }

  let init regex insensible field value = { regex; insensible; field; value }
  let title regex insensible value = { regex; insensible; field = Title; value }

  let sort_name regex insensible value =
    { regex; insensible; field = SortName; value }

  let artist regex insensible value =
    { regex; insensible; field = Artist; value }

  let album regex insensible value = { regex; insensible; field = Album; value }

  let album_artist regex insensible value =
    { regex; insensible; field = AlbumArtist; value }

  let composer regex insensible value =
    { regex; insensible; field = Composer; value }

  let genre regex insensible value = { regex; insensible; field = Genre; value }
  let path regex insensible value = { regex; insensible; field = Path; value }

  let extension regex insensible value =
    { regex; insensible; field = FileExtension; value }

  let arbitrary regex insensible key value =
    { regex; insensible; field = Arbitrary key; value }

  let to_string s =
    let { regex; insensible; field; value } = s in
    let key = Key.to_string field in
    sformat regex insensible key value

  let to_sexp s =
    let { regex; insensible; field; value } = s in
    let key = Key.to_string field in
    sformat regex insensible key (Util.quote value)

  let regex s = s.regex
  let insensible s = s.insensible
  let key s = s.field
  let value s = s.value
end

module Bool = struct
  module Key = struct
    type t = Art | Flag

    let equal = ( = )
    let compare = Stdlib.compare
    let to_string = function Art -> Keys.art | Flag -> Keys.flag
  end

  type 'a t = { field : Key.t; value : 'a }

  let init field value = { field; value }
  let art b = { field = Art; value = b }
  let flag b = { field = Flag; value = b }

  let to_string s =
    let { field; value } = s in
    let key = Key.to_string field in
    format key (Bool.to_string value)

  let to_sexp = to_string
  let key s = s.field
  let value s = s.value
end

module Int64 = struct
  module Key = struct
    type t = Id | Size

    let equal = ( = )
    let compare = compare
    let to_string = function Id -> Keys.id | Size -> Keys.size
  end

  type 'a t = { field : Key.t; value : 'a Range.t }

  let init field value = { field; value }
  let id value = { field = Id; value }
  let size value = { field = Size; value }

  let to_string t =
    let { field; value } = t in
    let key = Key.to_string field in
    let s = Range.to_string Int64.to_string value in
    format key s

  let to_sexp = to_string
  let key s = s.field
  let value s = s.value
end

module Int = struct
  module Key = struct
    type t =
      | Track
      | Year
      | Disc
      | Plays
      | Length (* in secondes *)
      | Rating
      | AlbumCount

    let equal = ( = )
    let compare = compare

    let to_string = function
      | Track ->
          Keys.track
      | Year ->
          Keys.year
      | Disc ->
          Keys.disc
      | Plays ->
          Keys.plays
      | Length ->
          Keys.length
      | Rating ->
          Keys.rating
      | AlbumCount ->
          Keys.album_count
  end

  type 'a t = { field : Key.t; value : 'a Range.t }

  let init field value = { field; value }
  let track value = { field = Track; value }
  let year value = { field = Year; value }
  let disc value = { field = Disc; value }
  let plays value = { field = Plays; value }
  let length value = { field = Length; value }
  let rating value = { field = Rating; value }
  let album_count value = { field = AlbumCount; value }

  let to_string t =
    let { field; value } = t in
    let key = Key.to_string field in
    let s = Range.to_string Int.to_string value in
    format key s

  let to_sexp = to_string
  let key s = s.field
  let value s = s.value
end

module Float = struct
  module Key = struct
    type t = AvgDuration | AvgRating | AvgSize

    let equal = ( = )
    let compare = compare

    let to_string = function
      | AvgDuration ->
          Keys.avg_duration
      | AvgRating ->
          Keys.avg_rating
      | AvgSize ->
          Keys.avg_size
  end

  type 'a t = { field : Key.t; value : 'a Range.t }

  let init field value = { field; value }
  let avg_duration value = { field = AvgDuration; value }
  let avg_rating value = { field = AvgRating; value }
  let avg_size value = { field = AvgSize; value }

  let to_string f =
    let { field; value } = f in
    let key = Key.to_string field in
    let s = Range.to_string Float.to_string value in
    format key s

  let to_sexp = to_string
  let key s = s.field
  let value s = s.value
end

type ('s, 'b, 'i, 'i64, 'f) t =
  | FString of 's String.t
  | FBool of 'b Bool.t
  | FInt of 'i Int.t
  | FInt64 of 'i64 Int64.t
  | FFloat of 'f Float.t

let string s = FString s
let bool b = FBool b
let int i = FInt i
let int64 i = FInt64 i
let float f = FFloat f

(* String *)
let title regex insensible s = string (String.title regex insensible s)
let sort_name regex insensible s = string (String.sort_name regex insensible s)
let artist regex insensible s = string (String.artist regex insensible s)
let album regex insensible s = string (String.album regex insensible s)

let album_artist regex insensible s =
  string (String.album_artist regex insensible s)

let composer regex insensible s = string (String.composer regex insensible s)
let genre regex insensible s = string (String.genre regex insensible s)
let path regex insensible s = string (String.path regex insensible s)
let extension regex insensible s = string (String.extension regex insensible s)

let arbitrary regex insensible key value =
  string (String.arbitrary regex insensible key value)

(* Bool *)
let art r = bool (Bool.art r)
let flag r = bool (Bool.flag r)

(* Int64 *)
let id i = int64 (Int64.id i)
let size r = int64 (Int64.size r)

(* Int *)
let track r = int (Int.track r)
let year r = int (Int.year r)
let disc r = int (Int.disc r)
let length r = int (Int.length r)
let plays r = int (Int.plays r)
let rating r = int (Int.rating r)
let album_count r = int (Int.album_count r)

(* Float *)
let avg_duration r = float (Float.avg_duration r)
let avg_rating r = float (Float.avg_rating r)
let avg_size r = float (Float.avg_size r)

let fold s b i i64 f = function
  | FString e ->
      s e
  | FBool e ->
      b e
  | FInt64 e ->
      i64 e
  | FInt e ->
      i e
  | FFloat e ->
      f e

let to_string = function
  | FString s ->
      String.to_string s
  | FBool b ->
      Bool.to_string b
  | FInt i ->
      Int.to_string i
  | FInt64 i ->
      Int64.to_string i
  | FFloat f ->
      Float.to_string f

let to_sexp = function
  | FString s ->
      String.to_sexp s
  | FBool b ->
      Bool.to_sexp b
  | FInt64 i ->
      Int64.to_sexp i
  | FInt i ->
      Int.to_sexp i
  | FFloat f ->
      Float.to_sexp f

let is_string = function
  | FString _ ->
      true
  | FBool _ | FInt _ | FFloat _ | FInt64 _ ->
      false