file-identify 0.5.0

File type identification (tags from names, extensions, shebangs and content) - a faithful Rust port of Python identify, as used by pre-commit
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
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
//! Tags, tag sets and identify's tag-group constants.
//!
//! A [`Tag`] is one of the [`TAG_COUNT`] names in
//! [`ALL_TAGS`]; a [`TagSet`] is a `Copy` bitset over them. Neither allocates,
//! and every set operation is a handful of word operations — cheap enough to
//! evaluate pre-commit's `types` / `types_or` / `exclude_types` filters for
//! every file of a large repository.
//!
//! ```
//! use file_identify::{Tag, TagSet};
//!
//! let tags: TagSet = "file, non-executable, python, text".parse()?;
//! assert!(tags.contains(Tag::TEXT));
//! assert!(tags.contains("python"));
//!
//! let types = TagSet::from_names(["python"])?;
//! let exclude = TagSet::from_names(["binary"])?;
//! assert!(tags.matches_types(&types, &TagSet::EMPTY, &exclude));
//!
//! // Iteration (and `Display`) follow tag-name order.
//! assert_eq!(tags.to_string(), "file, non-executable, python, text");
//! # Ok::<(), file_identify::UnknownTagError>(())
//! ```

use core::fmt;
use core::iter::FusedIterator;
use core::ops::{
    BitAnd, BitAndAssign, BitOr, BitOrAssign, BitXor, BitXorAssign, Not, Sub, SubAssign,
};
use core::str::FromStr;

use crate::generated::{TAG_COUNT, TAG_INDEX, WORDS};

pub use crate::generated::ALL_TAGS;
pub use crate::meta::FileKind;

/// Name of the `directory` tag.
pub const DIRECTORY: &str = "directory";
/// Name of the `symlink` tag.
pub const SYMLINK: &str = "symlink";
/// Name of the `socket` tag.
pub const SOCKET: &str = "socket";
/// Name of the `file` tag.
pub const FILE: &str = "file";
/// Name of the `executable` tag.
pub const EXECUTABLE: &str = "executable";
/// Name of the `non-executable` tag.
pub const NON_EXECUTABLE: &str = "non-executable";
/// Name of the `text` tag.
pub const TEXT: &str = "text";
/// Name of the `binary` tag.
pub const BINARY: &str = "binary";

/// `identify.TYPE_TAGS`: `directory`, `file`, `symlink`, `socket`.
pub const TYPE_TAGS: TagSet = TagSet::EMPTY
    .with(Tag::DIRECTORY)
    .with(Tag::FILE)
    .with(Tag::SYMLINK)
    .with(Tag::SOCKET);

/// `identify.MODE_TAGS`: `executable`, `non-executable`.
pub const MODE_TAGS: TagSet = TagSet::EMPTY
    .with(Tag::EXECUTABLE)
    .with(Tag::NON_EXECUTABLE);

/// `identify.ENCODING_TAGS`: `binary`, `text`.
pub const ENCODING_TAGS: TagSet = TagSet::EMPTY.with(Tag::BINARY).with(Tag::TEXT);

/// Whether `tag` is one of [`TYPE_TAGS`].
#[must_use]
pub fn is_type_tag(tag: &str) -> bool {
    matches!(tag, DIRECTORY | FILE | SYMLINK | SOCKET)
}

/// Whether `tag` is one of [`MODE_TAGS`].
#[must_use]
pub fn is_mode_tag(tag: &str) -> bool {
    matches!(tag, EXECUTABLE | NON_EXECUTABLE)
}

/// Whether `tag` is one of [`ENCODING_TAGS`].
#[must_use]
pub fn is_encoding_tag(tag: &str) -> bool {
    matches!(tag, BINARY | TEXT)
}

/// One identify tag, such as `python`, `text` or `executable`.
///
/// A `Tag` is a 2-byte handle: its [`id`](Tag::id) is the tag's position in
/// the sorted [`ALL_TAGS`] array, so comparing tags compares their names.
/// Only the eight file-system tags have named constants ([`Tag::FILE`],
/// [`Tag::TEXT`], …); look any other tag up by name:
///
/// ```
/// use file_identify::Tag;
///
/// let python: Tag = "python".parse()?;
/// assert_eq!(python.as_str(), "python");
/// assert_eq!(Tag::from_name("python"), Some(python));
/// assert_eq!(Tag::from_name("pythn"), None);
/// assert!(Tag::FILE < Tag::TEXT); // name order
/// # Ok::<(), file_identify::UnknownTagError>(())
/// ```
#[derive(Clone, Copy, PartialEq, Eq, Hash, PartialOrd, Ord)]
pub struct Tag(u16);

