Skip to content

Commit 28cee10

Browse files
committed
make fields public; minor refactoring; version bump
- make fields public for: Instrument, Group and Region. - add doc comments for the fields. - make the error module public. - create custom Result type. - rename guitar example to parse. - version bump
1 parent 8861de3 commit 28cee10

File tree

8 files changed

+71
-55
lines changed

8 files changed

+71
-55
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
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.3"
4+
version = "0.2.0"
55
edition = "2018"
66
authors = ["José Luis Cruz <joseluis@andamira.net>"]
77
repository = "https://github.com/andamira/sofiza"
File renamed without changes.

src/errors.rs renamed to src/error.rs

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,8 @@
11
use thiserror::Error;
22

3-
/// enumerates all possible errors returned by this library
3+
/// All the possible errors returned by this library.
44
#[derive(Error, Debug)]
55
pub enum Error {
6-
76
// (TBD more specific)
87
#[error("generic error")]
98
Generic,
@@ -14,9 +13,10 @@ pub enum Error {
1413
/// Represents all other cases of `std::io::Error`.
1514
#[error(transparent)]
1615
IOError(#[from] std::io::Error),
17-
1816
}
1917

18+
/// The more concise Result type used by this library.
19+
pub type Result<T> = std::result::Result<T, Error>;
2020

2121
#[cfg(test)]
2222
mod tests_error {
@@ -30,5 +30,4 @@ mod tests_error {
3030
fn test_error() {
3131
assert("generic error", Error::Generic);
3232
}
33-
3433
}

src/lib.rs

Lines changed: 2 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,8 @@
1-
mod errors;
1+
pub mod error;
22
pub(crate) mod sfz;
33
pub(crate) mod utils;
44

55
pub use sfz::{
6-
Group,
7-
Header,
8-
Instrument,
9-
Opcode,
10-
Region,
116
types::{fil_type, loop_mode, trigger},
7+
Group, Header, Instrument, Opcode, Region,
128
};
13-
14-
pub use errors::Error;

src/sfz/group.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -10,8 +10,11 @@ use crate::sfz::{Opcode, OpcodeMap};
1010
///
1111
#[derive(Debug)]
1212
pub struct Group {
13-
opcodes: OpcodeMap, // these opcodes overwrites the defaults, and the inherited
14-
label: String,
13+
/// This list of opcodes overwrites the default ones.
14+
pub opcodes: OpcodeMap,
15+
16+
/// The label of this group.
17+
pub label: String,
1518
}
1619

1720
impl Group {

src/sfz/instrument.rs

Lines changed: 54 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,16 @@
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

78
use 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)]
2426
pub 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() {

src/sfz/region.rs

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -22,8 +22,11 @@ use crate::sfz::{Opcode, OpcodeMap};
2222
///
2323
#[derive(Debug)]
2424
pub struct Region {
25-
group: Option<usize>, // inherits the opcodes in this group
26-
opcodes: OpcodeMap, // these opcodes overwrites the defaults, and the inherited
25+
/// The opcodes of this group are applied and will override the defaults.
26+
pub group: Option<usize>,
27+
28+
/// This list of opcodes will override both the default and inherited opcodes.
29+
pub opcodes: OpcodeMap,
2730
}
2831

2932
impl Region {

src/utils.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,5 +5,5 @@
55
mod misc;
66
mod parse;
77

8-
pub(crate) use misc::{print_type, fix_path_separators};
8+
pub(crate) use misc::{fix_path_separators, print_type};
99
pub(crate) use parse::*;

0 commit comments

Comments
 (0)