package shakuhachi

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

Source file query.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
(**********************************************************************************************)
(*                                                                                            *)
(* 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/>.                                                *)
(*                                                                                            *)
(**********************************************************************************************)

type t = Node of Node.t | And of (t * t) | Or of (t * t) | Not of t

let node n = Node n
let ( land ) lhs rhs = And (lhs, rhs)
let ( lor ) lhs rhs = Or (lhs, rhs)
let lnot lhs = Not lhs
let equal : t -> t -> bool = Stdlib.( = )
let verum = node @@ Node.field @@ Field.id @@ Range.range None None
let always = verum
let falsum = lnot verum
let never = falsum
let union = ( lor )
let diff lhs rhs = lhs land lnot rhs

let rec to_string = function
  | Node node ->
      Node.to_string node
  | And (lhs, rhs) ->
      Printf.sprintf "(%s and %s)" (to_string lhs) (to_string rhs)
  | Or (lhs, rhs) ->
      Printf.sprintf "(%s || %s)" (to_string lhs) (to_string rhs)
  | Not e ->
      Printf.sprintf "not %s" (to_string e)

let rec to_sexp = function
  | Node node ->
      Node.to_sexp node
  | And (lhs, rhs) ->
      Printf.sprintf "(and %s %s)" (to_sexp lhs) (to_sexp rhs)
  | Or (lhs, rhs) ->
      Printf.sprintf "(or %s %s)" (to_sexp lhs) (to_sexp rhs)
  | Not e ->
      Printf.sprintf "(not %s)" (to_sexp e)

let comsume combine node s =
  match Angstrom.parse_string ~consume:All Parser.node_cli s with
  | Ok node' ->
      combine node (Node node')
  | Error _ ->
      node

let of_sexp s =
  let module P = struct
    open Angstrom

    let whitespace = Parser.whitespace
    let query = Parser.node_sexp
    let parenthesis parser = char '(' *> parser <* char ')'

    let sexp =
      Angstrom.fix (fun sexp ->
          let sand =
            string_ci "and"
            *> map2 (whitespace sexp) (whitespace sexp) ~f:(fun lhs rhs ->
                And (lhs, rhs)
            )
            |> parenthesis
          in
          let sor =
            string_ci "or"
            *> map2 (whitespace sexp) (whitespace sexp) ~f:(fun lhs rhs ->
                Or (lhs, rhs)
            )
            |> parenthesis
          in
          let snot =
            string_ci "not" *> map (whitespace sexp) ~f:(fun lhs -> Not lhs)
            |> parenthesis
          in
          choice [ sand; sor; snot; map ~f:(fun node -> Node node) query ]
      )
  end in
  Result.to_option @@ Angstrom.parse_string ~consume:All P.sexp s

let rec of_strings_advance = function
  | [] ->
      (None, [])
  | ("," | "^") :: q ->
      of_strings_advance q
  | t :: q ->
      (Some t, q)

let rec of_strings' node s =
  match s with
  | [] ->
      node
  | "," :: q ->
      let head, q = of_strings_advance q in
      let node =
        match head with
        | None ->
            node
        | Some t ->
            comsume (fun lhs rhs -> Or (lhs, rhs)) node t
      in
      of_strings' node q
  | "^" :: q ->
      let head, q = of_strings_advance q in
      let node =
        match head with
        | None ->
            node
        | Some t ->
            comsume (fun lhs rhs -> And (lhs, Not rhs)) node t
      in
      of_strings' node q
  | s :: q ->
      let node = comsume (fun lhs rhs -> And (lhs, rhs)) node s in
      of_strings' node q

let rec of_strings s =
  match s with
  | [] ->
      None
  | "," :: q ->
      of_strings q
  | "^" :: q -> (
      let head, q = of_strings_advance q in
      match head with
      | None ->
          of_strings q
      | Some t -> (
          match Angstrom.parse_string ~consume:All Parser.node_cli t with
          | Ok node' ->
              Some (of_strings' (Not (Node node')) q)
          | Error _ ->
              of_strings q
        )
    )
  | t :: q -> (
      match Angstrom.parse_string ~consume:All Parser.node_cli t with
      | Ok node' ->
          Some (of_strings' (Node node') q)
      | Error _ ->
          of_strings q
    )

let rec matches f query =
  match query with
  | Node node ->
      f node
  | And (lhs, rhs) ->
      matches f lhs && matches f rhs
  | Or (lhs, rhs) ->
      matches f lhs || matches f rhs
  | Not t ->
      not (matches f t)

let matches_musics subfields music_file album participants properties query =
  matches
    (QueryMusic.matches_node subfields
       (music_file, album, participants, properties)
    )
    query

let matches_albums subfields album stats participants query =
  matches (QueryAlbum.matches_node subfields (album, stats, participants)) query

let matches_artists subfields artist query =
  matches (QueryArtist.matches_node subfields artist) query