impl Tag {
    /// Construct from a generated id. Only used by `generated.rs`, whose ids
    /// are `< TAG_COUNT` by construction.
    pub(crate) const fn from_id_const(id: u16) -> Self {
        Self(id)
    }

    /// The tag with the given [`id`](Tag::id), if `id < TAG_COUNT`.
    #[must_use]
    pub fn from_id(id: usize) -> Option<Self> {
        if id < TAG_COUNT {
            u16::try_from(id).ok().map(Self)
        } else {
            None
        }
    }

    /// Look a tag up by its exact (case-sensitive) name.
    ///
    /// This is a hash lookup with no allocation.
    #[must_use]
    pub fn from_name(name: &str) -> Option<Self> {
        TAG_INDEX
            .find(name.as_bytes(), |i| ALL_TAGS.get(i).copied())
            .and_then(Self::from_id)
    }

    /// The tag's position in [`ALL_TAGS`] (`0..TAG_COUNT`).
    #[must_use]
    pub const fn id(self) -> usize {
        self.0 as usize
    }

    /// The tag's name, e.g. `"python"`.
    #[must_use]
    pub fn as_str(self) -> &'static str {
        ALL_TAGS.get(self.id()).copied().unwrap_or_default()
    }

    /// Whether this is one of [`TYPE_TAGS`].
    #[must_use]
    pub const fn is_type_tag(self) -> bool {
        TYPE_TAGS.contains_tag(self)
    }

    /// Whether this is one of [`MODE_TAGS`].
    #[must_use]
    pub const fn is_mode_tag(self) -> bool {
        MODE_TAGS.contains_tag(self)
    }

    /// Whether this is one of [`ENCODING_TAGS`].
    #[must_use]
    pub const fn is_encoding_tag(self) -> bool {
        ENCODING_TAGS.contains_tag(self)
    }

    /// Iterate over every known tag in name order.
    #[must_use]
    pub const fn all() -> Iter {
        TagSet::ALL.iter()
    }

    /// Word index and bit mask of this tag inside a [`TagSet`].
    const fn bit(self) -> (usize, u64) {
        ((self.0 / 64) as usize, 1u64 << (self.0 % 64))
    }
}

impl fmt::Debug for Tag {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_tuple("Tag").field(&self.as_str()).finish()
    }
}

impl fmt::Display for Tag {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(self.as_str())
    }
}

impl AsRef<str> for Tag {
    fn as_ref(&self) -> &str {
        self.as_str()
    }
}

impl FromStr for Tag {
    type Err = UnknownTagError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::from_name(s).ok_or_else(|| UnknownTagError::new(vec![s.to_owned()]))
    }
}

impl TryFrom<&str> for Tag {
    type Error = UnknownTagError;

    fn try_from(s: &str) -> Result<Self, Self::Error> {
        s.parse()
    }
}

impl PartialEq<str> for Tag {
    fn eq(&self, other: &str) -> bool {
        self.as_str() == other
    }
}

impl PartialEq<&str> for Tag {
    fn eq(&self, other: &&str) -> bool {
        self.as_str() == *other
    }
}

impl PartialEq<Tag> for str {
    fn eq(&self, other: &Tag) -> bool {
        self == other.as_str()
    }
}

impl PartialEq<Tag> for &str {
    fn eq(&self, other: &Tag) -> bool {
        *self == other.as_str()
    }
}

/// Error returned when parsing names that are not in [`ALL_TAGS`].
///
/// The `Display` text is `unknown tag "x"` (or `unknown tags "x", "y"`);
/// callers that need identify/pre-commit's exact wording should build it from
/// [`names`](UnknownTagError::names).
#[derive(Clone, Debug, PartialEq, Eq)]
pub struct UnknownTagError {
    names: Vec<String>,
}

impl UnknownTagError {
    fn new(names: Vec<String>) -> Self {
        Self { names }
    }

    /// The unrecognized names, in input order (never empty).
    #[must_use]
    pub fn names(&self) -> &[String] {
        &self.names
    }
}

