-
Notifications
You must be signed in to change notification settings - Fork 10
/
elfeed-score-serde.el
1583 lines (1416 loc) · 57.1 KB
/
elfeed-score-serde.el
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
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
;;; elfeed-score-serde.el --- SERDE `elfeed-score' -*- lexical-binding: t; -*-
;; Copyright (C) 2021-2024 Michael Herstine <[email protected]>
;; This program 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.
;; This program 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 this program. If not, see <https://www.gnu.org/licenses/>.
;;; Commentary:
;; This package provides `elfeed-score' serialization &
;; deserialization facilities.
;;; Code:
(require 'cl-lib)
(require 'elfeed-score-rules)
(require 'elfeed-score-rule-stats)
(define-obsolete-variable-alias
'elfeed-score-score-file
'elfeed-score-serde-score-file
"0.7.0"
"Refactoring elfeed-score.el.")
(defcustom elfeed-score-serde-score-file
(concat (expand-file-name user-emacs-directory) "elfeed.score")
"Location at which to persist scoring rules.
Set this to nil to disable automatic serialization &
deserialization of scoring rules."
:group 'elfeed-score
:type 'file)
(defconst elfeed-score-serde-current-format 10
"The most recent score file format version.")
(defvar elfeed-score-serde-title-rules nil
"List of structs each defining a scoring rule for entry titles.")
(defvar elfeed-score-serde-feed-rules nil
"List of structs each defining a scoring rule for entry feeds.")
(defvar elfeed-score-serde-authors-rules nil
"List of structs each defining a scoring rule for entry authors.")
(defvar elfeed-score-serde-content-rules nil
"List of structs each defining a scoring rule for entry content.")
(defvar elfeed-score-serde-title-or-content-rules nil
"List of structs each defining a scoring rule for entry title or content.")
(defvar elfeed-score-serde-tag-rules nil
"List of structs each defining a scoring rule for entry tags.")
(defvar elfeed-score-serde-link-rules nil
"List of structs each defining a scoring rule for entry links.")
(defvar elfeed-score-serde-udf-rules nil
"List of structs each defining a scoring rule based on a user-defined function.")
(defvar elfeed-score-serde-score-mark nil
"Score below which entries shall be marked as read.")
(defvar elfeed-score-serde-adjust-tags-rules nil
"List of rules to be run after scoring to adjust tags based on score.")
(defsubst elfeed-score-serde--nth (x i)
"Retrieve slot I of X."
(if (recordp x) (aref x i) (elt x i)))
;; This implementation is more general that this package requires--
;; `elfeed-score-rules' defines its structs in terms of records &
;; reserves no slots, so I could have used a more succinct
;; implementation that just runs through the slot info with no
;; additional logic. Still, I got interested in the CL struct
;; facility altogether & decided to try 'n come up with a general
;; implementation.
(defun elfeed-score-serde-struct-to-plist (x &rest params)
"Serialize a CL struct X to a plist. Keyword args in PARAMS.
The type of X is assumed to be in the first slot; if that is not
so, pass the :type keyword parameter with either an integer
giving the slot in which the type resides, or a symbol naming the
type of X.
By default, the resulting plist will just contain the state
necessary to deserialize X; the reader will have to \"just know\"
the type of X on read. To tag the plist with the type, pass the
keyword argument :type-tag with a non-nil value. In this case,
the plist will contain a property named :_type whose value will
be the structure name.
If the struct reserved slots using the :initial-offset option,
those slots will be serialized when non-nil with names of :_n
where n is the zero-baesd slot number."
(let ((plist)
(type-tag (plist-get params :type-tag))
(ty (plist-get params :type)))
(cl-loop for i upfrom 0
for (slot . rest) in
(cl-struct-slot-info
(cond
((integerp ty) (elfeed-score-serde--nth x ty))
((and ty (symbolp ty)) ty)
(t (elfeed-score-serde--nth x 0))))
do
(let ((val (elfeed-score-serde--nth x i)))
(cond
((eq slot 'cl-tag-slot)
(if type-tag
(setq plist (plist-put plist :_type val))))
((eq slot 'cl-skip-slot)
(if val
(setq plist (plist-put plist (intern (format ":_%d" i)) val))))
(t
;; We write `val' if it is *not* the default
(if (not (equal val (eval (car rest))))
(setq
plist
(plist-put plist (intern (concat ":" (symbol-name slot))) val)))))))
(if (and type-tag (not (plist-get plist :_type)))
(setq plist (plist-put plist :_type ty)))
plist))
;; Similar to `elfeed-score-serde-struct-to-plist', this
;; implementation is more general than this package strictly needs.
(defun elfeed-score-serde-plist-to-struct (plist &rest params)
"Deserialize PLIST to a struct; options in PARAMS.
PLIST is a property list presumably created by
`elfeed-score-serde-struct-to-plist': a property list containing
the state of a struct indexed by slot name. If the struct type
reserved slots, any non-nil values should be indexed in PLIST by
:_n where n is the zero-based slot number. The plist may,
optionally, contain the struct type indexed by :_type.
If the plist contains the type, this implementation will
deserialize the struct instance with no further information.
Else, the caller must supply the type name with the keyword
argument :type.
If the keyword argument :mandatory is present, it should be a
list of slot names to be checked. If any are not present, an
error will be flagged."
(let ((ty (or (plist-get plist :_type) (plist-get params :type)))
(mandatory (plist-get params :mandatory))
(vals))
(cl-loop
for i upfrom 0
for (slot . rest) in
(cl-struct-slot-info ty)
do
(cond
((eq slot 'cl-tag-slot)
(setq vals (append vals (list ty))))
((eq slot 'cl-skip-slot)
(setq vals (append vals (list (plist-get plist (intern (format ":_%d" i)))))))
(t
(let ((val
(or
(plist-get plist (intern (concat ":" (symbol-name slot))))
(eval (car rest)))))
(if (and
mandatory
(not val)
(member slot mandatory))
(error "Missing mandatory field %s" slot))
(setq vals (append vals (list val)))))))
(let ((seq-ty (cl-struct-sequence-type ty)))
(cond
((eq seq-ty 'list) vals)
((eq seq-ty 'vector) (apply #'vector vals))
(t (apply #'record (car vals) (cdr vals)))))))
(defun elfeed-score-serde--parse-title-rule-sexps (sexps)
"Parse a list of lists SEXPS into a list of title rules.
Each sub-list shall have the form (TEXT VALUE TYPE DATE TAGS HITS
FEEDS). NB Over the course of successive score file versions,
new fields have been added at the end so as to maintain backward
compatibility (i.e. this function can be used to read all
versions of the title rule serialization format prior to version
6).
Due to the change to storing rule stats outside the rule itself
beginning with version 8, this method will now return a list of
cons cells each of whose car is the rule structure and whose cdr
is the stats structure."
(cl-mapcar
(lambda (item)
(cons
(elfeed-score-title-rule--create :text (nth 0 item)
:value (nth 1 item)
:type (nth 2 item)
:tags (nth 4 item)
:feeds (nth 6 item))
(elfeed-score-rule-stats--create
:date (nth 3 item)
:hits (let ((hits (nth 5 item))) (or hits 0)))))
sexps))
(defun elfeed-score-serde--parse-feed-rule-sexps (sexps)
"Parse a list of lists SEXPS into a list of feed rules.
Each sub-list shall have the form (TEXT VALUE TYPE ATTR DATE TAGS
HITS). NB Over the course of successive score file versions, new
fields have been added at the end so as to maintain backward
compatibility (i.e. this function can be used to read all
versions of the feed rule serialization format prior to version
6).
Due to the change to storing rule stats outside the rule itself
beginning with version 8, this method will now return a list of
cons cells each of whose car is the rule structure and whose cdr
is the stats structure."
(cl-mapcar
(lambda (item)
(cons
(elfeed-score-feed-rule--create :text (nth 0 item)
:value (nth 1 item)
:type (nth 2 item)
:attr (nth 3 item)
:tags (nth 5 item))
(elfeed-score-rule-stats--create
:date (nth 4 item)
:hits (let ((hits (nth 6 item))) (or hits 0)))))
sexps))
(defun elfeed-score-serde--parse-content-rule-sexps (sexps)
"Parse a list of lists SEXPS into a list of content rules.
Each sub-list shall have the form (TEXT VALUE TYPE DATE TAGS HITS
FEEDS). NB Over the course of successive score file versions,
new fields have been added at the end so as to maintain backward
compatibility (i.e. this function can be used to read all
versions of the content rule serialization format prior to
version 6).
Due to the change to storing rule stats outside the rule itself
beginning with version 8, this method will now return a list of
cons cells each of whose car is the rule structure and whose cdr
is the stats structure."
(cl-mapcar
(lambda (item)
(cons
(elfeed-score-content-rule--create :text (nth 0 item)
:value (nth 1 item)
:type (nth 2 item)
:tags (nth 4 item)
:feeds (nth 6 item))
(elfeed-score-rule-stats--create
:date (nth 3 item)
:hits (let ((hits (nth 5 item))) (or hits 0)))))
sexps))
(defun elfeed-score-serde--parse-title-or-content-rule-sexps (sexps)
"Parse a list of lists SEXPS into a list of title-or-content rules.
Each sub-list shall have the form '(TEXT TITLE-VALUE
CONTENT-VALUE TYPE DATE TAGS HITS FEEDS). NB Over the course of
successive score file versions, new fields have been added at the
end so as to maintain backward compatibility (i.e. this function
can be used to read all versions of the title-or-content rule
serialization format prior to version 6).
Due to the change to storing rule stats outside the rule itself
beginning with version 8, this method will now return a list of
cons cells each of whose car is the rule structure and whose cdr
is the stats structure."
(cl-mapcar
(lambda (item)
(cons
(elfeed-score-title-or-content-rule--create
:text (nth 0 item)
:title-value (nth 1 item)
:content-value (nth 2 item)
:type (nth 3 item)
:tags (nth 5 item)
:feeds (nth 7 item))
(elfeed-score-rule-stats--create
:date (nth 4 item)
:hits (let ((hits (nth 6 item))) (or hits 0)))))
sexps))
(defun elfeed-score-serde--parse-scoring-sexp-1 (sexp)
"Interpret the S-expression SEXP as scoring rules version 1.
Parse version 1 of the scoring S-expression. This function will
fail if SEXP has a \"version\" key with a value other than 1 (the
caller may want to remove it via `assoc-delete-all' or some
such). Return a property list with the following keys & values:
- :title : list of cons cells whose car-s are
elfeed-score-title-rule structs & whose cdr-s are stats
structs
- :content : similar list of elfeed-score-content-rule &
stats structs
- :feed : similar list of elfeed-score-feed-rule & stats
structs
- :mark : score below which entries shall be marked read"
(let (mark titles feeds content)
(dolist (raw-item sexp)
(let ((key (car raw-item))
(rest (cdr raw-item)))
(cond
((string= key "version")
(unless (eq 1 (car rest))
(error "Unsupported score file version %s" (car rest))))
((string= key "title")
(setq titles (elfeed-score-serde--parse-title-rule-sexps rest)))
((string= key "content")
(setq content (elfeed-score-serde--parse-content-rule-sexps rest)))
((string= key "feed")
(setq feeds (elfeed-score-serde--parse-feed-rule-sexps rest)))
((eq key 'mark)
;; set `mark' to (cdr rest) if (not mark) or (< mark (cdr rest))
(let ((rest (car rest)))
(if (or (not mark)
(< mark rest))
(setq mark rest))))
(t
(error "Unknown score file key %s" key)))))
(list
:mark mark
:feeds feeds
:titles titles
:content content)))
(defun elfeed-score-serde--parse-scoring-sexp-2 (sexp)
"Interpret the S-expression SEXP as scoring rules version 2.
Parse version 2 of the scoring S-expression. Return a property list
with the following keys & values:
- :title : list of cons cells, each of whose car is an
elfeed-score-title-rule struct & whose cdr is a stats
struct
- :content : similar list of cons cells containing
elfeed-score-content-rule & stats structs
- :title-or-content: similar list of cons cells containing
elfeed-score-title-or-content-rule & stats structs
- :feed : similar list of cons cells containing
elfeed-score-feed-rule & stats structs
- :mark : score below which entries shall be marked read"
(let (mark titles feeds content tocs)
(dolist (raw-item sexp)
(let ((key (car raw-item))
(rest (cdr raw-item)))
(cond
((string= key "version")
(unless (eq 2 (car rest))
(error "Unsupported score file version %s" (car rest))))
((string= key "title")
(setq titles (elfeed-score-serde--parse-title-rule-sexps rest)))
((string= key "content")
(setq content (elfeed-score-serde--parse-content-rule-sexps rest)))
((string= key "feed")
(setq feeds (elfeed-score-serde--parse-feed-rule-sexps rest)))
((string= key "title-or-content")
(setq tocs (elfeed-score-serde--parse-title-or-content-rule-sexps rest)))
((eq key 'mark)
;; set `mark' to (cdr rest) if (not mark) or (< mark (cdr rest))
(let ((rest (car rest)))
(if (or (not mark)
(< mark rest))
(setq mark rest))))
(t
(error "Unknown score file key %s" key)))))
(list
:mark mark
:feeds feeds
:titles titles
:content content
:title-or-content tocs)))
(defun elfeed-score-serde--parse-tag-rule-sexps (sexps)
"Parse a list of lists SEXPS into a list of tag rules.
Each sub-list shall have the form '(TAGS VALUE DATE HITS). NB
Over the course of successive score file versions, new fields
have been added at the end so as to maintain backward
compatibility (i.e. this function can be used to read all
versions of the tag rule serialization format prior to version
6).
Due to the change to storing rule stats outside the rule itself
beginning with version 8, this method will now return a list of
cons cells each of whose car is the rule structure and whose cdr
is the stats structure."
(cl-mapcar
(lambda (item)
(cons
(elfeed-score-tag-rule--create :tags (nth 0 item)
:value (nth 1 item))
(elfeed-score-rule-stats--create
:date (nth 2 item)
:hits (let ((hits (nth 5 item))) (or hits 0)))))
sexps))
(defun elfeed-score-serde--parse-adjust-tags-rule-sexps (sexps)
"Parse a list of lists SEXPS into a list of adjust-tags rules.
Each sub-list shall have the form '(TAGS VALUE DATE HITS). NB
Over the course of successive score file versions, new fields
have been added at the end so as to maintain backward
compatibility (i.e. this function can be used to read all
versions of the adjust-tag rule serialization format prior to
version 6).
Due to the change to storing rule stats outside the rule itself
beginning with version 8, this method will now return a list of
cons cells each of whose car is the rule structure and whose cdr
is the stats structure."
(cl-mapcar
(lambda (item)
(cons
(elfeed-score-adjust-tags-rule--create :threshold (nth 0 item)
:tags (nth 1 item))
(elfeed-score-rule-stats--create
:date (nth 2 item)
:hits (let ((hits (nth 5 item))) (or hits 0)))))
sexps))
(defun elfeed-score-serde--parse-scoring-sexp-3 (sexp)
"Interpret the S-expression SEXP as scoring rules version 3.
Parse version 3 of the scoring S-expression. Return a property list
with the following keys & values:
- :title : list of cons cells each of whose car is an
elfeed-score-title-rule struct & each of whose cdr is a
stats struct
- :content : similar list of cons cells containing
elfeed-score-content-rule & stats structs
- :title-or-content: similar list of cons cells containing
elfeed-score-title-or-content-rule & stats structs
- :feed : similar list of cons cells containing
elfeed-score-feed-rule & stats structs
- :tag : similar list of cons cells containing
elfeed-score-tag-rule & stats structs
- :adjust-tags : similar list of cons cells containing
elfeed-score-adjust-tags-rule & stats structs
- :mark : score below which entries shall be marked read"
(let (mark titles feeds content tocs tags adj-tags)
(dolist (raw-item sexp)
(let ((key (car raw-item))
(rest (cdr raw-item)))
(cond
((string= key "version")
(unless (eq 3 (car rest))
(error "Unsupported score file version %s" (car rest))))
((string= key "title")
(setq titles (elfeed-score-serde--parse-title-rule-sexps rest)))
((string= key "content")
(setq content (elfeed-score-serde--parse-content-rule-sexps rest)))
((string= key "feed")
(setq feeds (elfeed-score-serde--parse-feed-rule-sexps rest)))
((string= key "title-or-content")
(setq tocs (elfeed-score-serde--parse-title-or-content-rule-sexps rest)))
((string= key "tag")
(setq tags (elfeed-score-serde--parse-tag-rule-sexps rest)))
((string= key "adjust-tags")
(setq adj-tags (elfeed-score-serde--parse-adjust-tags-rule-sexps rest)))
((eq key 'mark)
;; set `mark' to (cdr rest) if (not mark) or (< mark (cdr rest))
(let ((rest (car rest)))
(if (or (not mark)
(< mark rest))
(setq mark rest))))
(t
(error "Unknown score file key %s" key)))))
(list
:mark mark
:adjust-tags adj-tags
:feeds feeds
:titles titles
:content content
:title-or-content tocs
:tag tags)))
(defun elfeed-score-serde--parse-authors-rule-sexps (sexps)
"Parse a list of lists SEXPS into a list of authors rules.
Each sub-list shall have the form '(TEXT VALUE TYPE DATE TAGS
HITS FEEDS). NB Over the course of successive score file
versions, new fields have been added at the end so as to maintain
backward compatibility (i.e. this function can be used to read
all versions of the authors rule serialization format prior to
version 6).
Due to the change to storing rule stats outside the rule itself
beginning with version 8, this method will now return a list of
cons cells each of whose car is the rule structure and whose cdr
is the stats structure."
(cl-mapcar
(lambda (item)
(cons
(elfeed-score-authors-rule--create :text (nth 0 item)
:value (nth 1 item)
:type (nth 2 item)
:tags (nth 4 item)
:feeds (nth 6 item))
(elfeed-score-rule-stats--create
:date (nth 3 item)
:hits (let ((hits (nth 5 item))) (or hits 0)))))
sexps))
(defun elfeed-score-serde--parse-scoring-sexp-4 (sexp)
"Interpret the S-expression SEXP as scoring rules version 4.
Parse version 4 of the scoring S-expression. Return a property list
with the following keys:
- :title : list of cons cells each of whose car is an
elfeed-score-title-rule struct & each of whose cdr is a
stats struct
- :content : similar list of cons cells containing
elfeed-score-content-rule & stats structs
- :title-or-content: similar list of cons cells containing
elfeed-score-title-or-content-rule & stats structs
- :feed : similar list of cons cells containing
elfeed-score-feed-rule & stats structs
- :tag : similar list of cons cells containing
elfeed-score-tag-rule & stats structs
- :adjust-tags : similar list of cons cells containing
elfeed-score-adjust-tags-rule & stats structs
- :authors : similar list of cons cells containing
elfeed-score-authors-rule & stats structs
- :mark : score below which entries shall be marked read"
(let (mark titles feeds content tocs authors tags adj-tags)
(dolist (raw-item sexp)
(let ((key (car raw-item))
(rest (cdr raw-item)))
(cond
((string= key "version")
(unless (or (eq 4 (car rest)) (eq 5 (car rest)))
(error "Unsupported score file version %s" (car rest))))
((string= key "title")
(setq titles (elfeed-score-serde--parse-title-rule-sexps rest)))
((string= key "content")
(setq content (elfeed-score-serde--parse-content-rule-sexps rest)))
((string= key "feed")
(setq feeds (elfeed-score-serde--parse-feed-rule-sexps rest)))
((string= key "title-or-content")
(setq tocs (elfeed-score-serde--parse-title-or-content-rule-sexps rest)))
((string= key "authors")
(setq authors (elfeed-score-serde--parse-authors-rule-sexps rest)))
((string= key "tag")
(setq tags (elfeed-score-serde--parse-tag-rule-sexps rest)))
((string= key "adjust-tags")
(setq adj-tags (elfeed-score-serde--parse-adjust-tags-rule-sexps rest)))
((eq key 'mark)
;; set `mark' to (cdr rest) if (not mark) or (< mark (cdr rest))
(let ((rest (car rest)))
(if (or (not mark)
(< mark rest))
(setq mark rest))))
(t
(error "Unknown score file key %s" key)))))
(list
:mark mark
:adjust-tags adj-tags
:feeds feeds
:titles titles
:content content
:title-or-content tocs
:authors authors
:tag tags)))
(defun elfeed-score-serde--plist-to-ctor (plist attrs ctor)
"Pull ATTRS from PLIST & hand them to CTOR.
ATTRS shall be a list of keywords (:text, or :date, e.g.). This
function creates a list of those keywords followed by that
keyword's value in PLIST (if any) & applies CTOR to that list."
(let* ((zip (cl-mapcar
#'cons
attrs
(cl-mapcar (lambda (x) (plist-get plist x)) attrs)))
(elems))
;; `zip' is a list of cons cells (keyword . value)
(while (consp zip)
(let ((elem (pop zip)))
(if (cdr elem)
(setq elems (append elems (list (car elem) (cdr elem)))))))
(apply ctor elems)))
;; For score file version 6 & 7, we need to deserialize to both a rule
;; & a stats object.
(defun elfeed-score-serde--plist-to-struct-6-7 (plist ty)
"Deserialize PLIST to a struct of type TY in score file versions 6 & 7.
Prior to version 8 of the score file format, rule statistics were
kept with the corresponding rule and serialized to the same
property list. Therefore, to deserialize, we need to read the
property lists into two structures: the rule & the stats. Both
are returned as a cons cell."
(cons
(cond
((eq ty 'elfeed-score-title-rule)
(elfeed-score-serde--plist-to-ctor
plist '(:text :value :type :tags :feeds)
#'elfeed-score-title-rule--create))
((eq ty 'elfeed-score-feed-rule)
(elfeed-score-serde--plist-to-ctor
plist '(:text :value :type :attr :tags)
#'elfeed-score-feed-rule--create))
((eq ty 'elfeed-score-content-rule)
(elfeed-score-serde--plist-to-ctor
plist '(:text :value :type :tags :feeds)
#'elfeed-score-content-rule--create))
((eq ty 'elfeed-score-title-or-content-rule)
(elfeed-score-serde--plist-to-ctor
plist '(:text :title-value :content-value :type :tags :feeds)
#'elfeed-score-title-or-content-rule--create))
((eq ty 'elfeed-score-authors-rule)
(elfeed-score-serde--plist-to-ctor
plist '(:text :value :type :tags :feeds)
#'elfeed-score-authors-rule--create))
((eq ty 'elfeed-score-tag-rule)
(elfeed-score-serde--plist-to-ctor
plist '(:tags :value)
#'elfeed-score-tag-rule--create))
((eq ty 'elfeed-score-link-rule)
(elfeed-score-serde--plist-to-ctor
plist '(:text :value :type :tags :feeds)
#'elfeed-score-link-rule--create))
((eq ty 'elfeed-score-adjust-tags-rule)
(elfeed-score-serde--plist-to-ctor
plist '(:threshold :tags)
#'elfeed-score-adjust-tags-rule--create))
(t
(error "Unknown rule type %s" ty)))
(elfeed-score-serde--plist-to-ctor
plist '(:hits :date) #'elfeed-score-rule-stats--create)))
(defun elfeed-score-serde--parse-scoring-sexp-6 (sexp)
"Interpret the S-expression SEXP as scoring rules version 6.
Parse version 6 of the scoring S-expression. Version 6
introduced keywords to the serialization format. Return a
property list with the following keys:
- :title : list of cons cells each of whose car is an
elfeed-score-title-rule struct & each of whose cdr is a
stats struct
- :content : similar list of cons cells containing
elfeed-score-content-rule & stats structs
- :title-or-content: similar list of cons cells containing
elfeed-score-title-or-content-rule & stats structs
- :feed : similar list of cons cells containing
elfeed-score-feed-rule & stats structs
- :tag : similar list of cons cells containing
elfeed-score-tag-rule & stats structs
- :adjust-tags : similar list of cons cells containing
elfeed-score-adjust-tags-rule & stats structs
- :authors : similar list of cons cells containing
elfeed-score-authors-rule & stats structs
- :mark : score below which entries shall be marked read"
(let (mark titles feeds content tocs authors tags adj-tags)
(dolist (raw-item sexp)
(let ((key (car raw-item))
(rest (cdr raw-item)))
(cond
((string= key "version")
(unless (eq 6 (car rest))
(error "Unsupported score file version %s" (car rest))))
((string= key "title")
(setq
titles
(mapcar
(lambda (plist)
(elfeed-score-serde--plist-to-struct-6-7 plist 'elfeed-score-title-rule))
rest)))
((string= key "content")
(setq
content
(mapcar
(lambda (plist)
(elfeed-score-serde--plist-to-struct-6-7 plist 'elfeed-score-content-rule))
rest)))
((string= key "feed")
(setq
feeds
(mapcar
(lambda (plist)
(elfeed-score-serde--plist-to-struct-6-7 plist 'elfeed-score-feed-rule))
rest)))
((string= key "title-or-content")
(setq
tocs
(mapcar
(lambda (plist)
(elfeed-score-serde--plist-to-struct-6-7
plist 'elfeed-score-title-or-content-rule))
rest)))
((string= key "authors")
(setq
authors
(mapcar
(lambda (plist)
(elfeed-score-serde--plist-to-struct-6-7 plist 'elfeed-score-authors-rule))
rest)))
((string= key "tag")
(setq
tags
(mapcar
(lambda (plist)
(elfeed-score-serde--plist-to-struct-6-7 plist 'elfeed-score-tag-rule))
rest)))
((string= key "adjust-tags")
(setq
adj-tags
(mapcar
(lambda (plist)
(elfeed-score-serde--plist-to-struct-6-7 plist 'elfeed-score-adjust-tags-rule))
rest)))
((eq key 'mark)
;; set `mark' to (cdr rest) if (not mark) or (< mark (cdr rest))
(let ((rest (car rest)))
(if (or (not mark)
(< mark rest))
(setq mark rest))))
(t
(error "Unknown score file key %s" key)))))
(list
:mark mark
:adjust-tags adj-tags
:feeds feeds
:titles titles
:content content
:title-or-content tocs
:authors authors
:tag tags)))
(defun elfeed-score-serde--parse-scoring-sexp-7 (sexp)
"Interpret the S-expression SEXP as scoring rules version 7.
Parse version 7 of the scoring S-expression. Return a property list
with the following keys:
- :title : list of cons cells each of whose car is an
elfeed-score-title-rule struct & each of whose cdr is a
stats struct
- :content : similar list of cons cells containing
elfeed-score-content-rule & stats structs
- :title-or-content: similar list of cons cells containing
elfeed-score-title-or-content-rule & stats structs
- :feed : similar list of cons cells containing
elfeed-score-feed-rule & stats structs
- :tag : similar list of cons cells containing
elfeed-score-tag-rule & stats structs
- :adjust-tags : similar list of cons cells containing
elfeed-score-adjust-tags-rule & stats structs
- :authors : similar list of cons cells containing
elfeed-score-authors-rule & stats structs
- :link : similar list of cons cells containing
elfeed-score-link-rule & stats structs
- :mark : score below which entries shall be marked read"
(let (mark titles feeds content tocs authors tags links adj-tags)
(dolist (raw-item sexp)
(let ((key (car raw-item))
(rest (cdr raw-item)))
(cond
((string= key "version")
(unless (eq 7 (car rest))
(error "Unsupported score file version %s" (car rest))))
((string= key "title")
(setq
titles
(mapcar
(lambda (plist)
(elfeed-score-serde--plist-to-struct-6-7 plist 'elfeed-score-title-rule))
rest)))
((string= key "content")
(setq
content
(mapcar
(lambda (plist)
(elfeed-score-serde--plist-to-struct-6-7 plist 'elfeed-score-content-rule))
rest)))
((string= key "feed")
(setq
feeds
(mapcar
(lambda (plist)
(elfeed-score-serde--plist-to-struct-6-7 plist 'elfeed-score-feed-rule))
rest)))
((string= key "title-or-content")
(setq
tocs
(mapcar
(lambda (plist)
(elfeed-score-serde--plist-to-struct-6-7
plist 'elfeed-score-title-or-content-rule))
rest)))
((string= key "authors")
(setq
authors
(mapcar
(lambda (plist)
(elfeed-score-serde--plist-to-struct-6-7 plist 'elfeed-score-authors-rule))
rest)))
((string= key "tag")
(setq
tags
(mapcar
(lambda (plist)
(elfeed-score-serde--plist-to-struct-6-7 plist 'elfeed-score-tag-rule))
rest)))
((string= key "link")
(setq
links
(mapcar
(lambda (plist)
(elfeed-score-serde--plist-to-struct-6-7 plist 'elfeed-score-link-rule))
rest)))
((string= key "adjust-tags")
(setq
adj-tags
(mapcar
(lambda (plist)
(elfeed-score-serde--plist-to-struct-6-7 plist 'elfeed-score-adjust-tags-rule))
rest)))
((eq key 'mark)
;; set `mark' to (cdr rest) if (not mark) or (< mark (cdr rest))
(let ((rest (car rest)))
(if (or (not mark)
(< mark rest))
(setq mark rest))))
(t
(error "Unknown score file key %s" key)))))
(list
:mark mark
:adjust-tags adj-tags
:feeds feeds
:titles titles
:content content
:title-or-content tocs
:authors authors
:tag tags
:link links)))
(defun elfeed-score-serde--parse-scoring-sexp-8 (sexp)
"Interpret the S-expression SEXP as scoring rules version 8 or 9.
Parse versions 8 & 9 of the scoring S-expression. With version 8
of the score file format, the rule stats are stored separately,
so this file won't load them. However, it will still return
lists of cons cells (with the cdr set to nil) to allow uniform
processing in our caller):
- :title : list of cons cells each of whose car is an
elfeed-score-title-rule struct & each of whose cdr is nil
- :content : similar list of cons cells containing
elfeed-score-content-rule structs
- :title-or-content: similar list of cons cells containing
elfeed-score-title-or-content-rule structs
- :feed : similar list of cons cells containing
elfeed-score-feed-rule structs
- :tag : similar list of cons cells containing
elfeed-score-tag-rule structs
- :adjust-tags : similar list of cons cells containing
elfeed-score-adjust-tags-rule structs
- :authors : similar list of cons cells containing
elfeed-score-authors-rule structs
- :link : similar list of cons cells containing
elfeed-score-link-rule structs
- :mark : score below which entries shall be marked read"
(let (mark titles feeds content tocs authors tags links adj-tags)
(dolist (raw-item sexp)
(let ((key (car raw-item))
(rest (cdr raw-item)))
(cond
((string= key "version")
(unless (or (eq 9 (car rest)) (eq 8 (car rest)))
(error "Unsupported score file version %s" (car rest))))
((string= key "title")
(setq
titles
(mapcar
(lambda (plist)
(list
(elfeed-score-serde-plist-to-struct
plist
:type 'elfeed-score-title-rule
:mandatory '(text value type))))
rest)))
((string= key "content")
(setq
content
(mapcar
(lambda (plist)
(list
(elfeed-score-serde-plist-to-struct
plist
:type 'elfeed-score-content-rule
:mandatory '(text value type))))
rest)))
((string= key "feed")
(setq
feeds
(mapcar
(lambda (plist)
(list
(elfeed-score-serde-plist-to-struct
plist
:type 'elfeed-score-feed-rule
:mandatory '(text value type attr))))
rest)))
((string= key "title-or-content")
(setq
tocs
(mapcar
(lambda (plist)
(list
(elfeed-score-serde-plist-to-struct
plist
:type 'elfeed-score-title-or-content-rule
:mandatory '(text title-value content-value type))))
rest)))
((string= key "authors")
(setq
authors
(mapcar
(lambda (plist)
(list
(elfeed-score-serde-plist-to-struct
plist
:type 'elfeed-score-authors-rule
:mandatory '(text value type))))
rest)))
((string= key "tag")
(setq
tags
(mapcar
(lambda (plist)
(list