mrrc 0.7.6

A Rust library for reading, writing, and manipulating MARC bibliographic records in ISO 2709 binary format
Documentation
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
//! MARC Holdings Record structures and utilities.
//!
//! Holdings records describe the specific holdings information for bibliographic items,
//! including location, call numbers, and enumeration/chronology information for serials.
//! They are linked to bibliographic records but maintain separate MARC records.

use crate::leader::Leader;
use crate::marc_record::MarcRecord;
use crate::record::Field;
use crate::record_helpers::control_field_char_at;
use indexmap::IndexMap;
use serde::{Deserialize, Serialize};

/// A MARC Holdings record (Type x/y/v/u, Leader/06)
///
/// Fields are stored in insertion order using `IndexMap`, preserving the order
/// in which fields were added to the record.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct HoldingsRecord {
    /// Record leader (24 bytes)
    pub leader: Leader,
    /// Control fields (000-009) - preserves insertion order
    /// Multiple values per tag are supported (e.g., repeated 006/007 fields)
    pub control_fields: IndexMap<String, Vec<String>>,
    /// Variable fields (010+) - unified storage, preserves insertion order
    pub fields: IndexMap<String, Vec<Field>>,
}

/// Type of holdings record (Leader/06)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum HoldingsType {
    /// x - Single-part item holdings (monographs)
    SinglePartItem,
    /// y - Serial item holdings
    SerialItem,
    /// v - Multipart item holdings (sets and multivolume monographs)
    MultipartItem,
    /// u - Unknown
    Unknown,
}

/// Acquisition status (008/06)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum AcquisitionStatus {
    /// 0 - Other
    Other,
    /// 1 - Received and complete
    ReceivedAndComplete,
    /// 2 - On order
    OnOrder,
    /// 3 - Received and incomplete
    ReceivedAndIncomplete,
}

/// Method of acquisition (008/07)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum MethodOfAcquisition {
    /// u - Unknown
    Unknown,
    /// f - Free
    Free,
    /// g - Gift
    Gift,
    /// l - Legal deposit
    LegalDeposit,
    /// m - Membership
    Membership,
    /// p - Purchase
    Purchase,
    /// e - Exchange
    Exchange,
}

/// Completeness of holdings (008/16)
#[derive(Debug, Clone, Copy, PartialEq, Eq, Serialize, Deserialize)]
pub enum Completeness {
    /// 1 - Complete
    Complete,
    /// 2 - Incomplete
    Incomplete,
    /// 3 - Scattered
    Scattered,
    /// 4 - Not applicable
    NotApplicable,
}

impl HoldingsRecord {
    /// Create a new holdings record with the given leader
    #[must_use]
    pub fn new(leader: Leader) -> Self {
        HoldingsRecord {
            leader,
            control_fields: IndexMap::new(),
            fields: IndexMap::new(),
        }
    }

    /// Create a builder for fluently constructing holdings records
    #[must_use]
    pub fn builder(leader: Leader) -> HoldingsRecordBuilder {
        HoldingsRecordBuilder {
            record: HoldingsRecord::new(leader),
        }
    }

    /// Add a control field
    pub fn add_control_field(&mut self, tag: String, value: String) {
        self.control_fields.entry(tag).or_default().push(value);
    }

    /// Get the first control field value for a tag
    #[must_use]
    pub fn get_control_field(&self, tag: &str) -> Option<&str> {
        self.control_fields
            .get(tag)
            .and_then(|v| v.first())
            .map(String::as_str)
    }

    /// Add a location field (852)
    pub fn add_location(&mut self, field: Field) {
        self.fields
            .entry("852".to_string())
            .or_default()
            .push(field);
    }

    /// Get location fields (852)
    #[must_use]
    pub fn locations(&self) -> &[Field] {
        self.fields.get("852").map_or(&[], Vec::as_slice)
    }

    /// Add a captions and pattern field for basic units (853)
    pub fn add_captions_basic(&mut self, field: Field) {
        self.fields
            .entry("853".to_string())
            .or_default()
            .push(field);
    }

    /// Get captions and pattern fields for basic units (853)
    #[must_use]
    pub fn captions_basic(&self) -> &[Field] {
        self.fields.get("853").map_or(&[], Vec::as_slice)
    }

    /// Add a captions and pattern field for supplements (854)
    pub fn add_captions_supplements(&mut self, field: Field) {
        self.fields
            .entry("854".to_string())
            .or_default()
            .push(field);
    }