impl fmt::Display for UnknownTagError {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.write_str(if self.names.len() == 1 {
            "unknown tag "
        } else {
            "unknown tags "
        })?;
        for (i, name) in self.names.iter().enumerate() {
            if i > 0 {
                f.write_str(", ")?;
            }
            write!(f, "{name:?}")?;
        }
        Ok(())
    }
}

impl std::error::Error for UnknownTagError {}

mod sealed {
    pub trait Sealed {}
    impl Sealed for super::Tag {}
    impl Sealed for &super::Tag {}
    impl Sealed for &str {}
    impl Sealed for String {}
    impl Sealed for &String {}
}

/// A [`Tag`] or a tag name; accepted by [`TagSet::contains`].
///
/// This trait is sealed. Looking up a name costs one hash lookup; unknown
/// names are never contained in any set.
pub trait IntoTag: sealed::Sealed {
    /// The tag this value denotes, if any.
    fn into_tag(self) -> Option<Tag>;
}

impl IntoTag for Tag {
    fn into_tag(self) -> Option<Tag> {
        Some(self)
    }
}

impl IntoTag for &Tag {
    fn into_tag(self) -> Option<Tag> {
        Some(*self)
    }
}

impl IntoTag for &str {
    fn into_tag(self) -> Option<Tag> {
        Tag::from_name(self)
    }
}

impl IntoTag for String {
    fn into_tag(self) -> Option<Tag> {
        Tag::from_name(&self)
    }
}

impl IntoTag for &String {
    fn into_tag(self) -> Option<Tag> {
        Tag::from_name(self)
    }
}

/// A set of [`Tag`]s: a `Copy` bitset with one bit per known tag.
///
/// All operations are allocation-free; the binary operators `|`, `&`, `-`,
/// `^` and `!` (complement relative to all known tags) are available as well
/// as the named methods. Iteration, [`Debug`](fmt::Debug) and
/// [`Display`](fmt::Display) list tags in name order.
#[derive(Clone, Copy, PartialEq, Eq, Hash, Default)]
pub struct TagSet {
    words: [u64; WORDS],
}

// All indexing below is `words[i]` with `i < WORDS`, guarded by the loop
// condition or an explicit check, so it cannot panic. Const fns cannot use
// iterators, hence the manual loops.
#[allow(clippy::indexing_slicing)]
impl TagSet {
    /// The empty set.
    pub const EMPTY: Self = Self { words: [0; WORDS] };

    /// Every known tag (`identify.ALL_TAGS`).
    pub const ALL: Self = {
        let mut words = [0u64; WORDS];
        let mut i = 0;
        while i < WORDS {
            let start = i * 64;
            words[i] = if TAG_COUNT >= start + 64 {
                u64::MAX
            } else if TAG_COUNT > start {
                (1u64 << (TAG_COUNT - start)) - 1
            } else {
                0
            };
            i += 1;
        }
        Self { words }
    };

    /// The empty set (same as [`TagSet::EMPTY`]).
    #[must_use]
    pub const fn new() -> Self {
        Self::EMPTY
    }

    /// Build a set from tag ids; ids `>= TAG_COUNT` are ignored. Used by the
    /// generated tables.
    pub(crate) const fn from_ids(ids: &[u16]) -> Self {
        let mut set = Self::EMPTY;
        let mut i = 0;
        while i < ids.len() {
            if (ids[i] as usize) < TAG_COUNT {
                set = set.with(Tag(ids[i]));
            }
            i += 1;
        }
        set
    }

    /// The set containing only `tag`.
    #[must_use]
    pub const fn from_tag(tag: Tag) -> Self {
        Self::EMPTY.with(tag)
    }

    /// Build a set from tag names, failing with every unknown name.
    ///
    /// # Errors
    ///
    /// Returns [`UnknownTagError`] listing all names not in [`ALL_TAGS`].
    pub fn from_names<I, S>(names: I) -> Result<Self, UnknownTagError>
    where
        I: IntoIterator<Item = S>,
        S: AsRef<str>,
    {
        let mut set = Self::EMPTY;
        let mut unknown = Vec::new();
        for name in names {
            let name = name.as_ref();
            match Tag::from_name(name) {
                Some(tag) => {
                    set.insert(tag);
                }
                None => unknown.push(name.to_owned()),
            }
        }
        if unknown.is_empty() {
            Ok(set)
        } else {
            Err(UnknownTagError::new(unknown))
        }
    }

