fea-rs-ast 0.2.0

fontTools-like AST wrapper around fea-rs parser
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
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
#![deny(missing_docs)]
//! # fea-rs-ast
//!
//! A Rust port of Python's [`fontTools.feaLib.ast`](https://fonttools.readthedocs.io/en/latest/feaLib/ast.html)
//! library, providing a fontTools-compatible AST (Abstract Syntax Tree) for OpenType Feature Files.
//!
//! This crate builds on top of the [`fea-rs`](https://github.com/googlefonts/fontc/tree/main/fea-rs)
//! parser, providing a higher-level, more ergonomic interface that matches the familiar fontTools API
//! while leveraging Rust's type safety and performance.
//!
//! ## Overview
//!
//! OpenType Feature Files (.fea) define advanced typographic features for fonts using a
//! domain-specific language. This crate provides:
//!
//! - **Parsing**: Load and parse feature files into a structured AST using `fea-rs`.
//! - **Construction**: Programmatically build feature file structures
//! - **Serialization**: Convert AST back to valid feature file syntax via the [`AsFea`] trait
//! - **Transformation**: Modify AST using the visitor pattern
//!
//! ## Architecture
//!
//! The crate provides two main statement enums:
//!
//! - [`Statement`]: All possible statements in a feature file, regardless of context
//! - [`ToplevelItem`]: Only statements valid at the top level of a feature file
//!
//! Both implement the [`AsFea`] trait for serialization back to .fea syntax.
//!
//! ## Examples
//!
//! ### Loading an Existing Feature File
//!
//! Parse a feature file from a string:
//!
//! ```rust
//! use fea_rs_ast::{FeatureFile, AsFea};
//!
//! let fea_code = r#"
//!     languagesystem DFLT dflt;
//!     
//!     feature smcp {
//!         sub a by a.smcp;
//!         sub b by b.smcp;
//!     } smcp;
//! "#;
//!
//! // Simple parsing without glyph name resolution
//! let feature_file = FeatureFile::try_from(fea_code).unwrap();
//!
//! // Or with full resolution support
//! let feature_file = FeatureFile::new_from_fea(
//!     fea_code,
//!     Some(&["a", "a.smcp", "b", "b.smcp"]), // Glyph names
//!     None::<&str>, // Project root for includes
//! ).unwrap();
//!
//! // Serialize back to .fea syntax
//! let output = feature_file.as_fea("");
//! println!("{}", output);
//! ```
//!
//! ### Constructing New Statements
//!
//! Build feature file structures programmatically:
//!
//! ```rust
//! use fea_rs_ast::*;
//!
//! // Create a glyph class definition
//! let lowercase = GlyphClassDefinition::new(
//!     "lowercase".to_string(),
//!     GlyphClass::new(vec![
//!         GlyphContainer::GlyphName(GlyphName::new("a")),
//!         GlyphContainer::GlyphName(GlyphName::new("b")),
//!         GlyphContainer::GlyphName(GlyphName::new("c")),
//!     ], 0..0),
//!     0..0, // location range
//! );
//!
//! // Create a single substitution statement
//! let subst = SingleSubstStatement::new(
//!     vec![GlyphContainer::GlyphName(GlyphName::new("a"))],
//!     vec![GlyphContainer::GlyphName(GlyphName::new("a.smcp"))],
//!     vec![], // prefix
//!     vec![], // suffix
//!     0..0,   // location
//!     false,  // force_chain
//! );
//!
//! // Create a feature block
//! let feature = FeatureBlock::new(
//!     "smcp".into(),
//!     vec![Statement::SingleSubst(subst)],
//!     false, // use_extension
//!     0..0,  // location
//! );
//!
//! // Build the complete feature file
//! let feature_file = FeatureFile::new(vec![
//!     ToplevelItem::GlyphClassDefinition(lowercase),
//!     ToplevelItem::Feature(feature),
//! ]);
//!
//! // Serialize to .fea syntax
//! let output = feature_file.as_fea("");
//! assert!(output.contains("@lowercase = [a b c];"));
//! assert!(output.contains("feature smcp"));
//! assert!(output.contains("sub a by a.smcp;"));
//! ```
//!
//! ### Using the Visitor Pattern
//!
//! Transform AST structures by implementing the [`LayoutVisitor`] trait:
//!
//! ```rust
//! use fea_rs_ast::*;
//!
//! // Create a visitor that renames all features
//! struct FeatureRenamer {
//!     old_name: String,
//!     new_name: String,
//! }
//!
//! impl LayoutVisitor for FeatureRenamer {
//!     fn visit_statement(&mut self, statement: &mut Statement) -> bool {
//!         match statement {
//!             Statement::FeatureBlock(feature) => {
//!                 if feature.name == self.old_name.as_str() {
//!                     feature.name = self.new_name.as_str().into();
//!                 }
//!             }
//!             _ => {}
//!         }
//!         true // Continue visiting
//!     }
//! }
//!
//! // Use the visitor
//! let fea_code = r#"
//!     feature liga {
//!         sub f i by fi;
//!     } liga;
//! "#;
//!
//! let mut feature_file = FeatureFile::try_from(fea_code).unwrap();
//! let mut visitor = FeatureRenamer {
//!     old_name: "liga".to_string(),
//!     new_name: "dlig".to_string(),
//! };
//!
//! visitor.visit(&mut feature_file).unwrap();
//!
//! let output = feature_file.as_fea("");
//! assert!(output.contains("feature dlig"));
//! ```
//!
//! ### More Complex Visitor: Glyph Name Substitution
//!
//! ```rust
//! use fea_rs_ast::*;
//! use std::collections::HashMap;
//!
//! // Visitor that replaces glyph names throughout the AST
//! struct GlyphNameReplacer {
//!     replacements: HashMap<String, String>,
//! }
//!
//! impl LayoutVisitor for GlyphNameReplacer {
//!     fn visit_statement(&mut self, statement: &mut Statement) -> bool {
//!         // Replace glyph names in various statement types
//!         match statement {
//!             Statement::SingleSubst(subst) => {
//!                 for container in &mut subst.glyphs {
//!                     self.replace_in_container(container);
//!                 }
//!                 for container in &mut subst.replacement {
//!                     self.replace_in_container(container);
//!                 }
//!             }
//!             Statement::GlyphClassDefinition(gcd) => {
//!                 for container in &mut gcd.glyphs.glyphs {
//!                     self.replace_in_container(container);
//!                 }
//!             }
//!             _ => {}
//!         }
//!         true
//!     }
//! }
//!
//! impl GlyphNameReplacer {
//!     fn replace_in_container(&self, container: &mut GlyphContainer) {
//!         match container {
//!             GlyphContainer::GlyphName(gn) => {
//!                 if let Some(new_name) = self.replacements.get(gn.name.as_str()) {
//!                     gn.name = new_name.as_str().into();
//!                 }
//!             }
//!             GlyphContainer::GlyphClass(gc) => {
//!                 for glyph_container in &mut gc.glyphs {
//!                     self.replace_in_container(glyph_container);
//!                 }
//!             }
//!             _ => {}
//!         }
//!     }
//! }
//! ```
//!
//! ## Feature Coverage
//!
//! This crate supports most OpenType feature file constructs:
//!
//! - **GSUB**: Single, Multiple, Alternate, Ligature, Contextual, and Reverse Chaining substitutions
//! - **GPOS**: Single, Pair, Cursive, Mark-to-Base, Mark-to-Ligature, and Mark-to-Mark positioning
//! - **Tables**: GDEF, BASE, head, hhea, name, OS/2, STAT, vhea
//! - **Contextual Rules**: Chaining context and ignore statements
//! - **Variable Fonts**: Conditionsets and variation blocks
//! - **Lookups**: Lookup blocks with flags and references
//! - **Features**: Feature blocks with useExtension
//!
//! Features which fea-rs parses which this crate does not currently support:
//!
//! - Glyphs number variables in value records
//! - CID-keyed glyph names
//!
//! ## Compatibility
//!
//! The API closely mirrors fontTools' Python API where practical, making it easier to port
//! existing Python code to Rust. Key differences:
//!
//! - Rust's type system provides compile-time guarantees about statement validity
//! - The [`Statement`] enum distinguishes between all possible statements
//! - The [`ToplevelItem`] enum ensures only valid top-level constructs
//! - Location tracking uses byte ranges (`Range<usize>`) instead of line/column numbers
//!
//! ## Re-exports
//!
//! This crate re-exports the underlying [`fea_rs`] parser for advanced use cases where
//! direct access to the parse tree is needed.