    /// Get captions and pattern fields for supplements (854)
    #[must_use]
    pub fn captions_supplements(&self) -> &[Field] {
        self.fields.get("854").map_or(&[], Vec::as_slice)
    }

    /// Add a captions and pattern field for indexes (855)
    pub fn add_captions_indexes(&mut self, field: Field) {
        self.fields
            .entry("855".to_string())
            .or_default()
            .push(field);
    }

    /// Get captions and pattern fields for indexes (855)
    #[must_use]
    pub fn captions_indexes(&self) -> &[Field] {
        self.fields.get("855").map_or(&[], Vec::as_slice)
    }

    /// Add an enumeration and chronology field for basic units (863)
    pub fn add_enumeration_basic(&mut self, field: Field) {
        self.fields
            .entry("863".to_string())
            .or_default()
            .push(field);
    }

    /// Get enumeration and chronology fields for basic units (863)
    #[must_use]
    pub fn enumeration_basic(&self) -> &[Field] {
        self.fields.get("863").map_or(&[], Vec::as_slice)
    }

    /// Add an enumeration and chronology field for supplements (864)
    pub fn add_enumeration_supplements(&mut self, field: Field) {
        self.fields
            .entry("864".to_string())
            .or_default()
            .push(field);
    }

    /// Get enumeration and chronology fields for supplements (864)
    #[must_use]
    pub fn enumeration_supplements(&self) -> &[Field] {
        self.fields.get("864").map_or(&[], Vec::as_slice)
    }

    /// Add an enumeration and chronology field for indexes (865)
    pub fn add_enumeration_indexes(&mut self, field: Field) {
        self.fields
            .entry("865".to_string())
            .or_default()
            .push(field);
    }

    /// Get enumeration and chronology fields for indexes (865)
    #[must_use]
    pub fn enumeration_indexes(&self) -> &[Field] {
        self.fields.get("865").map_or(&[], Vec::as_slice)
    }

    /// Add a textual holdings field for basic units (866)
    pub fn add_textual_holdings_basic(&mut self, field: Field) {
        self.fields
            .entry("866".to_string())
            .or_default()
            .push(field);
    }

    /// Get textual holdings fields for basic units (866)
    #[must_use]
    pub fn textual_holdings_basic(&self) -> &[Field] {
        self.fields.get("866").map_or(&[], Vec::as_slice)
    }

    /// Add a textual holdings field for supplements (867)
    pub fn add_textual_holdings_supplements(&mut self, field: Field) {
        self.fields
            .entry("867".to_string())
            .or_default()
            .push(field);
    }

    /// Get textual holdings fields for supplements (867)
    #[must_use]
    pub fn textual_holdings_supplements(&self) -> &[Field] {
        self.fields.get("867").map_or(&[], Vec::as_slice)
    }

    /// Add a textual holdings field for indexes (868)
    pub fn add_textual_holdings_indexes(&mut self, field: Field) {
        self.fields
            .entry("868".to_string())
            .or_default()
            .push(field);
    }

    /// Get textual holdings fields for indexes (868)
    #[must_use]
    pub fn textual_holdings_indexes(&self) -> &[Field] {
        self.fields.get("868").map_or(&[], Vec::as_slice)
    }

    /// Add an item information field (876-878)
    pub fn add_item_information(&mut self, field: Field) {
        self.fields
            .entry(field.tag.clone())
            .or_default()
            .push(field);
    }

    /// Get item information fields by tag
    #[must_use]
    pub fn get_item_information(&self, tag: &str) -> Option<&[Field]> {
        self.fields.get(tag).map(Vec::as_slice)
    }

    /// Add a field
    pub fn add_field(&mut self, field: Field) {
        self.fields
            .entry(field.tag.clone())
            .or_default()
            .push(field);
    }

    /// Get fields by tag
    #[must_use]
    pub fn get_fields(&self, tag: &str) -> Option<&[Field]> {
        self.fields.get(tag).map(Vec::as_slice)
    }

    /// Get holdings type from leader
    #[must_use]
    pub fn holdings_type(&self) -> HoldingsType {
        match self.leader.record_type {
            'x' => HoldingsType::SinglePartItem,
            'y' => HoldingsType::SerialItem,
            'v' => HoldingsType::MultipartItem,
            _ => HoldingsType::Unknown,
        }
    }

