1+ use std:: fmt:: Debug ;
12use std:: fs:: File ;
2- use std:: io:: { Error , prelude:: * } ;
3+ use std:: io:: { prelude:: * , Error } ;
34use std:: path:: { Path , PathBuf } ;
45use std:: result:: Result ;
5- use std:: fmt:: Debug ;
66
77use logos:: Logos ;
88
9- use crate :: sfz:: { Group , Header , Opcode , Region , SfzToken } ;
109use crate :: sfz:: types:: OpcodeMap ;
10+ use crate :: sfz:: { Group , Header , Opcode , Region , SfzToken } ;
1111
1212/// Represents the SFZ instrument parsed
1313///
@@ -21,22 +21,19 @@ use crate::sfz::types::OpcodeMap;
2121///
2222#[ derive( Debug ) ]
2323pub struct Instrument {
24- global : OpcodeMap , // these opcodes apply by default
25- groups : Vec < Group > , // these opcodes override those in global for current region
26- regions : Vec < Region > , // these opcodes override global, and their group ones
24+ global : OpcodeMap , // these opcodes apply by default
25+ groups : Vec < Group > , // these opcodes override those in global for current region
26+ regions : Vec < Region > , // these opcodes override global, and their group ones
2727
2828 // maybe make this later a: struct Control
2929 // https://sfzformat.com/headers/control
3030 default_path : PathBuf ,
3131}
3232
33-
3433impl Instrument {
35-
3634 /// Creates an Instrument via loading and parsing some SFZ code in a file
3735 ///
3836 pub fn from_file ( sfz_path : & Path ) -> Result < Self , Error > {
39-
4037 // open sfz file, and read it into sfz_text
4138 let mut sfz_file = File :: open ( & sfz_path) ?;
4239 let mut sfz_text = String :: new ( ) ;
@@ -45,14 +42,12 @@ impl Instrument {
4542 Self :: from_sfz ( & sfz_text, sfz_path. parent ( ) . unwrap ( ) )
4643 }
4744
48-
4945 /// Creates an Instrument via parsing some SFZ code in a string
5046 ///
5147 /// sfz_path would be the root location from where to find the samples
5248 /// and default_path opcode value is appended to it.
5349 ///
5450 pub fn from_sfz ( sfz : & str , sfz_path : & Path ) -> Result < Self , Error > {
55-
5651 // Initializes an instrument for construction
5752 let mut instrument = Instrument {
5853 global : OpcodeMap :: new ( ) ,
@@ -61,77 +56,70 @@ impl Instrument {
6156 default_path : sfz_path. to_path_buf ( ) ,
6257 } ;
6358
64-
6559 // parser loop status
6660 let mut status = InstrumentParsingStatus :: init ( ) ;
6761
6862 // parser loop
6963 let lex = SfzToken :: lexer ( & sfz) ;
70- for t in lex { match & t {
71-
64+ for t in lex {
65+ match & t {
7266 SfzToken :: Header ( h) => {
7367 match h {
7468 Header :: Group => {
7569 status. new_group ( ) ;
7670 instrument. groups . push ( Group :: new ( ) ) ;
77- } ,
71+ }
7872 Header :: Region => {
7973 status. new_region ( ) ;
8074
8175 // FIXME: NOTE: an empty region (or without a sample) should be discarded
8276 instrument. regions . push ( Region :: new ( ) ) ;
83- } ,
77+ }
8478 Header :: Control => {
8579 status. new_control ( ) ;
86- } ,
80+ }
8781 Header :: Global => {
8882 status. new_global ( ) ;
89- } ,
83+ }
9084 // TBD
9185 Header :: Curve => println ! ( "\n <curve>" ) ,
9286 // TBD
9387 Header :: Effect => println ! ( "\n <effect>" ) ,
94- _ => ( )
88+ _ => ( ) ,
9589 }
96- } ,
90+ }
9791
9892 SfzToken :: Opcode ( o) => {
99-
10093 // an opcode for <global>
10194 if status. is_header_global {
10295 instrument. global . insert ( o. str_name ( ) , o. clone ( ) ) ;
10396
10497 // an opcode for <control>
10598 } else if status. is_header_control {
10699 match o {
107- Opcode :: default_path( p) => {
108- instrument. default_path . push ( p)
109- } ,
110- _ => ( )
100+ Opcode :: default_path( p) => instrument. default_path . push ( p) ,
101+ _ => ( ) ,
111102 }
112-
113103 } else {
114-
115104 // an opcode for the <region>
116105 if status. are_regions ( ) {
117106 instrument. add_region_opcode ( & status, o) ;
118107
119108 // an opcode for the <group>
120109 } else if status. are_groups ( ) {
121110 instrument. add_group_opcode ( & status, o) ;
122-
123111 } else {
124112 unreachable ! ( ) ;
125113 }
126114 }
127- } ,
115+ }
128116 _ => ( ) ,
129- } }
117+ }
118+ }
130119
131120 Ok ( instrument)
132121 }
133122
134-
135123 fn add_region_opcode ( & mut self , status : & InstrumentParsingStatus , opcode : & Opcode ) {
136124 self . regions [ status. region_counter . unwrap ( ) ] . add ( opcode, status. group_counter ) ;
137125 }
@@ -141,7 +129,6 @@ impl Instrument {
141129 }
142130}
143131
144-
145132/// The current status of the parsing of the instrument
146133#[ derive( Debug ) ]
147134struct InstrumentParsingStatus {
@@ -162,7 +149,6 @@ impl InstrumentParsingStatus {
162149 }
163150 }
164151
165-
166152 /// A new group header appears
167153 ///
168154 pub fn new_group ( & mut self ) {
@@ -187,7 +173,6 @@ impl InstrumentParsingStatus {
187173 self . region_increment ( ) ;
188174 }
189175
190-
191176 /// A new control header appears
192177 ///
193178 /// There can only be one, and must appear
@@ -198,8 +183,8 @@ impl InstrumentParsingStatus {
198183 if !self . is_header_control
199184 && !self . is_header_global
200185 && self . group_counter == None
201- && self . region_counter == None {
202-
186+ && self . region_counter == None
187+ {
203188 // enter the <control> header
204189 self . is_header_control = true ;
205190 }
@@ -212,10 +197,7 @@ impl InstrumentParsingStatus {
212197 ///
213198 // TODO: if incorrectly placed, following opcodes should be ignored
214199 pub fn new_global ( & mut self ) {
215- if !self . is_header_global
216- && self . group_counter == None
217- && self . region_counter == None {
218-
200+ if !self . is_header_global && self . group_counter == None && self . region_counter == None {
219201 // ensure we are out of the <control> header
220202 self . is_header_control = false ;
221203 // enter the <global> header
@@ -226,7 +208,7 @@ impl InstrumentParsingStatus {
226208 /// Increments the region counter
227209 fn region_increment ( & mut self ) {
228210 match self . region_counter {
229- Some ( c) => self . region_counter = Some ( c+ 1 ) ,
211+ Some ( c) => self . region_counter = Some ( c + 1 ) ,
230212 None => self . region_counter = Some ( 0 ) ,
231213 }
232214 }
@@ -237,7 +219,7 @@ impl InstrumentParsingStatus {
237219 /// Increments the group counter
238220 fn group_increment ( & mut self ) {
239221 match self . region_counter {
240- Some ( c) => self . group_counter = Some ( c+ 1 ) ,
222+ Some ( c) => self . group_counter = Some ( c + 1 ) ,
241223 None => self . group_counter = Some ( 0 ) ,
242224 }
243225 }
0 commit comments