use std::{
    ops::Range,
    path::{Path, PathBuf},
    sync::Arc,
};
mod base;
mod contextual;
mod dummyresolver;
mod error;
mod gdef;
mod glyphcontainers;
mod gpos;
mod gsub;
mod miscellenea;
mod name;
mod os2;
mod stat;
mod tables;
mod values;
mod visitor;
pub use contextual::*;
pub use error::Error;
pub use fea_rs;
use fea_rs::{parse::FileSystemResolver, typed::AstNode as _, GlyphMap, NodeOrToken, ParseTree};
pub use gdef::*;
pub use glyphcontainers::*;
pub use gpos::*;
pub use gsub::*;
pub use miscellenea::*;
pub use name::*;
use smol_str::SmolStr;
pub use tables::*;
pub use values::*;
pub use visitor::LayoutVisitor;

use crate::{base::Base, os2::Os2};

pub(crate) const SHIFT: &str = "    ";

/// Helper function for serde: return default Range<usize> (0..0)
#[cfg(feature = "serde")]
pub(crate) fn default_range() -> Range<usize> {
    0..0
}

/// Helper function for serde: check if a Range<usize> is the default (0..0)
#[cfg(feature = "serde")]
pub(crate) fn is_default_range(r: &Range<usize>) -> bool {
    r.start == 0 && r.end == 0
}

/// Trait for converting AST nodes back to feature file syntax.
pub trait AsFea {
    /// Convert the AST node to feature file syntax with the given indentation.
    fn as_fea(&self, indent: &str) -> String;
}