    /// Get acquisition status from 008/06
    #[must_use]
    pub fn acquisition_status(&self) -> Option<AcquisitionStatus> {
        match control_field_char_at(self, "008", 6)? {
            '0' => Some(AcquisitionStatus::Other),
            '1' => Some(AcquisitionStatus::ReceivedAndComplete),
            '2' => Some(AcquisitionStatus::OnOrder),
            '3' => Some(AcquisitionStatus::ReceivedAndIncomplete),
            _ => None,
        }
    }

    /// Get method of acquisition from 008/07
    #[must_use]
    pub fn method_of_acquisition(&self) -> Option<MethodOfAcquisition> {
        match control_field_char_at(self, "008", 7)? {
            'u' => Some(MethodOfAcquisition::Unknown),
            'f' => Some(MethodOfAcquisition::Free),
            'g' => Some(MethodOfAcquisition::Gift),
            'l' => Some(MethodOfAcquisition::LegalDeposit),
            'm' => Some(MethodOfAcquisition::Membership),
            'p' => Some(MethodOfAcquisition::Purchase),
            'e' => Some(MethodOfAcquisition::Exchange),
            _ => None,
        }
    }

    /// Get completeness of holdings from 008/16
    #[must_use]
    pub fn completeness(&self) -> Option<Completeness> {
        match control_field_char_at(self, "008", 16)? {
            '1' => Some(Completeness::Complete),
            '2' => Some(Completeness::Incomplete),
            '3' => Some(Completeness::Scattered),
            '4' => Some(Completeness::NotApplicable),
            _ => None,
        }
    }

    /// Check if this is a serial holdings record
    #[must_use]
    pub fn is_serial(&self) -> bool {
        self.holdings_type() == HoldingsType::SerialItem
    }

    /// Check if this is a multipart item holdings record
    #[must_use]
    pub fn is_multipart(&self) -> bool {
        self.holdings_type() == HoldingsType::MultipartItem
    }
}

impl MarcRecord for HoldingsRecord {
    fn leader(&self) -> &Leader {
        &self.leader
    }

    fn leader_mut(&mut self) -> &mut Leader {
        &mut self.leader
    }

    fn add_control_field(&mut self, tag: impl Into<String>, value: impl Into<String>) {
        self.control_fields
            .entry(tag.into())
            .or_default()
            .push(value.into());
    }

    fn get_control_field(&self, tag: &str) -> Option<&str> {
        self.control_fields
            .get(tag)
            .and_then(|v| v.first())
            .map(String::as_str)
    }

    fn control_fields_iter(&self) -> Box<dyn Iterator<Item = (&str, &str)> + '_> {
        Box::new(self.control_fields.iter().flat_map(|(tag, values)| {
            values
                .iter()
                .map(move |value| (tag.as_str(), value.as_str()))
        }))
    }

    fn get_fields(&self, tag: &str) -> Option<&[Field]> {
        self.fields.get(tag).map(std::vec::Vec::as_slice)
    }

    fn get_field(&self, tag: &str) -> Option<&Field> {
        self.fields.get(tag).and_then(|v| v.first())
    }
}

/// Builder for fluently constructing holdings records
#[derive(Debug)]
pub struct HoldingsRecordBuilder {
    record: HoldingsRecord,
}

impl HoldingsRecordBuilder {
    /// Add a control field
    #[must_use]
    pub fn control_field(mut self, tag: String, value: String) -> Self {
        self.record.add_control_field(tag, value);
        self
    }

    /// Add a location field (852)
    #[must_use]
    pub fn location(mut self, field: Field) -> Self {
        self.record.add_location(field);
        self
    }

    /// Add a captions and pattern field for basic units (853)
    #[must_use]
    pub fn captions_basic(mut self, field: Field) -> Self {
        self.record.add_captions_basic(field);
        self
    }

    /// Add a captions and pattern field for supplements (854)
    #[must_use]
    pub fn captions_supplements(mut self, field: Field) -> Self {
        self.record.add_captions_supplements(field);
        self
    }

    /// Add a captions and pattern field for indexes (855)
    #[must_use]
    pub fn captions_indexes(mut self, field: Field) -> Self {
        self.record.add_captions_indexes(field);
        self
    }

    /// Add an enumeration and chronology field for basic units (863)
    #[must_use]
    pub fn enumeration_basic(mut self, field: Field) -> Self {
        self.record.add_enumeration_basic(field);
        self
    }

    /// Add an enumeration and chronology field for supplements (864)
    #[must_use]
    pub fn enumeration_supplements(mut self, field: Field) -> Self {
        self.record.add_enumeration_supplements(field);
        self
    }

