Skip to content

Commit 543c607

Browse files
committed
rustfmt; +exports; -dep; bump
- format code with rustfmt - add some additionaly type exports - remove unneeded dependency - bump version
1 parent 07d4ce4 commit 543c607

File tree

12 files changed

+237
-250
lines changed

12 files changed

+237
-250
lines changed

Cargo.toml

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22
name = "sofiza"
33
description = "SFZ format parser"
4-
version = "0.1.0"
4+
version = "0.1.1"
55
edition = "2018"
66
authors = ["José Luis Cruz <joseluis@andamira.net>"]
77
repository = "https://github.com/andamira/sofiza"
@@ -21,7 +21,6 @@ logos = "0.11.4"
2121
regex = "1"
2222

2323
shellexpand = "1.1.1"
24-
path-slash = "0.1.3"
2524

2625
phf = { version = "0.8", features = ["macros"] }
2726

src/lib.rs

Lines changed: 4 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,12 +1,10 @@
11
pub mod sfz;
22

33
pub use sfz::{
4+
group::Group,
5+
headers::Header,
46
instrument::Instrument,
5-
6-
// headers::Header,
7-
// group::Group,
8-
// region::Region,
97
opcodes::Opcode,
10-
//
11-
// types::{loop_mode, trigger, fil_type, UndefinedInteger, UndefinedUnsignedInteger, UnknownType},
8+
region::Region,
9+
types::{fil_type, loop_mode, trigger},
1210
};

src/sfz/group.rs

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -3,15 +3,14 @@ use std::fmt::Debug;
33

44
use crate::sfz::{Opcode, OpcodeMap};
55

6-
76
/// Groups allow entering common parameters for multiple regions.
87
///
98
/// A group is defined with the <group> opcode, and the parameters enumerated
109
/// on it last till the next group opcode, or till the end of the file.
1110
///
1211
#[derive(Debug)]
1312
pub struct Group {
14-
opcodes: OpcodeMap, // these opcodes overwrites the defaults, and the inherited
13+
opcodes: OpcodeMap, // these opcodes overwrites the defaults, and the inherited
1514
label: String,
1615
}
1716

@@ -27,4 +26,3 @@ impl Group {
2726
self.opcodes.insert(o.str_name(), o.clone());
2827
}
2928
}
30-

src/sfz/headers.rs

Lines changed: 11 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use logos::Lexer;
22

33
use crate::sfz::SfzToken;
44

5-
65
/// SFZ files are subdivided into sections by headers.
76
///
87
/// The global/group/region or global/master/group/region hierarchy contains the opcodes
@@ -12,9 +11,7 @@ use crate::sfz::SfzToken;
1211
1312
#[derive(Debug, PartialEq)]
1413
pub enum Header {
15-
1614
// sfz v1 headers
17-
1815
/// The basic component of an instrument. An instrument is defined by one or more regions.
1916
///
2017
/// - version: v1
@@ -28,7 +25,6 @@ pub enum Header {
2825
Group,
2926

3027
// sfz v2 headers:
31-
3228
/// The control header would be found at the beginning of the file
3329
/// and includes special opcodes for setting up MIDI CC controls.
3430
///
@@ -63,7 +59,6 @@ pub enum Header {
6359
Effect,
6460

6561
// aria headers:
66-
6762
/// The master header is an extra level added inbetween group and global for the ARIA player.
6863
///
6964
/// - version: aria extension
@@ -78,32 +73,30 @@ pub enum Header {
7873
Midi,
7974

8075
// cakewalk headers:
81-
8276
/// Allows to embed sample data directly in SFZ files (Rapture).
8377
///
8478
/// - version: cakewalk extension
8579
/// - info: [sample](https://sfzformat.com/headers/sample)
8680
Sample,
8781
}
8882

89-
9083
impl Header {
9184
/// Parses the opcode, making it lowercase and removing the brackets
9285
///
9386
pub(crate) fn parse_header(lex: &mut Lexer<SfzToken>) -> Option<Header> {
9487
let slice = lex.slice().to_ascii_lowercase();
95-
let name = &slice[1..slice.len()-1];
88+
let name = &slice[1..slice.len() - 1];
9689
match name {
97-
"region" => Some(Header::Region),
98-
"group" => Some(Header::Group),
99-
"control" => Some(Header::Control),
100-
"global" => Some(Header::Global),
101-
"curve" => Some(Header::Curve),
102-
"effect" => Some(Header::Effect),
103-
"master" => Some(Header::Master),
104-
"midi" => Some(Header::Midi),
105-
"sample" => Some(Header::Sample),
106-
_ => None,
90+
"region" => Some(Header::Region),
91+
"group" => Some(Header::Group),
92+
"control" => Some(Header::Control),
93+
"global" => Some(Header::Global),
94+
"curve" => Some(Header::Curve),
95+
"effect" => Some(Header::Effect),
96+
"master" => Some(Header::Master),
97+
"midi" => Some(Header::Midi),
98+
"sample" => Some(Header::Sample),
99+
_ => None,
107100
}
108101
}
109102
}

src/sfz/instrument.rs

Lines changed: 24 additions & 42 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,13 @@
1+
use std::fmt::Debug;
12
use std::fs::File;
2-
use std::io::{Error, prelude::*};
3+
use std::io::{prelude::*, Error};
34
use std::path::{Path, PathBuf};
45
use std::result::Result;
5-
use std::fmt::Debug;
66

77
use logos::Logos;
88

9-
use crate::sfz::{Group, Header, Opcode, Region, SfzToken};
109
use 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)]
2323
pub 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-
3433
impl 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)]
147134
struct 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
}

src/sfz/mod.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,9 +4,9 @@
44
//! Multiple regions can be arranged in a group.
55
//! Groups allow entering common parameters for multiple regions.
66
7+
pub mod group;
78
pub mod headers;
89
pub mod instrument;
9-
pub mod group;
1010
pub mod opcodes;
1111
pub mod region;
1212

src/sfz/opcodes/defaults.rs

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@ use phf::phf_map;
22

33
use crate::sfz::types::{fil_type, trigger, OpcodeType};
44

5-
65
/// This map returns the optional default value of an opcode type
76
pub(crate) static OPCODE_DEFAULT: phf::Map<&'static str, OpcodeType> = phf_map! {
87

0 commit comments

Comments
 (0)