// All possible statements in a feature file need to go
// here, regardless of context, because we need to be able to
// treat them as a heterogeneous collection when we do visiting etc.
// We split them up by context in later enums.
/// An AST node representing a single statement in a feature file.
#[allow(clippy::large_enum_variant)]
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum Statement {
    // GSUB statements
    /// A single substitution (GSUB type 1) statement: `sub a by b;`
    SingleSubst(SingleSubstStatement),
    /// A multiple substitution (GSUB type 2) statement: `sub a by b c;`
    MultipleSubst(MultipleSubstStatement),
    /// An alternate substitution (GSUB type 3) statement: `sub a from [b c d];`
    AlternateSubst(AlternateSubstStatement),
    /// A ligature substitution (GSUB type 4) statement: `sub a b by c;`
    LigatureSubst(LigatureSubstStatement),
    /// A reverse chaining contextual single substitution (GSUB type 8) statement
    ReverseChainSubst(ReverseChainSingleSubstStatement),
    /// A chaining contextual substitution (GSUB type 6) statement: `sub a' lookup foo b;`
    ChainedContextSubst(ChainedContextStatement<Subst>),
    /// An ignore substitution rule: `ignore sub a b;`
    IgnoreSubst(IgnoreStatement<Subst>),
    // GPOS
    /// A single adjustment positioning (GPOS type 1) statement: `pos a <10 0 20 0>;`
    SinglePos(SinglePosStatement),
    /// A pair adjustment positioning (GPOS type 2) statement: `pos a b <10 0 20 0>;`
    PairPos(PairPosStatement),
    /// A cursive attachment positioning (GPOS type 3) statement
    CursivePos(CursivePosStatement),
    /// A mark-to-base attachment positioning (GPOS type 4) statement
    MarkBasePos(MarkBasePosStatement),
    /// A mark-to-ligature attachment positioning (GPOS type 5) statement
    MarkLigPos(MarkLigPosStatement),
    /// A mark-to-mark attachment positioning (GPOS type 6) statement
    MarkMarkPos(MarkMarkPosStatement),
    /// A chaining contextual positioning (GPOS type 8) statement: `pos a' lookup foo b;`
    ChainedContextPos(ChainedContextStatement<Pos>),
    /// An ignore positioning rule: `ignore pos a b;`
    IgnorePos(IgnoreStatement<Pos>),
    // Miscellenea
    /// An anchor definition: `anchorDef 100 200 contourpoint 5 MyAnchor;`
    AnchorDefinition(AnchorDefinition),
    /// A mark class definition: `markClass a <anchor 100 200> @TOP_MARKS;`
    MarkClassDefinition(MarkClassDefinition),
    /// A comment in the feature file: `# This is a comment`
    Comment(Comment),
    /// A feature name statement within a `featureNames` block
    FeatureNameStatement(NameRecord),
    /// A font revision statement: `FontRevision 1.000;`
    FontRevision(FontRevisionStatement),
    /// A feature reference statement: `feature liga;`
    FeatureReference(FeatureReferenceStatement),
    /// A glyph class definition: `@lowercase = [a b c];`
    GlyphClassDefinition(GlyphClassDefinition),
    /// A language statement: `language dflt;`
    Language(LanguageStatement),
    /// A language system statement: `languagesystem DFLT dflt;`
    LanguageSystem(LanguageSystemStatement),
    /// A lookup flag statement: `lookupflag RightToLeft;`
    LookupFlag(LookupFlagStatement),
    /// A lookup reference statement: `lookup MyLookup;`
    LookupReference(LookupReferenceStatement),
    /// Size feature parameters: `parameters 10.0 0;`
    SizeParameters(SizeParameters),
    /// A size menu name statement: `sizemenuname 3 1 0x409 "Small";`
    SizeMenuName(NameRecord),
    /// A subtable statement: `subtable;`
    Subtable(SubtableStatement),
    /// A script statement: `script latn;`
    Script(ScriptStatement),
    /// A value record definition: `valueRecordDef 10 MyValue;`
    ValueRecordDefinition(ValueRecordDefinition),
    /// A condition set for variable fonts: `conditionset heavy { wght 700 900; } heavy;`
    ConditionSet(ConditionSet),
    /// A variation block for variable fonts: `variation rvrn heavy { ... } rvrn;`
    VariationBlock(VariationBlock),
    // Tables and blocks
    /// A BASE table definition: `table BASE { ... } BASE;`
    Base(Table<Base>),
    /// A GDEF table definition: `table GDEF { ... } GDEF;`
    Gdef(Table<Gdef>),
    /// A head table definition: `table head { ... } head;`
    Head(Table<Head>),
    /// An hhea table definition: `table hhea { ... } hhea;`
    Hhea(Table<Hhea>),
    /// A name table definition: `table name { ... } name;`
    Name(Table<Name>),
    /// An OS/2 table definition: `table OS/2 { ... } OS/2;`
    Os2(Table<Os2>),
    /// A STAT table definition: `table STAT { ... } STAT;`
    Stat(Table<Stat>),
    /// A vhea table definition: `table vhea { ... } vhea;`
    Vhea(Table<Vhea>),
    /// A feature block: `feature liga { ... } liga;`
    FeatureBlock(FeatureBlock),
    /// A lookup block: `lookup MyLookup { ... } MyLookup;`
    LookupBlock(LookupBlock),
    /// A nested block (e.g., `featureNames { ... };`)
    NestedBlock(NestedBlock),
    // GDEF-related statements
    /// A GDEF Attach statement: `Attach a 1 2 3;`
    GdefAttach(AttachStatement),
    /// A GDEF GlyphClassDef statement: `GlyphClassDef [a b], [c d], , [e f];`
    GdefClassDef(GlyphClassDefStatement),
    /// A GDEF LigatureCaretByIndex statement: `LigatureCaret a 1 2;`
    GdefLigatureCaretByIndex(LigatureCaretByIndexStatement),
    /// A GDEF LigatureCaretByPos statement: `LigatureCaretByPos a 100 200;`
    GdefLigatureCaretByPos(LigatureCaretByPosStatement),
}
impl AsFea for Statement {
    fn as_fea(&self, indent: &str) -> String {
        match self {
            // GSUB
            Statement::SingleSubst(ss) => ss.as_fea(indent),
            Statement::MultipleSubst(ms) => ms.as_fea(indent),
            Statement::AlternateSubst(alt) => alt.as_fea(indent),
            Statement::LigatureSubst(ls) => ls.as_fea(indent),
            Statement::ChainedContextSubst(ccs) => ccs.as_fea(indent),
            Statement::IgnoreSubst(is) => is.as_fea(indent),
            Statement::ReverseChainSubst(rss) => rss.as_fea(indent),
            // GPOS
            Statement::SinglePos(sp) => sp.as_fea(indent),
            Statement::PairPos(pp) => pp.as_fea(indent),
            Statement::CursivePos(cp) => cp.as_fea(indent),
            Statement::MarkBasePos(mbp) => mbp.as_fea(indent),
            Statement::MarkLigPos(mlp) => mlp.as_fea(indent),
            Statement::MarkMarkPos(mmp) => mmp.as_fea(indent),
            Statement::ChainedContextPos(ccs) => ccs.as_fea(indent),
            Statement::IgnorePos(ip) => ip.as_fea(indent),
            // Miscellenea
            Statement::AnchorDefinition(ad) => ad.as_fea(indent),
            Statement::Comment(c) => c.as_fea(indent),
            Statement::FeatureReference(fr) => fr.as_fea(indent),
            Statement::FeatureNameStatement(fr) => fr.as_fea(indent),
            Statement::FontRevision(fr) => fr.as_fea(indent),
            Statement::GlyphClassDefinition(gcd) => gcd.as_fea(indent),
            Statement::Language(ls) => ls.as_fea(indent),
            Statement::LanguageSystem(ls) => ls.as_fea(indent),
            Statement::LookupFlag(lf) => lf.as_fea(indent),
            Statement::LookupReference(lr) => lr.as_fea(indent),
            Statement::MarkClassDefinition(mc) => mc.as_fea(indent),
            Statement::Script(sc) => sc.as_fea(indent),
            Statement::SizeMenuName(sm) => sm.as_fea(indent),
            Statement::SizeParameters(sp) => sp.as_fea(indent),
            Statement::Subtable(st) => st.as_fea(indent),
            Statement::ValueRecordDefinition(vrd) => vrd.as_fea(indent),
            Statement::ConditionSet(cs) => cs.as_fea(indent),
            Statement::VariationBlock(vb) => vb.as_fea(indent),
            // GDEF-related statements
            Statement::GdefAttach(at) => at.as_fea(indent),
            Statement::GdefClassDef(gcd) => gcd.as_fea(indent),
            Statement::GdefLigatureCaretByIndex(lc) => lc.as_fea(indent),
            Statement::GdefLigatureCaretByPos(lc) => lc.as_fea(indent),
            // Tables and blocks
            Statement::Base(base) => base.as_fea(indent),
            Statement::Gdef(gdef) => gdef.as_fea(indent),
            Statement::Head(head) => head.as_fea(indent),
            Statement::Hhea(hhea) => hhea.as_fea(indent),
            Statement::Name(name) => name.as_fea(indent),
            Statement::Os2(os2) => os2.as_fea(indent),
            Statement::Stat(stat) => stat.as_fea(indent),
            Statement::Vhea(vhea) => vhea.as_fea(indent),
            Statement::FeatureBlock(fb) => fb.as_fea(indent),
            Statement::LookupBlock(lb) => lb.as_fea(indent),
            Statement::NestedBlock(nb) => nb.as_fea(indent),
        }
    }
}