    /// This set plus `tag`.
    #[must_use]
    pub const fn with(self, tag: Tag) -> Self {
        let (w, bit) = tag.bit();
        let mut words = self.words;
        if w < WORDS {
            words[w] |= bit;
        }
        Self { words }
    }

    /// This set minus `tag`.
    #[must_use]
    pub const fn without(self, tag: Tag) -> Self {
        let (w, bit) = tag.bit();
        let mut words = self.words;
        if w < WORDS {
            words[w] &= !bit;
        }
        Self { words }
    }

    /// Whether `tag` is in the set (`const` version of
    /// [`contains`](TagSet::contains) for [`Tag`] values).
    #[must_use]
    pub const fn contains_tag(&self, tag: Tag) -> bool {
        let (w, bit) = tag.bit();
        w < WORDS && self.words[w] & bit != 0
    }

    /// Whether the set contains `tag`, given as a [`Tag`] or a tag name.
    ///
    /// ```
    /// use file_identify::{Tag, tags_from_filename};
    ///
    /// let tags = tags_from_filename("setup.py");
    /// assert!(tags.contains("python"));
    /// assert!(tags.contains(Tag::TEXT));
    /// assert!(!tags.contains("no-such-tag"));
    /// ```
    #[must_use]
    pub fn contains(&self, tag: impl IntoTag) -> bool {
        tag.into_tag().is_some_and(|t| self.contains_tag(t))
    }

    /// Add `tag`; returns whether it was newly inserted.
    pub fn insert(&mut self, tag: Tag) -> bool {
        let was = self.contains_tag(tag);
        *self = self.with(tag);
        !was
    }

    /// Remove `tag`; returns whether it was present.
    pub fn remove(&mut self, tag: Tag) -> bool {
        let was = self.contains_tag(tag);
        *self = self.without(tag);
        was
    }

    /// Remove every tag.
    pub fn clear(&mut self) {
        *self = Self::EMPTY;
    }

    /// Number of tags in the set.
    #[must_use]
    pub const fn len(&self) -> usize {
        let mut n = 0;
        let mut i = 0;
        while i < WORDS {
            n += self.words[i].count_ones() as usize;
            i += 1;
        }
        n
    }

    /// Whether the set is empty.
    #[must_use]
    pub const fn is_empty(&self) -> bool {
        let mut i = 0;
        while i < WORDS {
            if self.words[i] != 0 {
                return false;
            }
            i += 1;
        }
        true
    }

    /// `self ∪ other`.
    #[must_use]
    pub const fn union(self, other: Self) -> Self {
        let mut words = self.words;
        let mut i = 0;
        while i < WORDS {
            words[i] |= other.words[i];
            i += 1;
        }
        Self { words }
    }

    /// `self ∩ other`.
    #[must_use]
    pub const fn intersection(self, other: Self) -> Self {
        let mut words = self.words;
        let mut i = 0;
        while i < WORDS {
            words[i] &= other.words[i];
            i += 1;
        }
        Self { words }
    }

    /// `self \ other`.
    #[must_use]
    pub const fn difference(self, other: Self) -> Self {
        let mut words = self.words;
        let mut i = 0;
        while i < WORDS {
            words[i] &= !other.words[i];
            i += 1;
        }
        Self { words }
    }

    /// Tags in exactly one of `self` and `other`.
    #[must_use]
    pub const fn symmetric_difference(self, other: Self) -> Self {
        let mut words = self.words;
        let mut i = 0;
        while i < WORDS {
            words[i] ^= other.words[i];
            i += 1;
        }
        Self { words }
    }

    /// Every known tag that is not in `self`.
    #[must_use]
    pub const fn complement(self) -> Self {
        Self::ALL.difference(self)
    }

    /// Whether every tag of `self` is in `other`.
    #[must_use]
    pub const fn is_subset(&self, other: &Self) -> bool {
        let mut i = 0;
        while i < WORDS {
            if self.words[i] & !other.words[i] != 0 {
                return false;
            }
            i += 1;
        }
        true
    }

    /// Whether every tag of `other` is in `self` (Python's `self >= other`).
    #[must_use]
    pub const fn is_superset(&self, other: &Self) -> bool {
        other.is_subset(self)
    }