    /// Add an enumeration and chronology field for indexes (865)
    #[must_use]
    pub fn enumeration_indexes(mut self, field: Field) -> Self {
        self.record.add_enumeration_indexes(field);
        self
    }

    /// Add a textual holdings field for basic units (866)
    #[must_use]
    pub fn textual_holdings_basic(mut self, field: Field) -> Self {
        self.record.add_textual_holdings_basic(field);
        self
    }

    /// Add a textual holdings field for supplements (867)
    #[must_use]
    pub fn textual_holdings_supplements(mut self, field: Field) -> Self {
        self.record.add_textual_holdings_supplements(field);
        self
    }

    /// Add a textual holdings field for indexes (868)
    #[must_use]
    pub fn textual_holdings_indexes(mut self, field: Field) -> Self {
        self.record.add_textual_holdings_indexes(field);
        self
    }

    /// Add an item information field
    #[must_use]
    pub fn item_information(mut self, field: Field) -> Self {
        self.record.add_item_information(field);
        self
    }

    /// Add a field to `other_fields`
    #[must_use]
    pub fn add_field(mut self, field: Field) -> Self {
        self.record.add_field(field);
        self
    }

    /// Build the holdings record
    #[must_use]
    pub fn build(self) -> HoldingsRecord {
        self.record
    }
}

#[cfg(test)]
mod tests {
    use super::*;
    use crate::record::Subfield;

    fn create_test_leader() -> Leader {
        Leader {
            record_length: 1024,
            record_status: 'n',
            record_type: 'y',
            bibliographic_level: '|',
            control_record_type: ' ',
            character_coding: ' ',
            indicator_count: 2,
            subfield_code_count: 2,
            data_base_address: 300,
            encoding_level: '1',
            cataloging_form: 'a',
            multipart_level: ' ',
            reserved: "4500".to_string(),
        }
    }

    #[test]
    fn test_create_holdings_record() {
        let leader = create_test_leader();
        let record = HoldingsRecord::new(leader);
        assert!(record.locations().is_empty());
        assert!(record.textual_holdings_basic().is_empty());
    }

    #[test]
    fn test_holdings_record_builder() {
        let leader = create_test_leader();
        let record = HoldingsRecord::builder(leader)
            .control_field(
                "008".to_string(),
                "000101n| a azannaabn          |a aaa      ".to_string(),
            )
            .build();

        assert_eq!(
            record.get_control_field("008"),
            Some("000101n| a azannaabn          |a aaa      ")
        );
    }

    #[test]
    fn test_holdings_type_detection() {
        // Serial holdings
        let mut leader = create_test_leader();
        leader.record_type = 'y';
        let record = HoldingsRecord::new(leader.clone());
        assert_eq!(record.holdings_type(), HoldingsType::SerialItem);
        assert!(record.is_serial());

        // Multipart item holdings
        leader.record_type = 'v';
        let record = HoldingsRecord::new(leader.clone());
        assert_eq!(record.holdings_type(), HoldingsType::MultipartItem);
        assert!(record.is_multipart());

        // Single-part item holdings
        leader.record_type = 'x';
        let record = HoldingsRecord::new(leader);
        assert_eq!(record.holdings_type(), HoldingsType::SinglePartItem);
    }

    #[test]
    fn test_acquisition_status_parsing() {
        let leader = create_test_leader();

        // Received and complete (position 6 = '1')
        let record = HoldingsRecord::builder(leader.clone())
            .control_field(
                "008".to_string(),
                "0001011 a azannaabn          |a aaa      ".to_string(),
            )
            .build();
        assert_eq!(
            record.acquisition_status(),
            Some(AcquisitionStatus::ReceivedAndComplete)
        );

        // On order (position 6 = '2')
        let record = HoldingsRecord::builder(leader.clone())
            .control_field(
                "008".to_string(),
                "0001012 a azannaabn          |a aaa      ".to_string(),
            )
            .build();
        assert_eq!(
            record.acquisition_status(),
            Some(AcquisitionStatus::OnOrder)
        );

        // Received and incomplete (position 6 = '3')
        let record = HoldingsRecord::builder(leader)
            .control_field(
                "008".to_string(),
                "0001013 a azannaabn          |a aaa      ".to_string(),
            )
            .build();
        assert_eq!(
            record.acquisition_status(),
            Some(AcquisitionStatus::ReceivedAndIncomplete)
        );
    }