fn to_statement(child: &NodeOrToken) -> Option<Statement> {
    if child.kind() == fea_rs::Kind::Comment {
        return Some(Statement::Comment(Comment::from(
            child.token_text().unwrap(),
        )));
    } else if child.kind() == fea_rs::Kind::SubtableNode {
        return Some(Statement::Subtable(SubtableStatement::new()));
    }
    #[allow(clippy::manual_map)]
    // GSUB
    if let Some(gsub1) = fea_rs::typed::Gsub1::cast(child) {
        Some(Statement::SingleSubst(gsub1.into()))
    } else if let Some(gsub2) = fea_rs::typed::Gsub2::cast(child) {
        Some(Statement::MultipleSubst(gsub2.into()))
    } else if let Some(gsub3) = fea_rs::typed::Gsub3::cast(child) {
        Some(Statement::AlternateSubst(gsub3.into()))
    } else if let Some(gsub4) = fea_rs::typed::Gsub4::cast(child) {
        Some(Statement::LigatureSubst(gsub4.into()))
    } else if let Some(gsub6) = fea_rs::typed::Gsub6::cast(child) {
        Some(gsub6.into())
    } else if let Some(rss) = fea_rs::typed::Gsub8::cast(child) {
        Some(Statement::ReverseChainSubst(rss.into()))
    } else if let Some(gsig) = fea_rs::typed::GsubIgnore::cast(child) {
        Some(Statement::IgnoreSubst(gsig.into()))
        // GPOS
    } else if let Some(gpos1) = fea_rs::typed::Gpos1::cast(child) {
        Some(Statement::SinglePos(gpos1.into()))
    } else if let Some(gpos2) = fea_rs::typed::Gpos2::cast(child) {
        Some(Statement::PairPos(gpos2.into()))
    } else if let Some(gpos3) = fea_rs::typed::Gpos3::cast(child) {
        Some(Statement::CursivePos(gpos3.into()))
    } else if let Some(gpos4) = fea_rs::typed::Gpos4::cast(child) {
        Some(Statement::MarkBasePos(gpos4.into()))
    } else if let Some(gpos5) = fea_rs::typed::Gpos5::cast(child) {
        Some(Statement::MarkLigPos(gpos5.into()))
    } else if let Some(gpos6) = fea_rs::typed::Gpos6::cast(child) {
        Some(Statement::MarkMarkPos(gpos6.into()))
    } else if let Some(gpos8) = fea_rs::typed::Gpos8::cast(child) {
        Some(gpos8.into())
    } else if let Some(gpig) = fea_rs::typed::GposIgnore::cast(child) {
        Some(Statement::IgnorePos(gpig.into()))
    // Miscellenea
    } else if let Some(ad) = fea_rs::typed::AnchorDef::cast(child) {
        Some(Statement::AnchorDefinition(ad.into()))
    } else if let Some(at) = fea_rs::typed::GdefAttach::cast(child) {
        Some(Statement::GdefAttach(at.into()))
    } else if let Some(gcd) = fea_rs::typed::GdefClassDef::cast(child) {
        Some(Statement::GdefClassDef(gcd.into()))
    } else if let Some(lc) = fea_rs::typed::GdefLigatureCaret::cast(child) {
        // Check if it's by position or by index based on the first keyword
        let is_by_pos = lc
            .iter()
            .next()
            .map(|t| t.kind() == fea_rs::Kind::LigatureCaretByPosKw)
            .unwrap_or(false);
        if is_by_pos {
            Some(Statement::GdefLigatureCaretByPos(lc.into()))
        } else {
            Some(Statement::GdefLigatureCaretByIndex(lc.into()))
        }
    } else if let Some(fr) = fea_rs::typed::FeatureRef::cast(child) {
        Some(Statement::FeatureReference(fr.into()))
    } else if let Some(fr) = fea_rs::typed::HeadFontRevision::cast(child) {
        Some(Statement::FontRevision(fr.into()))
    } else if let Some(gcd) = fea_rs::typed::GlyphClassDef::cast(child) {
        Some(Statement::GlyphClassDefinition(gcd.into()))
    } else if let Some(lang) = fea_rs::typed::Language::cast(child) {
        Some(Statement::Language(lang.into()))
    } else if let Some(langsys) = fea_rs::typed::LanguageSystem::cast(child) {
        Some(Statement::LanguageSystem(langsys.into()))
    } else if let Some(lookupflag) = fea_rs::typed::LookupFlag::cast(child) {
        Some(Statement::LookupFlag(lookupflag.into()))
    } else if let Some(lookupref) = fea_rs::typed::LookupRef::cast(child) {
        Some(Statement::LookupReference(lookupref.into()))
    } else if let Some(mcd) = fea_rs::typed::MarkClassDef::cast(child) {
        Some(Statement::MarkClassDefinition(mcd.into()))
    } else if let Some(script) = fea_rs::typed::Script::cast(child) {
        Some(Statement::Script(script.into()))
    } else if let Some(menuname) = fea_rs::typed::SizeMenuName::cast(child) {
        Some(Statement::SizeMenuName(menuname.into()))
    } else if let Some(sizeparams) = fea_rs::typed::Parameters::cast(child) {
        Some(Statement::SizeParameters(sizeparams.into()))
    } else if let Some(featurenames) = fea_rs::typed::FeatureNames::cast(child) {
        Some(Statement::NestedBlock(featurenames.into()))
    // Doesn't exist in fea_rs AST!
    // } else if let Some(subtable) = fea_rs::typed::Subtable::cast(child) {
    //     Some(Statement::Subtable(SubtableStatement::new()))
    } else if let Some(vrd) = fea_rs::typed::ValueRecordDef::cast(child) {
        Some(Statement::ValueRecordDefinition(vrd.into()))
    } else if let Some(cs) = fea_rs::typed::ConditionSet::cast(child) {
        Some(Statement::ConditionSet(cs.into()))
    } else if let Some(fv) = fea_rs::typed::FeatureVariation::cast(child) {
        Some(Statement::VariationBlock(fv.into()))
    // Lookup blocks can exist within features
    } else if let Some(lookup) = fea_rs::typed::LookupBlock::cast(child) {
        Some(Statement::LookupBlock(lookup.into()))
    } else {
        None
    }
}

