package shakuhachi

  1. Overview
  2. Docs
Legend:
Page
Library
Module
Module type
Parameter
Class
Class type
Source

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
(**********************************************************************************************)
(*                                                                                            *)
(* This file is part of shakuhachi: a music collection manager                                *)
(* Copyright (C) 2025 Yves Ndiaye                                                             *)
(*                                                                                            *)
(* shakuhachi is free software: you can redistribute it and/or modify it under the terms      *)
(* of the GNU General Public License as published by the Free Software Foundation,            *)
(* either version 3 of the License, or (at your option) any later version.                    *)
(*                                                                                            *)
(* shakuhachi is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;    *)
(* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR           *)
(* PURPOSE.  See the GNU General Public License for more details.                             *)
(* You should have received a copy of the GNU General Public License along with shakuhachi.   *)
(* If not, see <http://www.gnu.org/licenses/>.                                                *)
(*                                                                                            *)
(**********************************************************************************************)

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
  type field =
    | Title of string
    | SortName of string
    | Artist of string
    | Album of string
    | AlbumArtist of string
    | Composer of string
    | Genre of string
    | Path of string
    | FileExtension of string

  type t = { regex : bool; insensible : bool; field : field }

  let title regex insensible s = { regex; insensible; field = Title s }
  let sort_name regex insensible s = { regex; insensible; field = SortName s }
  let artist regex insensible s = { regex; insensible; field = Artist s }
  let album regex insensible s = { regex; insensible; field = Album s }

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

  let composer regex insensible s = { regex; insensible; field = Composer s }
  let genre regex insensible s = { regex; insensible; field = Genre s }
  let path regex insensible s = { regex; insensible; field = Path s }

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

  let to_string s =
    let { regex; insensible; field } = s in
    let key, value =
      match field with
      | Title s ->
          (Keys.title, s)
      | SortName s ->
          (Keys.sort_name, s)
      | Artist s ->
          (Keys.artist, s)
      | Album s ->
          (Keys.album, s)
      | AlbumArtist s ->
          (Keys.album_artist, s)
      | Composer s ->
          (Keys.composer, s)
      | Genre s ->
          (Keys.genre, s)
      | Path s ->
          (Keys.path, s)
      | FileExtension s ->
          (Keys.extension, s)
    in
    sformat regex insensible key value

  let to_sexp s =
    let { regex; insensible; field } = s in
    let key, value =
      match field with
      | Title s ->
          (Keys.title, s)
      | SortName s ->
          (Keys.sort_name, s)
      | Artist s ->
          (Keys.artist, s)
      | Album s ->
          (Keys.album, s)
      | AlbumArtist s ->
          (Keys.album_artist, s)
      | Composer s ->
          (Keys.composer, s)
      | Genre s ->
          (Keys.genre, s)
      | Path s ->
          (Keys.path, s)
      | FileExtension s ->
          (Keys.extension, s)
    in
    sformat regex insensible key (Util.quote value)

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

module Bool = struct
  type t = Art of bool | Flag of bool

  let art b = Art b
  let flag b = Flag b

  let to_string s =
    let key, value =
      match s with Art b -> (Keys.art, b) | Flag b -> (Keys.flag, b)
    in
    format key (Bool.to_string value)

  let to_sexp = to_string
end

module Int = struct
  type t =
    | Id of int64 Range.t
    | Size of int64 Range.t
    | Track of int Range.t
    | Year of int Range.t
    | Disc of int Range.t
    | Plays of int Range.t
    | Length of int Range.t (* in secondes *)
    | Rating of int Range.t
    | AlbumCount of int Range.t

  let id r = Id r
  let size r = Size r
  let track r = Track r
  let year r = Year r
  let disc r = Disc r
  let plays r = Plays r
  let length r = Length r
  let rating r = Rating r
  let album_count r = AlbumCount r

  let to_string = function
    | Id r ->
        format Keys.id (Range.to_string Int64.to_string r)
    | Size r ->
        format Keys.size (Range.to_string Int64.to_string r)
    | Track r ->
        format Keys.track (Range.to_string Int.to_string r)
    | Year r ->
        format Keys.year (Range.to_string Int.to_string r)
    | Disc r ->
        format Keys.disc (Range.to_string Int.to_string r)
    | Plays r ->
        format Keys.plays (Range.to_string Int.to_string r)
    | Length r ->
        format Keys.length (Range.to_string Int.to_string r)
    | Rating r ->
        format Keys.rating (Range.to_string Int.to_string r)
    | AlbumCount r ->
        format Keys.album_count (Range.to_string Int.to_string r)

  let to_sexp = to_string
end

module Float = struct
  type t =
    | AvgDuration of float Range.t
    | AvgRating of float Range.t
    | AvgSize of float Range.t

  let avg_duration r = AvgDuration r
  let avg_rating r = AvgRating r
  let avg_size r = AvgSize r

  let to_string f =
    let variable, range =
      match f with
      | AvgDuration f ->
          (Keys.avg_duration, f)
      | AvgRating f ->
          (Keys.avg_rating, f)
      | AvgSize f ->
          (Keys.avg_size, f)
    in
    format variable (Range.to_string Float.to_string range)

  let to_sexp = to_string
end

type t =
  | FString of String.t
  | FBool of Bool.t
  | FInt of Int.t
  | FFloat of Float.t

let string s = FString s
let bool b = FBool b
let int i = FInt 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)

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

(* Int *)
let id i = int (Int.id i)
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 size r = int (Int.size 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 to_string = function
  | FString s ->
      String.to_string s
  | FBool b ->
      Bool.to_string b
  | FInt i ->
      Int.to_string i
  | FFloat f ->
      Float.to_string f

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

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