    #[test]
    fn test_method_of_acquisition_parsing() {
        let leader = create_test_leader();

        // Purchase (position 7 = 'p')
        let record = HoldingsRecord::builder(leader.clone())
            .control_field(
                "008".to_string(),
                "00010n1pzannaabn          |a aaa      ".to_string(),
            )
            .build();
        assert_eq!(
            record.method_of_acquisition(),
            Some(MethodOfAcquisition::Purchase)
        );

        // Gift (position 7 = 'g')
        let record = HoldingsRecord::builder(leader)
            .control_field(
                "008".to_string(),
                "00010n1gzannaabn          |a aaa      ".to_string(),
            )
            .build();
        assert_eq!(
            record.method_of_acquisition(),
            Some(MethodOfAcquisition::Gift)
        );
    }

    #[test]
    fn test_completeness_parsing() {
        let leader = create_test_leader();

        // Create a 40-character 008 field with position 16 = '1' (Complete)
        let field_008_str = "0000001pzzzzzzzz1                       ".to_string();
        let record = HoldingsRecord::builder(leader)
            .control_field("008".to_string(), field_008_str)
            .build();
        assert_eq!(record.completeness(), Some(Completeness::Complete));
    }

    #[test]
    fn test_add_location() {
        let leader = create_test_leader();
        let location = Field {
            tag: "852".to_string(),
            indicator1: ' ',
            indicator2: '1',
            subfields: smallvec::smallvec![Subfield {
                code: 'b',
                value: "Main Library".to_string(),
            }],
        };

        let record = HoldingsRecord::builder(leader).location(location).build();

        assert_eq!(record.locations().len(), 1);
        assert_eq!(record.locations()[0].tag, "852");
    }

    #[test]
    fn test_add_textual_holdings() {
        let leader = create_test_leader();
        let holdings = Field {
            tag: "866".to_string(),
            indicator1: '4',
            indicator2: '1',
            subfields: smallvec::smallvec![Subfield {
                code: 'a',
                value: "v.1 (1990)-v.10 (2000)".to_string(),
            }],
        };

        let record = HoldingsRecord::builder(leader)
            .textual_holdings_basic(holdings)
            .build();

        assert_eq!(record.textual_holdings_basic().len(), 1);
        assert_eq!(
            record.textual_holdings_basic()[0].get_subfield('a'),
            Some("v.1 (1990)-v.10 (2000)")
        );
    }

    #[test]
    fn test_control_field_operations() {
        let leader = create_test_leader();
        let mut record = HoldingsRecord::new(leader);

        record.add_control_field("001".to_string(), "ocm00123456".to_string());
        record.add_control_field("005".to_string(), "20250101".to_string());

        assert_eq!(record.get_control_field("001"), Some("ocm00123456"));
        assert_eq!(record.get_control_field("005"), Some("20250101"));
    }

    #[test]
    fn test_acquisition_status_none() {
        let leader = create_test_leader();

        // Missing 008 field
        let record = HoldingsRecord::new(leader.clone());
        assert_eq!(record.acquisition_status(), None);

        // 008 field too short
        let record = HoldingsRecord::builder(leader)
            .control_field("008".to_string(), "000".to_string())
            .build();
        assert_eq!(record.acquisition_status(), None);
    }

    #[test]
    fn test_method_of_acquisition_all_values() {
        let leader = create_test_leader();

        // Unknown
        let record = HoldingsRecord::builder(leader.clone())
            .control_field(
                "008".to_string(),
                "00010n1uzannaabn          |a aaa      ".to_string(),
            )
            .build();
        assert_eq!(
            record.method_of_acquisition(),
            Some(MethodOfAcquisition::Unknown)
        );

        // Free
        let record = HoldingsRecord::builder(leader.clone())
            .control_field(
                "008".to_string(),
                "00010n1fzannaabn          |a aaa      ".to_string(),
            )
            .build();
        assert_eq!(
            record.method_of_acquisition(),
            Some(MethodOfAcquisition::Free)
        );

        // Legal deposit
        let record = HoldingsRecord::builder(leader.clone())
            .control_field(
                "008".to_string(),
                "00010n1lzannaabn          |a aaa      ".to_string(),
            )
            .build();
        assert_eq!(
            record.method_of_acquisition(),
            Some(MethodOfAcquisition::LegalDeposit)
        );

        // Membership
        let record = HoldingsRecord::builder(leader.clone())
            .control_field(
                "008".to_string(),
                "00010n1mzannaabn          |a aaa      ".to_string(),
            )
            .build();
        assert_eq!(
            record.method_of_acquisition(),
            Some(MethodOfAcquisition::Membership)
        );

        // Exchange
        let record = HoldingsRecord::builder(leader)
            .control_field(
                "008".to_string(),
                "00010n1ezannaabn          |a aaa      ".to_string(),
            )
            .build();
        assert_eq!(
            record.method_of_acquisition(),
            Some(MethodOfAcquisition::Exchange)
        );
    }