/// A named feature block. (`feature foo { ... } foo;`)
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FeatureBlock {
    /// The name of the feature (also called the tag)
    pub name: SmolStr,
    /// The statements in the feature block
    pub statements: Vec<Statement>,
    /// Whether the feature uses `useExtension`
    pub use_extension: bool,
    /// The position of the feature block in the source
    #[cfg_attr(
        feature = "serde",
        serde(
            default = "crate::default_range",
            skip_serializing_if = "crate::is_default_range"
        )
    )]
    pub pos: Range<usize>,
}

impl FeatureBlock {
    /// Creates a new FeatureBlock.
    pub fn new(
        name: SmolStr,
        statements: Vec<Statement>,
        use_extension: bool,
        pos: Range<usize>,
    ) -> Self {
        Self {
            name,
            statements,
            use_extension,
            pos,
        }
    }
}

impl AsFea for FeatureBlock {
    fn as_fea(&self, indent: &str) -> String {
        let mut res = String::new();
        res.push_str(&format!("{}feature {} {{\n", indent, self.name));
        let mid_indent = indent.to_string() + SHIFT;
        res.push_str(&format!(
            "{}\n",
            self.statements
                .iter()
                .map(|s| s.as_fea(&mid_indent))
                .collect::<Vec<_>>()
                .join(&format!("\n{mid_indent}"))
        ));
        res.push_str(&format!("{}}} {};", indent, self.name));
        res
    }
}

impl From<fea_rs::typed::Feature> for FeatureBlock {
    fn from(val: fea_rs::typed::Feature) -> Self {
        let statements: Vec<Statement> = val
            .node()
            .iter_children()
            .filter_map(to_statement)
            .collect();
        FeatureBlock {
            name: SmolStr::new(&val.tag().token().text),
            use_extension: val.iter().any(|t| t.kind() == fea_rs::Kind::UseExtensionKw),
            statements,
            pos: val.node().range(),
        }
    }
}

/// A named lookup block. (`lookup foo { ... } foo;`)
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct LookupBlock {
    /// The name of the lookup
    pub name: SmolStr,
    /// The statements in the lookup block
    pub statements: Vec<Statement>,
    /// Whether the lookup should be placed in a separate extension subtable
    pub use_extension: bool,
    /// The position of the lookup block in the source
    #[cfg_attr(
        feature = "serde",
        serde(
            default = "crate::default_range",
            skip_serializing_if = "crate::is_default_range"
        )
    )]
    pub pos: Range<usize>,
}

impl LookupBlock {
    /// Creates a new LookupBlock.
    pub fn new(
        name: SmolStr,
        statements: Vec<Statement>,
        use_extension: bool,
        pos: Range<usize>,
    ) -> Self {
        Self {
            name,
            statements,
            use_extension,
            pos,
        }
    }
}

impl AsFea for LookupBlock {
    fn as_fea(&self, indent: &str) -> String {
        let mut res = String::new();
        res.push_str(&format!("{}lookup {} {{\n", indent, self.name));
        let mid_indent = indent.to_string() + SHIFT;
        res.push_str(&format!(
            "{mid_indent}{}\n",
            self.statements
                .iter()
                .map(|s| s.as_fea(&mid_indent))
                .collect::<Vec<_>>()
                .join(&format!("\n{mid_indent}"))
        ));
        res.push_str(&format!("{}}} {};", indent, self.name));
        res
    }
}

impl From<fea_rs::typed::LookupBlock> for LookupBlock {
    fn from(val: fea_rs::typed::LookupBlock) -> Self {
        let statements: Vec<Statement> = val
            .node()
            .iter_children()
            .filter_map(to_statement)
            .collect();
        let label = val
            .iter()
            .find(|t| t.kind() == fea_rs::Kind::Label)
            .unwrap();
        LookupBlock {
            name: SmolStr::from(label.as_token().unwrap().text.as_str()),
            use_extension: val.iter().any(|t| t.kind() == fea_rs::Kind::UseExtensionKw),
            statements,
            pos: val.node().range(),
        }
    }
}

/// A nested block containing statements (e.g., `featureNames { ... };`)
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct NestedBlock {
    /// The tag identifying the block type
    pub tag: SmolStr,
    /// The statements contained in the block
    pub statements: Vec<Statement>,
    /// The position of the block in the source
    #[cfg_attr(
        feature = "serde",
        serde(
            default = "crate::default_range",
            skip_serializing_if = "crate::is_default_range"
        )
    )]
    pub pos: Range<usize>,
}

impl AsFea for NestedBlock {
    fn as_fea(&self, indent: &str) -> String {
        let mut res = String::new();
        res.push_str(&format!("{}{} {{\n", indent, self.tag));
        let mid_indent = indent.to_string() + SHIFT;
        res.push_str(&format!(
            "{mid_indent}{}\n",
            self.statements
                .iter()
                .map(|s| s.as_fea(&mid_indent))
                .collect::<Vec<_>>()
                .join(&format!("\n{mid_indent}"))
        ));
        res.push_str(&format!("{}}};\n", indent));
        res
    }
}

