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
//! Base MARC record model and associated components.
use crate::query::ComplexSpecification;
const TAG_SIZE: usize = 3;
const LEADER_SIZE: usize = 24;
const CODE_SIZE: usize = 1;
const DEFAULT_LEADER: &str = " ";
const DEFAULT_INDICATOR: &str = " ";
const ESTIMATED_NUMBER_OF_FIELDS: usize = 30;
const ESTIMATED_NUMBER_OF_SUBFIELDS: usize = 5;
/// Verifies the provided string is composed of 'len' number of bytes.
fn check_byte_count(s: &str, len: usize) -> Result<(), String> {
let byte_len = s.len();
if byte_len != len {
return Err(format!(
"Invalid byte count for string s={s} wanted={len} found={byte_len}"
));
}
Ok(())
}
/// MARC Control Field whose tag value is < "010"
#[derive(Debug, Clone, PartialEq)]
pub struct Controlfield {
tag: String,
content: String,
}
impl Controlfield {
/// Create a Controlfield with the provided tag and content.
///
/// * `tag` - Must have the correct byte count.
///
/// # Examples
///
/// ```
/// let control_field = marctk::Controlfield::new("008", "12345").unwrap();
/// assert_eq!(control_field.tag(), "008");
/// ```
/// ```
/// let control_field = marctk::Controlfield::new("010", "12345");
///
/// assert_eq!(control_field.is_err(), true);
/// assert_eq!(control_field.unwrap_err(), "Invalid Controlfield tag: 010");
/// ```
pub fn new(tag: impl Into<String>, content: impl Into<String>) -> Result<Self, String> {
let tag = tag.into();
check_byte_count(&tag, TAG_SIZE)?;
if tag.as_str() < "000" || tag.as_str() > "009" {
return Err(format!("Invalid Controlfield tag: {tag}"));
}
Ok(Self::new_unchecked(tag, content))
}
/// A Controlfield with any tag, as a parser found it.
pub(crate) fn new_unchecked(tag: impl Into<String>, content: impl Into<String>) -> Self {
Controlfield {
tag: tag.into(),
content: content.into(),
}
}
/// Get the tag
///
/// # Examples
///
/// ```
/// use marctk::Controlfield;
///
/// let control_field = Controlfield::new("008", "12345").unwrap();
/// assert_eq!(control_field.tag(), "008");
/// ```
pub fn tag(&self) -> &str {
&self.tag
}
/// Get the content
///
/// # Examples
///
/// ```
/// use marctk::Controlfield;
///
/// let control_field = Controlfield::new("008", "12345").unwrap();
/// assert_eq!(control_field.content(), "12345");
/// ```
pub fn content(&self) -> &str {
&self.content
}
/// Set the Controlfield content.
///
/// # Examples
///
/// ```
/// use marctk::Controlfield;
///
/// let mut control_field = Controlfield::new("008", "12345").unwrap();
/// control_field.set_content("6789");
/// assert_eq!(control_field.content(), "6789");
/// ```
pub fn set_content(&mut self, content: impl Into<String>) {
self.content = content.into();
}
}
/// A single subfield code + value pair
#[derive(Debug, Clone, PartialEq)]
pub struct Subfield {
code: String,
content: String,
}
impl Subfield {
/// Create a Subfield with the provided code and content.
///
/// * `code` - Must have the correct byte count.
///
/// # Examples
///
/// ```
/// use marctk::Subfield;
/// let subfield: Subfield = match Subfield::new("a", "Στη σκιά της πεταλούδας") {
/// Ok(sf) => sf,
/// Err(e) => panic!("Subfield::new() failed with: {}", e),
/// };
/// assert_eq!(subfield.content(), "Στη σκιά της πεταλούδας");
/// ```
///
/// ```should_panic
/// use marctk::Subfield;
/// Subfield::new("🦋", "Στη σκιά της πεταλούδας").unwrap();
/// ```
///
pub fn new(code: impl Into<String>, content: impl Into<String>) -> Result<Self, String> {
let code = code.into();
check_byte_count(&code, CODE_SIZE)?;
Ok(Self::new_unchecked(code, content))
}
/// A Subfield with any code, as a parser found it.
pub(crate) fn new_unchecked(code: impl Into<String>, content: impl Into<String>) -> Self {
Subfield {
code: code.into(),
content: content.into(),
}
}
/// Get the Subfield content.
pub fn content(&self) -> &str {
&self.content
}
/// Set the Subfield content.
///
/// # Examples
///
/// ```
/// use marctk::Subfield;
/// let mut subfield: Subfield = Subfield::new("a", "potato").unwrap();
/// subfield.set_content("cheese");
/// assert_eq!(subfield.content(), "cheese");
/// ```
///
pub fn set_content(&mut self, content: impl Into<String>) {
self.content = content.into();
}
/// Get the Subfield code.
///
/// # Examples
///
/// ```
/// use marctk::Subfield;
/// let subfield: Subfield = Subfield::new("a", "potato").unwrap();
/// assert_eq!(subfield.code(), "a");
/// ```
///
pub fn code(&self) -> &str {
&self.code
}
/// Set the Subfield code.
///
/// # Examples
///
/// ```
/// use marctk::Subfield;
/// let mut subfield: Subfield = Subfield::new("a", "potato").unwrap();
/// subfield.set_code("q");
/// assert_eq!(subfield.code(), "q");
/// ```
///
/// ```should_panic
/// use marctk::Subfield;
/// let mut subfield: Subfield = Subfield::new("a", "potato").unwrap();
/// subfield.set_code("🥔").unwrap();
/// ```
///
pub fn set_code(&mut self, code: impl Into<String>) -> Result<(), String> {
let code: String = code.into();
check_byte_count(&code, CODE_SIZE)?;
self.code = code;
Ok(())
}
}
/// A MARC Data Field with tag, indicators, and subfields.
#[derive(Debug, Clone, PartialEq)]
pub struct Field {
tag: String,
ind1: Option<String>,
ind2: Option<String>,
subfields: Vec<Subfield>,
}
impl Field {
/// Create a Field with the provided tag.
///
/// * `tag` - Must have the correct byte count. Any three bytes will
/// do, not only 010 through 999: OCLC, for one, uses "DAT".
///
/// # Examples
///
/// ```
/// use marctk::Field;
///
/// let field: Field = match Field::new("245") {
/// Ok(f) => f,
/// Err(e) => panic!("Field::new() failed with: {}", e),
/// };
/// assert_eq!(field.tag(), "245");
/// assert_eq!(field.ind1(), " ");
/// assert_eq!(field.ind2(), " ");
/// assert_eq!(field.subfields().len(), 0);
///
/// assert_eq!(Field::new("DAT").unwrap().tag(), "DAT");
/// assert!(Field::new("24").is_err());
/// ```
///
pub fn new(tag: impl Into<String>) -> Result<Self, String> {
let tag = tag.into();
check_byte_count(&tag, TAG_SIZE)?;
Ok(Self::new_unchecked(tag))
}
/// A Field with any tag, as a parser found it.
pub(crate) fn new_unchecked(tag: impl Into<String>) -> Self {
Field {
tag: tag.into(),
ind1: None,
ind2: None,
subfields: Vec::with_capacity(ESTIMATED_NUMBER_OF_SUBFIELDS),
}
}
/// Get the tag
pub fn tag(&self) -> &str {
&self.tag
}
/// Get the value of indicator-1, defaulting to DEFAULT_INDICATOR.
pub fn ind1(&self) -> &str {
self.ind1.as_deref().unwrap_or(DEFAULT_INDICATOR)
}
/// Get the value of indicator-2, defaulting to DEFAULT_INDICATOR.
pub fn ind2(&self) -> &str {
self.ind2.as_deref().unwrap_or(DEFAULT_INDICATOR)
}
/// Get the full list of subfields
pub fn subfields(&self) -> &Vec<Subfield> {
&self.subfields
}
/// Get a mutable list of subfields.
pub fn subfields_mut(&mut self) -> &mut Vec<Subfield> {
&mut self.subfields
}
/// Set the indicator-1 value.
///
/// * `ind` - Must have the correct byte count.
pub fn set_ind1(&mut self, ind: impl Into<String>) -> Result<(), String> {
let ind = ind.into();
check_byte_count(&ind, CODE_SIZE)?;
self.ind1 = Some(ind);
Ok(())
}
/// Set the indicator-2 value.
///
/// * `ind` - Must have the correct byte count.
pub fn set_ind2(&mut self, ind: impl Into<String>) -> Result<(), String> {
let ind = ind.into();
check_byte_count(&ind, CODE_SIZE)?;
self.ind2 = Some(ind);
Ok(())
}
/// Set indicator-1 as a parser found it. An empty one reads as
/// blank, as a missing one does.
pub(crate) fn set_ind1_unchecked(&mut self, ind: impl Into<String>) {
let ind = ind.into();
self.ind1 = (!ind.is_empty()).then_some(ind);
}
/// Set indicator-2 as a parser found it. An empty one reads as
/// blank, as a missing one does.
pub(crate) fn set_ind2_unchecked(&mut self, ind: impl Into<String>) {
let ind = ind.into();
self.ind2 = (!ind.is_empty()).then_some(ind);
}
/// Get a list of subfields with the provided code.
pub fn get_subfields(&self, code: &str) -> Vec<&Subfield> {
self.subfields.iter().filter(|f| f.code() == code).collect()
}
/// Get the first occurrence of the subfield with the provided code,
/// if one is present.
///
/// # Examples
///
/// ```
/// use marctk::Field;
///
/// let mut field: Field = Field::new("245").unwrap();
/// assert!(field.first_subfield("a").is_none());
///
/// field.add_subfield("a", "First one").unwrap();
/// field.add_subfield("a", "Second one").unwrap();
///
/// assert_eq!(field.first_subfield("a").unwrap().content(), "First one");
/// ```
pub fn first_subfield(&self, code: &str) -> Option<&Subfield> {
self.subfields.iter().find(|f| f.code() == code)
}
/// Mutable variant of ['first_subfield()`].
///
/// # Examples
///
/// ```
/// use marctk::Field;
///
/// let mut field: Field = Field::new("245").unwrap();
/// assert!(field.first_subfield("a").is_none());
///
/// field.add_subfield("a", "First one").unwrap();
///
/// field.first_subfield_mut("a").unwrap().set_content("Other text");
///
/// assert_eq!(field.first_subfield("a").unwrap().content(), "Other text");
/// ```
pub fn first_subfield_mut(&mut self, code: &str) -> Option<&mut Subfield> {
self.subfields.iter_mut().find(|f| f.code() == code)
}
/// True if a subfield with the provided code is present.
///
/// # Examples
///
/// ```
/// use marctk::Field;
///
/// let mut field: Field = Field::new("245").unwrap();
/// assert!(!field.has_subfield("a"));
///
/// field.add_subfield("a", "My title").unwrap();
///
/// assert!(field.has_subfield("a"));
/// ```
pub fn has_subfield(&self, code: &str) -> bool {
self.subfields.iter().any(|f| f.code() == code)
}
/// Get a mutable list of subfields with the provided code.
///
/// # Examples
///
/// ```
/// use marctk::Field;
///
/// let mut field: Field = Field::new("245").unwrap();
/// field.add_subfield("a", "First one").unwrap();
/// field.add_subfield("a", "Second one").unwrap();
///
/// for mut subfield in field.get_subfields_mut("a") {
/// subfield.set_content(subfield.content().to_uppercase());
/// }
///
/// assert_eq!(field.first_subfield("a").unwrap().content(), "FIRST ONE");
/// ```
pub fn get_subfields_mut(&mut self, code: &str) -> Vec<&mut Subfield> {
self.subfields
.iter_mut()
.filter(|f| f.code() == code)
.collect()
}
/// Adds a new Subfield to this field using the provided code and content.
///
/// * `code` - Must have the correct byte count.
pub fn add_subfield(
&mut self,
code: impl Into<String>,
content: impl Into<String>,
) -> Result<(), String> {
self.subfields.push(Subfield::new(code, content)?);
Ok(())
}
/// Remove the first subfield with the specified code.
///
/// # Examples
///
/// ```
/// use marctk::Field;
///
/// let mut field: Field = Field::new("245").unwrap();
/// field.add_subfield("a", "First one").unwrap();
/// field.add_subfield("a", "Second one").unwrap();
/// assert_eq!(field.subfields().len(), 2);
///
/// assert_eq!(field.remove_first_subfield("a").unwrap().content(), "First one");
/// assert_eq!(field.subfields().len(), 1);
/// assert_eq!(field.first_subfield("a").unwrap().content(), "Second one");
/// ```
pub fn remove_first_subfield(&mut self, code: &str) -> Option<Subfield> {
if let Some(index) = self.subfields.iter().position(|s| s.code.eq(code)) {
return Some(self.subfields.remove(index));
}
None
}
/// Remove all subfields with the specified code and returns
/// the count of removed subfields.
///
/// # Examples
///
/// ```
/// use marctk::Field;
/// let mut field = Field::new("505").unwrap();
/// let _ = field.add_subfield("t", "Chapter 1 /");
/// let _ = field.add_subfield("r", "Cool author --");
/// let _ = field.add_subfield("t", "Chapter 2.");
/// assert_eq!(field.subfields().len(), 3);
///
/// assert_eq!(field.remove_subfields("t"), 2);
///
/// assert_eq!(field.subfields().len(), 1);
/// ```
pub fn remove_subfields(&mut self, code: &str) -> usize {
let mut removed = 0;
while let Some(index) = self.subfields.iter().position(|s| s.code.eq(code)) {
self.subfields.remove(index);
removed += 1;
}
removed
}
/// # Examples
///
/// ```
/// use marctk::Field;
/// let field = Field::new("505").unwrap();
/// assert!(field.matches_spec("505"));
/// assert!(field.matches_spec("5xx"));
/// assert!(field.matches_spec("50x"));
/// assert!(field.matches_spec("5x5"));
/// assert!(field.matches_spec("x05"));
/// assert!(field.matches_spec("5XX"));
///
/// assert!(!field.matches_spec("6xx"));
/// assert!(!field.matches_spec("LDR"));
/// assert!(!field.matches_spec("invalid spec"));
/// ```
pub fn matches_spec(&self, spec: &str) -> bool {
if spec.len() != 3 {
return false;
};
let potential_matches = self.tag().chars().map(|c| [c, 'x', 'X']);
spec.chars()
.zip(potential_matches)
.all(|(spec_char, matching_chars)| matching_chars.contains(&spec_char))
}
}
/// A MARC record with leader, control fields, and data fields.
#[derive(Debug, Clone, PartialEq)]
pub struct Record {
leader: String,
control_fields: Vec<Controlfield>,
fields: Vec<Field>,
}
impl Default for Record {
fn default() -> Self {
Self::new()
}
}
impl Record {
/// Create a new Record with a default leader and no content.
pub fn new() -> Self {
Record {
leader: DEFAULT_LEADER.to_string(),
control_fields: Vec::new(),
fields: Vec::with_capacity(ESTIMATED_NUMBER_OF_FIELDS),
}
}
/// Get the leader as a string.
pub fn leader(&self) -> &str {
&self.leader
}
/// Apply a leader value.
///
/// Returns Err if the value is not composed of the correct number
/// of bytes.
///
/// # Examples
///
/// ```
/// use marctk::Record;
/// let mut record = Record::default();
/// assert!(record.set_leader("too short").is_err());
/// assert!(record.set_leader("just right ").is_ok());
/// ```
pub fn set_leader(&mut self, leader: impl Into<String>) -> Result<(), String> {
let leader = leader.into();
check_byte_count(&leader, LEADER_SIZE)?;
self.leader = leader;
Ok(())
}
/// Apply a leader value of any length, as a parser found it.
pub(crate) fn set_leader_unchecked(&mut self, leader: impl Into<String>) {
self.leader = leader.into();
}
/// Err unless every value has the byte count the binary format
/// needs: a 24-byte leader, 3-byte tags, and 1-byte indicators and
/// subfield codes. The constructors and setters ensure these, but
/// parsers keep what they find.
pub(crate) fn check_byte_counts(&self) -> Result<(), String> {
check_byte_count(self.leader(), LEADER_SIZE).map_err(|e| format!("Leader: {e}"))?;
for cfield in self.control_fields() {
check_byte_count(cfield.tag(), TAG_SIZE)
.map_err(|e| format!("Control field tag: {e}"))?;
}
for field in self.fields() {
let tag = field.tag();
check_byte_count(tag, TAG_SIZE).map_err(|e| format!("Field tag: {e}"))?;
for ind in [field.ind1(), field.ind2()] {
check_byte_count(ind, CODE_SIZE)
.map_err(|e| format!("Field {tag} indicator: {e}"))?;
}
for sf in field.subfields() {
check_byte_count(sf.code(), CODE_SIZE)
.map_err(|e| format!("Field {tag} subfield code: {e}"))?;
}
}
Ok(())
}
/// Apply a leader value from a set of bytes
///
/// Returns Err if the value is not composed of the correct number
/// of bytes.
///
/// # Examples
///
/// ```
/// use marctk::Record;
/// let mut record = Record::default();
/// assert!(record.set_leader_bytes("too short".as_bytes()).is_err());
/// assert!(record.set_leader_bytes("just right ".as_bytes()).is_ok());
/// ```
pub fn set_leader_bytes(&mut self, bytes: &[u8]) -> Result<(), String> {
let s = std::str::from_utf8(bytes)
.map_err(|e| format!("Leader is not a valid UTF-8 string: {e} bytes={bytes:?}"))?;
self.set_leader(s)
}
/// Get the full list of control fields.
pub fn control_fields(&self) -> &Vec<Controlfield> {
&self.control_fields
}
/// Get the full list of control fields, mutable.
pub fn control_fields_mut(&mut self) -> &mut Vec<Controlfield> {
&mut self.control_fields
}
/// Get the full list of fields.
pub fn fields(&self) -> &Vec<Field> {
&self.fields
}
/// Get the full list of fields, mutable.
pub fn fields_mut(&mut self) -> &mut Vec<Field> {
&mut self.fields
}
/// Return a list of control fields with the provided tag.
pub fn get_control_fields(&self, tag: &str) -> Vec<&Controlfield> {
self.control_fields
.iter()
.filter(|f| f.tag() == tag)
.collect()
}
/// Return a list of fields with the provided tag.
pub fn get_fields(&self, tag: &str) -> Vec<&Field> {
self.fields.iter().filter(|f| f.tag() == tag).collect()
}
/// Return a mutable list of fields with the provided tag.
pub fn get_fields_mut(&mut self, tag: &str) -> Vec<&mut Field> {
self.fields.iter_mut().filter(|f| f.tag() == tag).collect()
}
/// Add a new control field with the provided tag and content and
/// insert it in tag order.
///
/// Controlfields are those with tag 001 .. 009
///
/// Err if the tag is invalid.
///
/// # Examples
///
/// ```
/// use marctk::Record;
/// let mut record = Record::default();
/// assert!(record.add_control_field("011", "foo").is_err());
/// assert!(record.add_control_field("002", "bar").is_ok());
/// assert!(record.add_control_field("001", "bar").is_ok());
///
/// // should be sorted by tag.
/// assert_eq!(record.control_fields()[0].tag(), "001");
/// ```
pub fn add_control_field(&mut self, tag: &str, content: &str) -> Result<(), String> {
self.insert_control_field(Controlfield::new(tag, content)?);
Ok(())
}
/// Insert a [`Controlfield`] in tag order.
pub fn insert_control_field(&mut self, field: Controlfield) {
if let Some(idx) = self
.control_fields()
.iter()
.position(|f| f.tag() > field.tag())
{
self.control_fields_mut().insert(idx, field);
} else {
self.control_fields_mut().push(field);
}
}
/// Insert a [`Field`] in tag order
pub fn insert_data_field(&mut self, field: Field) -> usize {
if let Some(idx) = self.fields().iter().position(|f| f.tag() > field.tag()) {
self.fields_mut().insert(idx, field);
idx
} else {
self.fields_mut().push(field);
0
}
}
/// Create a new Field with the provided tag, insert it into the
/// record in tag order, then return a mut ref to the new field.
///
/// # Examples
///
/// ```
/// use marctk::Record;
/// let mut record = Record::default();
/// assert!(record.add_data_field("245").is_ok());
/// assert!(record.add_data_field("240").is_ok());
/// assert!(record.add_data_field("1234").is_err());
///
/// assert_eq!(record.fields()[0].tag(), "240");
/// ```
pub fn add_data_field(&mut self, tag: impl Into<String>) -> Result<&mut Field, String> {
let pos = self.insert_data_field(Field::new(tag)?);
Ok(self.fields_mut().get_mut(pos).unwrap())
}
/// Returns a list of values for the specified tag and subfield.
///
/// # Examples
///
/// ```
/// use marctk::Record;
/// let mut record = Record::default();
/// let field = record.add_data_field("650").expect("added field");
/// field.add_subfield("a", "foo");
/// field.add_subfield("a", "bar");
///
/// let field = record.add_data_field("650").expect("added field");
/// field.add_subfield("a", "baz");
///
/// let values = record.get_field_values("650", "a");
///
/// assert_eq!(values.len(), 3);
/// assert_eq!(values[1], "bar");
/// ```
pub fn get_field_values(&self, tag: &str, sfcode: &str) -> Vec<&str> {
let mut vec = Vec::new();
for field in self.get_fields(tag) {
for sf in field.get_subfields(sfcode) {
vec.push(sf.content.as_str());
}
}
vec
}
/// Remove all occurrences of control fields with the provided tag.
///
/// # Examples
///
/// ```
/// use marctk::Record;
/// let mut record = Record::default();
/// let _ = record.add_control_field("008", "stuffandsuch").unwrap();
/// let _ = record.add_control_field("008", "morestuffandsuch").unwrap();
///
/// assert_eq!(record.get_control_fields("008").len(), 2);
///
/// record.remove_control_fields("007");
/// assert_eq!(record.get_control_fields("008").len(), 2);
///
/// record.remove_control_fields("008");
/// assert!(record.get_fields("008").is_empty());
/// ```
pub fn remove_control_fields(&mut self, tag: &str) {
while let Some(pos) = self.control_fields.iter().position(|f| f.tag() == tag) {
self.control_fields.remove(pos);
}
}
/// Remove all occurrences of fields with the provided tag.
///
/// # Examples
///
/// ```
/// use marctk::Record;
/// let mut record = Record::default();
/// let field = record.add_data_field("650").unwrap();
/// field.add_subfield("a", "Art");
/// field.add_subfield("a", "Science");
///
/// assert_eq!(record.get_fields("650").len(), 1);
///
/// record.remove_fields("200");
/// assert_eq!(record.get_fields("650").len(), 1);
///
/// record.remove_fields("650");
/// assert!(record.get_fields("650").is_empty());
/// ```
pub fn remove_fields(&mut self, tag: &str) {
while let Some(pos) = self.fields.iter().position(|f| f.tag() == tag) {
self.fields.remove(pos);
}
}
/// Extract MARC fields using a range of tags or a specification
/// inspired by [ruby-marc](https://github.com/ruby-marc/ruby-marc/),
/// [SolrMarc](https://github.com/solrmarc/solrmarc/wiki/Basic-field-based-extraction-specifications),
/// and [traject](https://github.com/traject/traject).
///
/// # Specification syntax
///
/// * A three-character tag will match any field that has that tag, for example `650` would
/// only match fields with the tag `650`.
/// * The letter `x` (or upper case `X`) can be used as a wildcard, for example `2xx` would
/// match any field with a tag that starts with the character `2`.
/// * Multiple specifications can be combined with a `:` between them, for example
/// `4xx:52x:901` would match any field with tag `901` or a tag that begins with
/// `4` or `52`.
///
/// # Examples
///
/// ```
/// use marctk::Record;
/// let record = Record::from_breaker(
/// r#"=600 10$aZhang, Heng, $d 78-139 $v Juvenile literature.
/// =650 \0$aEarthquakes $v Juvenile literature.
/// =955 \0$a1234"#
/// ).unwrap();
///
/// let mut some_fields = record.extract_fields(600..=699);
/// assert_eq!(some_fields.next().unwrap().tag(), "600");
/// assert_eq!(some_fields.next().unwrap().tag(), "650");
/// assert!(some_fields.next().is_none());
///
/// let mut more_fields = record.extract_fields("9xx");
/// assert_eq!(more_fields.next().unwrap().tag(), "955");
/// assert!(more_fields.next().is_none());
///
/// let mut you_can_combine_specs = record.extract_fields("600:9xx");
/// assert_eq!(you_can_combine_specs.next().unwrap().tag(), "600");
/// assert_eq!(you_can_combine_specs.next().unwrap().tag(), "955");
/// assert!(you_can_combine_specs.next().is_none());
/// ```
pub fn extract_fields(
&self,
query: impl Into<crate::query::FieldQuery>,
) -> impl Iterator<Item = &Field> {
self.fields().iter().filter(query.into().field_filter)
}
/// Mutable variant of [`extract_fields()`].
///
/// # Examples
///
/// ```
/// use marctk::Record;
/// let mut record = Record::from_breaker(
/// r#"=600 10$aZhang, Heng, $d 78-139 $v Juvenile literature.
/// =650 \0$aEarthquakes $v Juvenile literature.
/// =955 \0$a1234"#
/// ).unwrap();
///
/// for field in record.extract_fields_mut(600..=699) {
/// field.first_subfield_mut("a").unwrap().set_content("HELLOOO");
/// field.add_subfield("x", "X CONTENT");
/// }
///
/// // This is kinda lazy, but you get the idea.
/// assert!(record.to_breaker().contains("$xX CONTENT"));
/// ```
pub fn extract_fields_mut(
&mut self,
query: impl Into<crate::query::FieldQueryMut>,
) -> impl Iterator<Item = &mut Field> {
self.fields_mut()
.iter_mut()
.filter(query.into().field_filter)
}
/// Extract only certain desired subfields from fields using a specification
/// inspired by [ruby-marc](https://github.com/ruby-marc/ruby-marc/),
/// [SolrMarc](https://github.com/solrmarc/solrmarc/wiki/Basic-field-based-extraction-specifications),
/// and [traject](https://github.com/traject/traject).
///
/// # Specification syntax
///
/// * A three-character tag will match any field that has that tag, for example `650` would
/// only match fields with the tag `650`.
/// * The letter `x` (or upper case `X`) can be used as a wildcard, for example `2xx` would
/// match any field with a tag that starts with the character `2`.
/// * Tags are optionally followed by indicators in parentheses. For example, `650(00)` would
/// match fields with first and second indicators equal to zero. `650(**)` would match
/// fields with any indicators, which is the same as omitting the indicators from the
/// specification altogether. `650(_0)` and `650( 0)` both match fields with an empty
/// first indicator, although you may find using the `_` version clearer.
/// * Each specification can optionally end with a list of subfield codes. For example,
/// `245abc` would match 245 fields and select only subfields `a`, `b`, and `c`.
/// * Multiple specifications can be combined with a `:` between them, for example
/// `60x(*0)a:650av:653` would select
/// * subfield `a` from any field that begins with `60` and has second indicator `0`, and
/// * subfields `a` and `v` from any field with tag `650`, and
/// * any subfield from any field with tag `653`
///
/// Returns an iterator over fields. You can call the `subfields()` method on the result
/// to iterate through the requested subfields.
///
/// # Examples
///
/// ```
/// use marctk::Record;
/// let record = Record::from_breaker(
/// r#"=600 10$aZhang, Heng, $d 78-139 $v Juvenile literature.
/// =650 \0$aEarthquakes $v Juvenile literature.
/// =955 \0$a1234"#
/// ).unwrap();
///
/// let fields = record.extract_partial_fields("600a");
/// assert_eq!(fields.len(), 1);
///
/// let field = fields.first().unwrap();
/// assert_eq!(field.tag(), "600");
/// assert_eq!(field.subfields().len(), 1);
/// assert_eq!(field.subfields()[0].code(), "a");
/// ```
///
/// An example with indicators specified:
///
/// ```
/// use marctk::Record;
/// let record = Record::from_breaker(
/// r#"=650 \0$aEarthquakes $v Juvenile literature.
///=650 \0$aEarthquake damage $v Juvenile literature.
///=650 \4$aNon-LCSH term"#
/// ).unwrap();
///
/// let fields = record.extract_partial_fields("650(*0)a");
/// let terms: Vec<_> = fields.into_iter().map(|f|f.first_subfield("a").unwrap().content().to_string()).collect();
/// assert_eq!(terms, vec!["Earthquakes ", "Earthquake damage "]);
/// ```
///
/// An example with multiple specifications (which means more potential
/// matching fields and subfields):
///
/// ```
/// use marctk::Record;
/// let record = Record::from_breaker(
/// r#"=600 10$aZhang, Heng, $d 78-139 $v Juvenile literature.
///=650 \0$aAmusement parks $vComic books, strips, etc.
///=655 \7$aHorror comics. $2lcgft
///=655 \7$aGraphic novels. $2lcgft"#
/// ).unwrap();
///
/// let genre_query = "600(*0)vx:650(*0)vx:655(*0)avx:655(*7)avx";
/// let fields = record.extract_partial_fields(genre_query);
/// let matching_subfields = fields.iter().fold(Vec::new(), |mut accumulator, field| {
/// accumulator.extend(field.subfields());
/// accumulator
/// });
/// let terms: Vec<&str> = matching_subfields.iter().map(|sf| sf.content()).collect();
/// assert_eq!(
/// terms,
/// vec![" Juvenile literature.", "Comic books, strips, etc.", "Horror comics. ", "Graphic novels. "]
/// );
/// ```
pub fn extract_partial_fields(&self, query: &str) -> Vec<Field> {
let specs: Vec<ComplexSpecification> = query
.split(':')
.filter_map(|spec| ComplexSpecification::try_from(spec).ok())
.collect();
let matching_fields = self
.fields()
.iter()
.filter(|f| specs.iter().any(|spec| spec.matches_field(f)));
matching_fields
.map(|field| {
let mut new_field = field.clone();
new_field
.subfields_mut()
.retain(|sf| specs.iter().any(|spec| spec.subfield_filter()(sf, field)));
new_field
})
.collect()
}
/// Extract only certain desired subfields from fields using a specification.
/// See [`extract_partial_fields`] for the details of the specification syntax.
///
/// # Examples
///
/// ```
/// use marctk::Record;
/// let record = Record::from_breaker(
/// r#"=600 10$aZhang, Heng, $d 78-139 $v Juvenile literature.
///=650 \0$aAmusement parks $vComic books, strips, etc.
///=655 \7$aHorror comics. $2lcgft
///=655 \7$aGraphic novels. $2lcgft"#
/// ).unwrap();
///
/// let genre_query = "600(*0)vx:650(*0)vx:655(*0)avx:655(*7)avx";
/// let values = record.extract_values(genre_query);
/// assert_eq!(
/// values,
/// vec![" Juvenile literature.", "Comic books, strips, etc.", "Horror comics. ", "Graphic novels. "]
/// );
/// ```
///
/// [`extract_partial_fields`]: crate::Record::extract_partial_fields
pub fn extract_values<'a>(&'a self, query: &str) -> Vec<&'a str> {
let specs: Vec<ComplexSpecification> = query
.split(':')
.filter_map(|spec| ComplexSpecification::try_from(spec).ok())
.collect();
let matching_fields = self
.fields()
.iter()
.filter(|f| specs.iter().any(|spec| spec.matches_field(f)));
matching_fields.fold(Vec::new(), |mut accumulator, field| {
let matching_subfield_values = field
.subfields()
.iter()
.filter(|sf| specs.iter().any(|spec| spec.subfield_filter()(sf, field)))
.map(|sf| sf.content());
accumulator.extend(matching_subfield_values);
accumulator
})
}
}
#[cfg(feature = "marc21_bibliographic")]
mod marc21_bibliographic;