    /// Whether `self` and `other` share no tag.
    #[must_use]
    pub const fn is_disjoint(&self, other: &Self) -> bool {
        !self.intersects(other)
    }

    /// Whether `self` and `other` share at least one tag.
    #[must_use]
    pub const fn intersects(&self, other: &Self) -> bool {
        let mut i = 0;
        while i < WORDS {
            if self.words[i] & other.words[i] != 0 {
                return true;
            }
            i += 1;
        }
        false
    }

    /// pre-commit's file-type filter (`Classifier.by_types`) applied to the
    /// tags of one file:
    ///
    /// `self ⊇ types && (types_or = ∅ || self ∩ types_or ≠ ∅) && self ∩ exclude_types = ∅`
    ///
    /// ```
    /// use file_identify::TagSet;
    ///
    /// let file: TagSet = "file, non-executable, python, text".parse()?;
    /// let none = TagSet::EMPTY;
    /// let types: TagSet = "file".parse()?;
    /// let types_or: TagSet = "python, pyi".parse()?;
    /// let exclude: TagSet = "binary".parse()?;
    /// assert!(file.matches_types(&types, &types_or, &exclude));
    /// assert!(!file.matches_types(&types, &none, &"python".parse()?));
    /// # Ok::<(), file_identify::UnknownTagError>(())
    /// ```
    #[must_use]
    pub const fn matches_types(&self, types: &Self, types_or: &Self, exclude_types: &Self) -> bool {
        self.is_superset(types)
            && (types_or.is_empty() || self.intersects(types_or))
            && self.is_disjoint(exclude_types)
    }

    /// Whether the set contains `text` or `binary` (identify's invariant for
    /// a classified regular file).
    #[must_use]
    pub const fn has_encoding(&self) -> bool {
        self.intersects(&ENCODING_TAGS)
    }

    /// Iterate over the tags in name order.
    #[must_use]
    pub const fn iter(&self) -> Iter {
        Iter {
            words: self.words,
            front: 0,
            back: WORDS,
        }
    }

    /// Iterate over the tag names in sorted order.
    #[must_use = "iterators are lazy and do nothing unless consumed"]
    pub fn names(&self) -> impl DoubleEndedIterator<Item = &'static str> + ExactSizeIterator {
        self.iter().map(Tag::as_str)
    }

    /// The first tag in name order.
    #[must_use]
    pub fn first(&self) -> Option<Tag> {
        self.iter().next()
    }

    /// The last tag in name order.
    #[must_use]
    pub fn last(&self) -> Option<Tag> {
        self.iter().next_back()
    }
}

impl fmt::Debug for TagSet {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        f.debug_set().entries(self.names()).finish()
    }
}

/// Tag names in name order separated by `", "`, e.g.
/// `file, non-executable, python, text` (the empty set prints nothing).
/// [`FromStr`] accepts the same format.
impl fmt::Display for TagSet {
    fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
        for (i, name) in self.names().enumerate() {
            if i > 0 {
                f.write_str(", ")?;
            }
            f.write_str(name)?;
        }
        Ok(())
    }
}

/// Parse comma-separated tag names (surrounding whitespace is ignored, empty
/// items are skipped), the format produced by [`Display`](fmt::Display).
impl FromStr for TagSet {
    type Err = UnknownTagError;

    fn from_str(s: &str) -> Result<Self, Self::Err> {
        Self::from_names(s.split(',').map(str::trim).filter(|n| !n.is_empty()))
    }
}

impl TryFrom<&[&str]> for TagSet {
    type Error = UnknownTagError;

    fn try_from(names: &[&str]) -> Result<Self, Self::Error> {
        Self::from_names(names)
    }
}

impl From<Tag> for TagSet {
    fn from(tag: Tag) -> Self {
        Self::from_tag(tag)
    }
}

impl<const N: usize> From<[Tag; N]> for TagSet {
    fn from(tags: [Tag; N]) -> Self {
        tags.into_iter().collect()
    }
}

impl FromIterator<Tag> for TagSet {
    fn from_iter<I: IntoIterator<Item = Tag>>(iter: I) -> Self {
        let mut set = Self::EMPTY;
        set.extend(iter);
        set
    }
}