impl From<fea_rs::typed::FeatureNames> for NestedBlock {
    fn from(val: fea_rs::typed::FeatureNames) -> Self {
        #[allow(clippy::manual_map)]
        let statements: Vec<Statement> = val
            .node()
            .iter_children()
            .filter_map(|child| {
                // Preserve comments
                if child.kind() == fea_rs::Kind::Comment {
                    return Some(Statement::Comment(Comment::from(
                        child.token_text().unwrap(),
                    )));
                }
                if let Some(name_spec) = fea_rs::typed::NameSpec::cast(child) {
                    let (platform_id, plat_enc_id, lang_id, string) = parse_namespec(name_spec);
                    Some(Statement::FeatureNameStatement(NameRecord {
                        platform_id,
                        plat_enc_id,
                        lang_id,
                        string,
                        kind: NameRecordKind::FeatureName,
                        location: child.range(),
                    }))
                } else {
                    None
                }
            })
            .collect();
        NestedBlock {
            tag: SmolStr::new("featureNames"),
            statements,
            pos: val.node().range(),
        }
    }
}

/// Statements that can appear at the top level of a feature file.
///
/// This is a subset of [`Statement`] containing only constructs that are
/// valid at the top level, excluding statements that can only appear within
/// features, lookups, or table definitions.
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub enum ToplevelItem {
    /// A glyph class definition: `@lowercase = [a b c];`
    GlyphClassDefinition(GlyphClassDefinition),
    /// A mark class definition: `markClass a <anchor 100 200> @TOP_MARKS;`
    MarkClassDefinition(MarkClassDefinition),
    /// A language system statement: `languagesystem DFLT dflt;`
    LanguageSystem(LanguageSystemStatement),
    // Include(IncludeStatement),
    /// A feature block: `feature liga { ... } liga;`
    Feature(FeatureBlock),
    /// A lookup block: `lookup MyLookup { ... } MyLookup;`
    Lookup(LookupBlock),
    /// A comment in the feature file: `# This is a comment`
    Comment(Comment),
    /// An anchor definition: `anchorDef 100 200 contourpoint 5 MyAnchor;`
    AnchorDefinition(AnchorDefinition),
    /// A value record definition: `valueRecordDef 10 MyValue;`
    ValueRecordDefinition(ValueRecordDefinition),
    /// A condition set for variable fonts: `conditionset heavy { wght 700 900; } heavy;`
    ConditionSet(ConditionSet),
    /// A variation block for variable fonts: `variation rvrn heavy { ... } rvrn;`
    VariationBlock(VariationBlock),
    /// A GDEF Glyph Class definition statement
    GdefClassDef(GlyphClassDefStatement),
    // Tables
    /// A BASE table definition: `table BASE { ... } BASE;`
    Base(Table<Base>),
    /// A GDEF table definition: `table GDEF { ... } GDEF;`
    Gdef(Table<Gdef>),
    /// A head table definition: `table head { ... } head;`
    Head(Table<Head>),
    /// An hhea table definition: `table hhea { ... } hhea;`
    Hhea(Table<Hhea>),
    /// A name table definition: `table name { ... } name;`
    Name(Table<Name>),
    /// An OS/2 table definition: `table OS/2 { ... } OS/2;`
    Os2(Table<Os2>),
    /// A STAT table definition: `table STAT { ... } STAT;`
    Stat(Table<Stat>),
    /// A vhea table definition: `table vhea { ... } vhea;`
    Vhea(Table<Vhea>),
}
impl From<ToplevelItem> for Statement {
    fn from(val: ToplevelItem) -> Self {
        match val {
            ToplevelItem::GlyphClassDefinition(gcd) => Statement::GlyphClassDefinition(gcd),
            ToplevelItem::MarkClassDefinition(gcd) => Statement::MarkClassDefinition(gcd),
            ToplevelItem::GdefClassDef(gcds) => Statement::GdefClassDef(gcds),

            ToplevelItem::LanguageSystem(ls) => Statement::LanguageSystem(ls),
            ToplevelItem::Feature(fb) => Statement::FeatureBlock(fb),
            ToplevelItem::Lookup(lb) => Statement::LookupBlock(lb),
            ToplevelItem::Comment(cmt) => Statement::Comment(cmt),
            ToplevelItem::AnchorDefinition(ad) => Statement::AnchorDefinition(ad),
            ToplevelItem::ValueRecordDefinition(vrd) => Statement::ValueRecordDefinition(vrd),
            ToplevelItem::ConditionSet(cs) => Statement::ConditionSet(cs),
            ToplevelItem::VariationBlock(vb) => Statement::VariationBlock(vb),
            ToplevelItem::Base(base) => Statement::Base(base),
            ToplevelItem::Gdef(gdef) => Statement::Gdef(gdef),
            ToplevelItem::Head(head) => Statement::Head(head),
            ToplevelItem::Hhea(hhea) => Statement::Hhea(hhea),
            ToplevelItem::Name(name) => Statement::Name(name),
            ToplevelItem::Os2(os2) => Statement::Os2(os2),
            ToplevelItem::Stat(stat) => Statement::Stat(stat),
            ToplevelItem::Vhea(vhea) => Statement::Vhea(vhea),
        }
    }
}
impl TryFrom<Statement> for ToplevelItem {
    type Error = crate::Error;

    fn try_from(value: Statement) -> Result<Self, Self::Error> {
        match value {
            Statement::GlyphClassDefinition(gcd) => Ok(ToplevelItem::GlyphClassDefinition(gcd)),
            Statement::MarkClassDefinition(mcd) => Ok(ToplevelItem::MarkClassDefinition(mcd)),
            Statement::GdefClassDef(gcds) => Ok(ToplevelItem::GdefClassDef(gcds)),
            Statement::LanguageSystem(ls) => Ok(ToplevelItem::LanguageSystem(ls)),
            Statement::FeatureBlock(fb) => Ok(ToplevelItem::Feature(fb)),
            Statement::LookupBlock(lb) => Ok(ToplevelItem::Lookup(lb)),
            Statement::Comment(cmt) => Ok(ToplevelItem::Comment(cmt)),
            Statement::AnchorDefinition(ad) => Ok(ToplevelItem::AnchorDefinition(ad)),
            Statement::ValueRecordDefinition(vrd) => Ok(ToplevelItem::ValueRecordDefinition(vrd)),
            Statement::ConditionSet(cs) => Ok(ToplevelItem::ConditionSet(cs)),
            Statement::VariationBlock(vb) => Ok(ToplevelItem::VariationBlock(vb)),
            Statement::Base(base) => Ok(ToplevelItem::Base(base)),
            Statement::Gdef(gdef) => Ok(ToplevelItem::Gdef(gdef)),
            Statement::Head(head) => Ok(ToplevelItem::Head(head)),
            Statement::Hhea(hhea) => Ok(ToplevelItem::Hhea(hhea)),
            Statement::Name(name) => Ok(ToplevelItem::Name(name)),
            Statement::Os2(os2) => Ok(ToplevelItem::Os2(os2)),
            Statement::Stat(stat) => Ok(ToplevelItem::Stat(stat)),
            Statement::Vhea(vhea) => Ok(ToplevelItem::Vhea(vhea)),
            _ => Err(crate::Error::CannotConvert),
        }
    }
}