    #[test]
    fn test_completeness_all_values() {
        let leader = create_test_leader();

        // Incomplete
        let record = HoldingsRecord::builder(leader.clone())
            .control_field(
                "008".to_string(),
                "0000001pzzzzzzzz2                       ".to_string(),
            )
            .build();
        assert_eq!(record.completeness(), Some(Completeness::Incomplete));

        // Scattered
        let record = HoldingsRecord::builder(leader.clone())
            .control_field(
                "008".to_string(),
                "0000001pzzzzzzzz3                       ".to_string(),
            )
            .build();
        assert_eq!(record.completeness(), Some(Completeness::Scattered));

        // Not applicable
        let record = HoldingsRecord::builder(leader)
            .control_field(
                "008".to_string(),
                "0000001pzzzzzzzz4                       ".to_string(),
            )
            .build();
        assert_eq!(record.completeness(), Some(Completeness::NotApplicable));
    }

    #[test]
    fn test_captions_and_enumeration_fields() {
        let leader = create_test_leader();

        let caption_field = Field {
            tag: "853".to_string(),
            indicator1: ' ',
            indicator2: '1',
            subfields: smallvec::smallvec![Subfield {
                code: 'a',
                value: "v.".to_string(),
            }],
        };

        let enum_field = Field {
            tag: "863".to_string(),
            indicator1: ' ',
            indicator2: '1',
            subfields: smallvec::smallvec![Subfield {
                code: 'a',
                value: "v.1".to_string(),
            }],
        };

        let record = HoldingsRecord::builder(leader)
            .captions_basic(caption_field)
            .enumeration_basic(enum_field)
            .build();

        assert_eq!(record.captions_basic().len(), 1);
        assert_eq!(record.enumeration_basic().len(), 1);
        assert_eq!(record.captions_basic()[0].tag, "853");
        assert_eq!(record.enumeration_basic()[0].tag, "863");
    }

    #[test]
    fn test_item_information_fields() {
        let leader = create_test_leader();

        let item_876 = Field {
            tag: "876".to_string(),
            indicator1: ' ',
            indicator2: ' ',
            subfields: smallvec::smallvec![Subfield {
                code: 'p',
                value: "12345".to_string(),
            }],
        };

        let item_877 = Field {
            tag: "877".to_string(),
            indicator1: ' ',
            indicator2: ' ',
            subfields: smallvec::smallvec![Subfield {
                code: 'p',
                value: "12346".to_string(),
            }],
        };

        let record = HoldingsRecord::builder(leader)
            .item_information(item_876)
            .item_information(item_877)
            .build();

        assert!(record.get_item_information("876").is_some());
        assert!(record.get_item_information("877").is_some());
        assert_eq!(record.get_item_information("876").unwrap().len(), 1);
    }

    #[test]
    fn test_other_fields() {
        let leader = create_test_leader();

        let field_500 = Field {
            tag: "500".to_string(),
            indicator1: ' ',
            indicator2: ' ',
            subfields: smallvec::smallvec![Subfield {
                code: 'a',
                value: "General note".to_string(),
            }],
        };

        let record = HoldingsRecord::builder(leader).add_field(field_500).build();

        assert!(record.get_fields("500").is_some());
        assert_eq!(record.get_fields("500").unwrap()[0].tag, "500");
    }

    #[test]
    fn test_single_part_item_type() {
        let mut leader = create_test_leader();
        leader.record_type = 'x';
        let record = HoldingsRecord::new(leader);
        assert_eq!(record.holdings_type(), HoldingsType::SinglePartItem);
        assert!(!record.is_serial());
        assert!(!record.is_multipart());
    }

    #[test]
    fn test_unknown_holdings_type() {
        let mut leader = create_test_leader();
        leader.record_type = 'z';
        let record = HoldingsRecord::new(leader);
        assert_eq!(record.holdings_type(), HoldingsType::Unknown);
    }
}