impl Extend<Tag> for TagSet {
    fn extend<I: IntoIterator<Item = Tag>>(&mut self, iter: I) {
        for tag in iter {
            self.insert(tag);
        }
    }
}

impl IntoIterator for TagSet {
    type Item = Tag;
    type IntoIter = Iter;

    fn into_iter(self) -> Iter {
        self.iter()
    }
}

impl IntoIterator for &TagSet {
    type Item = Tag;
    type IntoIter = Iter;

    fn into_iter(self) -> Iter {
        self.iter()
    }
}

macro_rules! set_op {
    ($trait:ident, $method:ident, $assign_trait:ident, $assign_method:ident, $op:ident) => {
        impl $trait for TagSet {
            type Output = TagSet;

            fn $method(self, rhs: TagSet) -> TagSet {
                self.$op(rhs)
            }
        }

        impl $trait<Tag> for TagSet {
            type Output = TagSet;

            fn $method(self, rhs: Tag) -> TagSet {
                self.$op(TagSet::from_tag(rhs))
            }
        }

        impl $assign_trait for TagSet {
            fn $assign_method(&mut self, rhs: TagSet) {
                *self = self.$op(rhs);
            }
        }

        impl $assign_trait<Tag> for TagSet {
            fn $assign_method(&mut self, rhs: Tag) {
                *self = self.$op(TagSet::from_tag(rhs));
            }
        }
    };
}

set_op!(BitOr, bitor, BitOrAssign, bitor_assign, union);
set_op!(BitAnd, bitand, BitAndAssign, bitand_assign, intersection);
set_op!(
    BitXor,
    bitxor,
    BitXorAssign,
    bitxor_assign,
    symmetric_difference
);
set_op!(Sub, sub, SubAssign, sub_assign, difference);

impl Not for TagSet {
    type Output = TagSet;

    fn not(self) -> TagSet {
        self.complement()
    }
}

/// Iterator over the tags of a [`TagSet`] in name order (see
/// [`TagSet::iter`]). Owns a copy of the set.
#[derive(Clone, Debug)]
pub struct Iter {
    words: [u64; WORDS],
    /// Index of the lowest word that may still have bits.
    front: usize,
    /// One past the highest word that may still have bits.
    back: usize,
}

impl Iter {
    fn tag(word: usize, bit: u32) -> Option<Tag> {
        let id = word.checked_mul(64)?.checked_add(bit as usize)?;
        Tag::from_id(id)
    }
}

impl Iterator for Iter {
    type Item = Tag;

    fn next(&mut self) -> Option<Tag> {
        while self.front < self.back {
            let w = self.words.get_mut(self.front)?;
            if *w != 0 {
                let bit = w.trailing_zeros();
                *w &= *w - 1;
                return Self::tag(self.front, bit);
            }
            self.front += 1;
        }
        None
    }

    fn size_hint(&self) -> (usize, Option<usize>) {
        let n = self
            .words
            .get(self.front..self.back)
            .unwrap_or_default()
            .iter()
            .map(|w| w.count_ones() as usize)
            .sum();
        (n, Some(n))
    }
}

impl DoubleEndedIterator for Iter {
    fn next_back(&mut self) -> Option<Tag> {
        while self.back > self.front {
            let idx = self.back - 1;
            let w = self.words.get_mut(idx)?;
            if *w != 0 {
                let bit = 63 - w.leading_zeros();
                *w &= !(1u64 << bit);
                return Self::tag(idx, bit);
            }
            self.back = idx;
        }
        None
    }
}

impl ExactSizeIterator for Iter {}