impl AsFea for ToplevelItem {
    fn as_fea(&self, indent: &str) -> String {
        match self {
            ToplevelItem::GlyphClassDefinition(gcd) => gcd.as_fea(indent),
            ToplevelItem::MarkClassDefinition(mcd) => mcd.as_fea(indent),
            ToplevelItem::GdefClassDef(gcds) => gcds.as_fea(indent),
            ToplevelItem::LanguageSystem(ls) => ls.as_fea(indent),
            ToplevelItem::Feature(fb) => fb.as_fea(indent),
            ToplevelItem::Lookup(lb) => lb.as_fea(indent),
            ToplevelItem::Comment(cmt) => cmt.as_fea(indent),
            ToplevelItem::AnchorDefinition(ad) => ad.as_fea(indent),
            ToplevelItem::ValueRecordDefinition(vrd) => vrd.as_fea(indent),
            ToplevelItem::ConditionSet(cs) => cs.as_fea(indent),
            ToplevelItem::VariationBlock(vb) => vb.as_fea(indent),
            ToplevelItem::Base(base) => base.as_fea(indent),
            ToplevelItem::Gdef(gdef) => gdef.as_fea(indent),
            ToplevelItem::Head(head) => head.as_fea(indent),
            ToplevelItem::Hhea(hhea) => hhea.as_fea(indent),
            ToplevelItem::Name(name) => name.as_fea(indent),
            ToplevelItem::Os2(os2) => os2.as_fea(indent),
            ToplevelItem::Stat(stat) => stat.as_fea(indent),
            ToplevelItem::Vhea(vhea) => vhea.as_fea(indent),
        }
    }
}
#[allow(clippy::manual_map)]
fn to_toplevel_item(child: &NodeOrToken) -> Option<ToplevelItem> {
    if child.kind() == fea_rs::Kind::Comment {
        Some(ToplevelItem::Comment(Comment::from(
            child.token_text().unwrap(),
        )))
    } else if let Some(gcd) = fea_rs::typed::GlyphClassDef::cast(child) {
        Some(ToplevelItem::GlyphClassDefinition(gcd.into()))
    } else if let Some(mcd) = fea_rs::typed::MarkClassDef::cast(child) {
        Some(ToplevelItem::MarkClassDefinition(mcd.into()))
    } else if let Some(langsys) = fea_rs::typed::LanguageSystem::cast(child) {
        Some(ToplevelItem::LanguageSystem(langsys.into()))
    } else if let Some(feature) = fea_rs::typed::Feature::cast(child) {
        Some(ToplevelItem::Feature(feature.into()))
    } else if let Some(lookup) = fea_rs::typed::LookupBlock::cast(child) {
        Some(ToplevelItem::Lookup(lookup.into()))
    } else if let Some(ad) = fea_rs::typed::AnchorDef::cast(child) {
        Some(ToplevelItem::AnchorDefinition(ad.into()))
    } else if let Some(vrd) = fea_rs::typed::ValueRecordDef::cast(child) {
        Some(ToplevelItem::ValueRecordDefinition(vrd.into()))
    } else if let Some(cs) = fea_rs::typed::ConditionSet::cast(child) {
        Some(ToplevelItem::ConditionSet(cs.into()))
    } else if let Some(fv) = fea_rs::typed::FeatureVariation::cast(child) {
        Some(ToplevelItem::VariationBlock(fv.into()))
    } else if let Some(base) = fea_rs::typed::BaseTable::cast(child) {
        Some(ToplevelItem::Base(base.into()))
    } else if let Some(gdef) = fea_rs::typed::GdefTable::cast(child) {
        Some(ToplevelItem::Gdef(gdef.into()))
    } else if let Some(head) = fea_rs::typed::HeadTable::cast(child) {
        Some(ToplevelItem::Head(head.into()))
    } else if let Some(hhea) = fea_rs::typed::HheaTable::cast(child) {
        Some(ToplevelItem::Hhea(hhea.into()))
    } else if let Some(vhea) = fea_rs::typed::VheaTable::cast(child) {
        Some(ToplevelItem::Vhea(vhea.into()))
    } else if let Some(name) = fea_rs::typed::NameTable::cast(child) {
        Some(ToplevelItem::Name(name.into()))
    } else if let Some(os2) = fea_rs::typed::Os2Table::cast(child) {
        Some(ToplevelItem::Os2(os2.into()))
    } else if let Some(stat) = fea_rs::typed::StatTable::cast(child) {
        Some(ToplevelItem::Stat(stat.into()))
    } else {
        None
    }
}

/// A complete OpenType Feature File.
///
/// This is the root structure representing a parsed .fea file, containing
/// a sequence of top-level statements such as glyph class definitions,
/// feature blocks, lookup blocks, and table definitions.
///
/// # Examples
///
/// ```
/// use fea_rs_ast::FeatureFile;
///
/// let fea_code = "languagesystem DFLT dflt;";
/// let feature_file = FeatureFile::try_from(fea_code).unwrap();
/// ```
#[cfg_attr(feature = "serde", derive(serde::Serialize, serde::Deserialize))]
#[derive(Debug, Clone, PartialEq, Eq)]
pub struct FeatureFile {
    /// The top-level statements in the feature file
    pub statements: Vec<ToplevelItem>,
}
impl FeatureFile {
    /// Creates a new `FeatureFile` from a list of top-level statements.
    pub fn new(statements: Vec<ToplevelItem>) -> Self {
        Self { statements }
    }

