1- use std:: fmt:: Debug ;
2- use std:: fs:: File ;
3- use std:: io:: prelude:: * ;
4- use std:: path:: { Path , PathBuf } ;
5- use std:: result:: Result ;
1+ use std:: {
2+ fmt:: Debug ,
3+ fs:: File ,
4+ io:: prelude:: * ,
5+ path:: { Path , PathBuf } ,
6+ } ;
67
78use logos:: Logos ;
89
9- use crate :: sfz:: types:: OpcodeMap ;
10- use crate :: sfz:: { Group , Header , Opcode , Region , SfzToken } ;
11- use crate :: Error ;
10+ use crate :: {
11+ error:: { Error , Result } ,
12+ sfz:: { types:: OpcodeMap , Group , Header , Opcode , Region , SfzToken } ,
13+ } ;
1214
1315/// Represents the SFZ instrument parsed
1416///
@@ -22,9 +24,18 @@ use crate::Error;
2224///
2325#[ derive( Debug ) ]
2426pub struct Instrument {
25- global : OpcodeMap , // these opcodes apply by default
26- groups : Vec < Group > , // these opcodes override those in global for current region
27- regions : Vec < Region > , // these opcodes override global, and their group ones
27+ /// The default opcodes for this instrument.
28+ pub global : OpcodeMap ,
29+
30+ /// The list of groups.
31+ ///
32+ /// The opcodes in a group overrides those in global, for the associated region.
33+ pub groups : Vec < Group > ,
34+
35+ /// The list of regions.
36+ ///
37+ /// The opcodes in a region overrides those in global and in its group.
38+ pub regions : Vec < Region > , // these opcodes override global, and their group ones
2839
2940 // maybe make this later a: struct Control
3041 // https://sfzformat.com/headers/control
@@ -64,7 +75,7 @@ impl Instrument {
6475
6576 /// Creates an Instrument via loading and parsing some SFZ code in a file
6677 ///
67- pub fn from_file ( sfz_path : & Path ) -> Result < Self , Error > {
78+ pub fn from_file ( sfz_path : & Path ) -> Result < Self > {
6879 // open sfz file, and read it into sfz_text
6980 let mut sfz_file = File :: open ( & sfz_path) ?;
7081 let mut sfz_text = String :: new ( ) ;
@@ -78,7 +89,7 @@ impl Instrument {
7889 /// sfz_path would be the root location from where to find the samples
7990 /// and default_path opcode value is appended to it.
8091 ///
81- pub fn from_sfz ( sfz : & str , sfz_path : & Path ) -> Result < Self , Error > {
92+ pub fn from_sfz ( sfz : & str , sfz_path : & Path ) -> Result < Self > {
8293 // Initializes an instrument for construction
8394 let mut instrument = Instrument {
8495 global : OpcodeMap :: new ( ) ,
@@ -137,7 +148,9 @@ impl Instrument {
137148 if status. are_regions ( ) {
138149 instrument. add_opcode_to_region ( o, status. region_counter . unwrap ( ) ) ?;
139150 instrument. set_region_group (
140- status. region_counter . unwrap ( ) , status. group_counter ) ?;
151+ status. region_counter . unwrap ( ) ,
152+ status. group_counter ,
153+ ) ?;
141154
142155 // an opcode for the <group>
143156 } else if status. are_groups ( ) {
@@ -157,11 +170,11 @@ impl Instrument {
157170 /// Add an opcode, depending on context, to either the last created region,
158171 /// the last created group, or the global header (in that priority order)
159172 ///
160- pub fn add_opcode ( & mut self , opcode : & Opcode ) -> Result < ( ) , Error > {
173+ pub fn add_opcode ( & mut self , opcode : & Opcode ) -> Result < ( ) > {
161174 match self . last_header_created {
162175 Header :: Global => Ok ( self . add_opcode_global ( opcode) ) ,
163- Header :: Group => self . add_opcode_to_group ( opcode, self . groups ( ) - 1 ) ,
164- Header :: Region => self . add_opcode_to_region ( opcode, self . regions ( ) - 1 ) ,
176+ Header :: Group => self . add_opcode_to_group ( opcode, self . groups ( ) - 1 ) ,
177+ Header :: Region => self . add_opcode_to_region ( opcode, self . regions ( ) - 1 ) ,
165178 _ => Err ( Error :: Generic ) ,
166179 }
167180 }
@@ -173,24 +186,26 @@ impl Instrument {
173186 }
174187
175188 /// Add an opcode to a group
176- pub fn add_opcode_to_group ( & mut self , opcode : & Opcode , group : usize ) -> Result < ( ) , Error > {
189+ pub fn add_opcode_to_group ( & mut self , opcode : & Opcode , group : usize ) -> Result < ( ) > {
177190 if group >= self . groups ( ) {
178- return Err ( Error :: OutOfBounds (
179- format ! [ "Tried to add an Opcode to Group `{0}`, but the last group is `{1}`" ,
180- group, self . groups( ) -1 ]
181- ) ) ;
191+ return Err ( Error :: OutOfBounds ( format ! [
192+ "Tried to add an Opcode to Group `{0}`, but the last group is `{1}`" ,
193+ group,
194+ self . groups( ) - 1
195+ ] ) ) ;
182196 }
183197 self . groups [ group] . add_opcode ( opcode) ;
184198 Ok ( ( ) )
185199 }
186200
187201 /// Add an opcode to a region
188- pub fn add_opcode_to_region ( & mut self , opcode : & Opcode , region : usize ) -> Result < ( ) , Error > {
202+ pub fn add_opcode_to_region ( & mut self , opcode : & Opcode , region : usize ) -> Result < ( ) > {
189203 if region >= self . regions ( ) {
190- return Err ( Error :: OutOfBounds (
191- format ! [ "Tried to add an Opcode to Region `{0}`, but the last region is `{1}`" ,
192- region, self . regions( ) -1 ]
193- ) ) ;
204+ return Err ( Error :: OutOfBounds ( format ! [
205+ "Tried to add an Opcode to Region `{0}`, but the last region is `{1}`" ,
206+ region,
207+ self . regions( ) - 1
208+ ] ) ) ;
194209 }
195210 self . regions [ region] . add_opcode ( opcode) ;
196211 Ok ( ( ) )
@@ -207,12 +222,13 @@ impl Instrument {
207222 }
208223
209224 /// Get the number of regions in a group
210- pub fn regions_in ( & self , group : usize ) -> Result < usize , Error > {
225+ pub fn regions_in ( & self , group : usize ) -> Result < usize > {
211226 if group >= self . groups ( ) {
212- return Err ( Error :: OutOfBounds (
213- format ! [ "There's no group `{0}`, the last group is `{1}`" ,
214- group, self . groups( ) -1 ]
215- ) ) ;
227+ return Err ( Error :: OutOfBounds ( format ! [
228+ "There's no group `{0}`, the last group is `{1}`" ,
229+ group,
230+ self . groups( ) - 1
231+ ] ) ) ;
216232 }
217233
218234 let mut count = 0 ;
@@ -252,12 +268,13 @@ impl Instrument {
252268 }
253269
254270 /// Set the group of a region (group can be None)
255- pub fn set_region_group ( & mut self , region : usize , group : Option < usize > ) -> Result < ( ) , Error > {
271+ pub fn set_region_group ( & mut self , region : usize , group : Option < usize > ) -> Result < ( ) > {
256272 if region >= self . regions ( ) {
257- return Err ( Error :: OutOfBounds (
258- format ! [ "Tried set the group of Region `{0}`, but the last region is `{1}`" ,
259- region, self . regions( ) -1 ]
260- ) ) ;
273+ return Err ( Error :: OutOfBounds ( format ! [
274+ "Tried set the group of Region `{0}`, but the last region is `{1}`" ,
275+ region,
276+ self . regions( ) - 1
277+ ] ) ) ;
261278 }
262279 if let Some ( g) = group {
263280 if g >= self . groups ( ) {
0 commit comments