impl FusedIterator for Iter {}

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

    #[test]
    fn all_tags_sorted_unique_and_indexed() {
        assert_eq!(ALL_TAGS.len(), TAG_COUNT);
        assert!(ALL_TAGS.windows(2).all(|w| w[0] < w[1]));
        for (i, name) in ALL_TAGS.iter().enumerate() {
            let tag = Tag::from_name(name).expect("indexed");
            assert_eq!(tag.id(), i);
            assert_eq!(tag.as_str(), *name);
        }
        assert_eq!(TagSet::ALL.len(), TAG_COUNT);
        assert_eq!(Tag::all().count(), TAG_COUNT);
    }

    #[test]
    fn base_tag_constants() {
        for (tag, name) in [
            (Tag::DIRECTORY, DIRECTORY),
            (Tag::SYMLINK, SYMLINK),
            (Tag::SOCKET, SOCKET),
            (Tag::FILE, FILE),
            (Tag::EXECUTABLE, EXECUTABLE),
            (Tag::NON_EXECUTABLE, NON_EXECUTABLE),
            (Tag::TEXT, TEXT),
            (Tag::BINARY, BINARY),
        ] {
            assert_eq!(tag.as_str(), name);
        }
        assert_eq!(TYPE_TAGS.len(), 4);
        assert_eq!(MODE_TAGS.len(), 2);
        assert_eq!(ENCODING_TAGS.len(), 2);
        assert!(TYPE_TAGS.is_disjoint(&MODE_TAGS));
        assert!(MODE_TAGS.is_disjoint(&ENCODING_TAGS));
        assert!(Tag::FILE.is_type_tag());
        assert!(Tag::TEXT.is_encoding_tag());
        assert!(Tag::EXECUTABLE.is_mode_tag());
        assert!(is_type_tag("socket") && is_mode_tag("executable") && is_encoding_tag("binary"));
        assert!(!is_type_tag("python"));
    }

    #[test]
    fn unknown_names() {
        assert_eq!(Tag::from_name(""), None);
        assert_eq!(Tag::from_name("Python"), None);
        assert_eq!(Tag::from_id(TAG_COUNT), None);
        let err = "python, nope, nada".parse::<TagSet>().expect_err("unknown");
        assert_eq!(err.names(), ["nope", "nada"]);
        assert_eq!(err.to_string(), r#"unknown tags "nope", "nada""#);
        let err = "zzz".parse::<Tag>().expect_err("unknown");
        assert_eq!(err.to_string(), r#"unknown tag "zzz""#);
    }

    #[test]
    fn set_operations() {
        let a: TagSet = "python, text, file".parse().expect("valid");
        let b: TagSet = "text, binary".parse().expect("valid");
        assert_eq!((a | b).to_string(), "binary, file, python, text");
        assert_eq!((a & b).to_string(), "text");
        assert_eq!((a - b).to_string(), "file, python");
        assert_eq!((a ^ b).to_string(), "binary, file, python");
        assert_eq!((!a).len(), TAG_COUNT - 3);
        assert!((a | b).is_superset(&a));
        assert!(a.is_subset(&(a | b)));
        assert!(a.intersects(&b));
        assert!(!a.is_disjoint(&b));
        let mut c = a;
        assert!(!c.insert(Tag::TEXT));
        assert!(c.remove(Tag::TEXT));
        assert!(!c.contains(Tag::TEXT));
        c |= Tag::BINARY;
        assert!(c.contains("binary"));
        c.clear();
        assert!(c.is_empty());
        assert_eq!(TagSet::from([Tag::TEXT, Tag::FILE]).len(), 2);
        assert_eq!(format!("{:?}", TagSet::from(Tag::TEXT)), r#"{"text"}"#);
        assert_eq!(format!("{:?}", Tag::TEXT), r#"Tag("text")"#);
        assert_eq!(TagSet::EMPTY.to_string(), "");
        assert_eq!("".parse::<TagSet>(), Ok(TagSet::EMPTY));
    }

    #[test]
    fn iteration_order_and_double_ended() {
        let set = TagSet::ALL;
        let forward: Vec<_> = set.names().collect();
        assert_eq!(forward, ALL_TAGS);
        let mut backward: Vec<_> = set.names().rev().collect();
        backward.reverse();
        assert_eq!(backward, ALL_TAGS);
        let mut it = set.iter();
        assert_eq!(it.len(), TAG_COUNT);
        assert_eq!(it.next().map(Tag::id), Some(0));
        assert_eq!(it.next_back().map(Tag::id), Some(TAG_COUNT - 1));
        assert_eq!(it.len(), TAG_COUNT - 2);
        assert_eq!(it.by_ref().count(), TAG_COUNT - 2);
        assert_eq!(it.next(), None);
        assert_eq!(it.next_back(), None);
    }

    #[test]
    fn send_sync_copy() {
        fn assert_traits<T: Send + Sync + Copy + 'static>() {}
        assert_traits::<Tag>();
        assert_traits::<TagSet>();
        assert_eq!(core::mem::size_of::<Tag>(), 2);
        assert_eq!(core::mem::size_of::<TagSet>(), WORDS * 8);
    }
}