    /// Returns an iterator over the top-level statements in the file.
    pub fn iter(&self) -> impl Iterator<Item = &ToplevelItem> {
        self.statements.iter()
    }

    /// Parses a feature file from a string with optional glyph name resolution.
    ///
    /// # Arguments
    ///
    /// * `features` - The feature file source code as a string
    /// * `glyph_names` - Optional list of glyph names for validation and range expansion
    /// * `project_root` - Optional project root directory for resolving `include` statements
    ///
    /// # Examples
    ///
    /// ```
    /// use fea_rs_ast::FeatureFile;
    ///
    /// let fea_code = "languagesystem DFLT dflt;";
    /// let feature_file = FeatureFile::new_from_fea(
    ///     fea_code,
    ///     None::<&[&str]>,
    ///     None::<&str>,
    /// ).unwrap();
    /// ```
    pub fn new_from_fea(
        features: &str,
        glyph_names: Option<&[&str]>,
        project_root: Option<impl Into<PathBuf>>,
    ) -> Result<Self, crate::Error> {
        let glyph_map = glyph_names
            .map(|gn| GlyphMap::new(gn.iter().cloned()))
            .transpose()?;
        let resolver: Box<dyn fea_rs::parse::SourceResolver> =
            if let Some(project_root) = project_root {
                let path = project_root.into();
                Box::new(FileSystemResolver::new(path))
            } else {
                Box::new(dummyresolver::DummyResolver)
            };
        let features_text: Arc<str> = Arc::from(features);
        let (parse_tree, mut diagnostics) = fea_rs::parse::parse_root(
            "get_parse_tree".into(),
            glyph_map.as_ref(),
            Box::new(move |s: &Path| {
                if s == Path::new("get_parse_tree") {
                    Ok(features_text.clone())
                } else {
                    let path = resolver.resolve_raw_path(s.as_ref(), None);
                    let canonical = resolver.canonicalize(&path)?;
                    resolver.get_contents(&canonical)
                }
            }),
        )?;
        diagnostics.split_off_warnings();
        if diagnostics.has_errors() {
            return Err(crate::Error::FeatureParsing(diagnostics));
        }
        Ok(parse_tree.into())
    }
}
impl AsFea for FeatureFile {
    fn as_fea(&self, indent: &str) -> String {
        let mut res = String::new();
        for stmt in &self.statements {
            res.push_str(&stmt.as_fea(indent));
            res.push('\n');
        }
        res
    }
}
impl From<ParseTree> for FeatureFile {
    fn from(val: ParseTree) -> Self {
        let statements: Vec<ToplevelItem> = val
            .root()
            .iter_children()
            .filter_map(to_toplevel_item)
            .collect();
        FeatureFile { statements }
    }
}

/// Turn a string into a FeatureFile
///
/// Only suitable for simple cases and testing; does not resolve glyph name
/// ranges or includes.
impl TryFrom<&str> for FeatureFile {
    type Error = fea_rs::DiagnosticSet;

    fn try_from(value: &str) -> Result<Self, Self::Error> {
        let (parsed, diag) = fea_rs::parse::parse_string(value);
        if diag.has_errors() {
            Err(diag)
        } else {
            Ok(parsed.into())
        }
    }
}
#[cfg(test)]
mod tests {
    use rstest::rstest;

    use super::*;

    #[test]
    fn test_parse() {
        const FEA: &str = r#"feature smcp {
            sub a by a.smcp;
        } smcp;
        "#;
        let (parsed, _) = fea_rs::parse::parse_string(FEA);
        let feature_block = parsed.root().iter_children().next().unwrap();

        let Some(feature) = fea_rs::typed::Feature::cast(feature_block) else {
            panic!("Expected Feature, got {:?}", feature_block.kind());
        };
        let feature_block: FeatureBlock = feature.into();
        assert_eq!(feature_block.name.as_str(), "smcp");
        assert_eq!(feature_block.statements.len(), 1);
        assert_eq!(
            normalize_whitespace(&feature_block.as_fea("")),
            normalize_whitespace("feature smcp {\n    sub a by a.smcp;\n} smcp;\n")
        );
    }

    fn normalize_whitespace(s: &str) -> String {
        s.replace("#", "\n#")
            .replace("\n\n", "\n")
            .lines()
            .filter(|l| !l.trim().is_empty())
            .map(|l| l.trim())
            .collect::<Vec<_>>()
            .join("\n")
            .replace("\t", "    ")
            .replace("position ", "pos ")
            .replace("substitute ", "sub ")
            .replace("reversesub ", "rsub ")
    }

    #[rstest]
    fn for_each_file(
        #[files("resources/test/*.fea")]
        #[exclude("ChainPosSubtable_fea")] // fontTools doesn't support it either
        #[exclude("AlternateChained.fea")] // fontTools doesn't support it either
        #[exclude("baseClass.fea")] // Fine, just the line breaks are different
        #[exclude("STAT_bad.fea")] // Fine, just the line breaks are different
        #[exclude("include0.fea")] // We don't process includes
        #[exclude("GSUB_error.fea")] // Literally a parse failure
        #[exclude("spec10.fea")] // I don't care
        path: std::path::PathBuf,
    ) {
        let fea_str = std::fs::read_to_string(&path).unwrap();
        let (parsed, diag) = fea_rs::parse::parse_string(fea_str.clone());
        if diag.has_errors() {
            panic!("fea-rs didn't like file {:?}:\n{:#?}", path, diag);
        }
        let feature_file: FeatureFile = parsed.into();
        let fea_output = feature_file.as_fea("");
        let orig = normalize_whitespace(&fea_str);
        let output = normalize_whitespace(&fea_output);
        let mut orig_lines = orig.lines().collect::<Vec<_>>();
        for i in 0..orig_lines.len() {
            if let Some(replacement) = orig_lines[i].strip_prefix("#test-fea2fea: ") {
                orig_lines[i + 1] = replacement;
            }
        }
        let orig = orig_lines.join("\n");
        pretty_assertions::assert_eq!(orig, output, "Mismatch in file {:?}", path);
    }
}