Skip to content

Commit

Permalink
Add functionality to parse to/from data buffer
Browse files Browse the repository at this point in the history
  • Loading branch information
hedgecrw committed Nov 4, 2024
1 parent 16cc82b commit 647b3a1
Show file tree
Hide file tree
Showing 4 changed files with 209 additions and 48 deletions.
6 changes: 5 additions & 1 deletion Makefile
Original file line number Diff line number Diff line change
@@ -1,13 +1,17 @@
.PHONY: all clean docs lib format publish checknostd testunit

all:
$(error You must specify one of the following targets: clean docs lib format publish checknostd check testunit test_EXAMPLE)
$(error You must specify one of the following targets: clean setup docs lib format publish checknostd check testunit test_EXAMPLE)

clean:
cd ensure_no_std && cargo clean && rm -rf Cargo.lock
cargo clean
@rm -rf pkg target*

setup:
rustup target add wasm32-unknown-unknown x86_64-unknown-none
cargo install wasm-pack

docs:
RUSTDOCFLAGS="--extend-css musicxml/assets/docs.css" cargo doc --workspace --no-deps --release --exclude musicxml_internal --exclude musicxml_macros
cp -r musicxml/assets/* target/doc/musicxml/
Expand Down
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -60,4 +60,6 @@ This library is licensed under the [MIT license](http://opensource.org/licenses/

## TODO

Add documentation about "std" feature, add options to parse MusicXML files directly from string (both compressed and uncompressed), same for writing to string
- [ ] Add documentation about "std" feature, including parsing to/from a data buffer instead of a file
- [ ] Remove dependency on regex
- [ ] Create WASM build
64 changes: 64 additions & 0 deletions musicxml/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -148,22 +148,51 @@ use elements::{ScorePartwise, ScoreTimewise};
/// Reads a MusicXML file and returns a [ScorePartwise] object.
///
/// The specified file can be either a `.musicxml` file or a compressed `.mxl` file.
///
/// # Errors
/// TODO
pub fn read_score_partwise(path: &str) -> Result<ScorePartwise, String> {
parser::parse_score_partwise_from_file(path)
}

/// Reads a MusicXML file and returns a [ScoreTimewise] object.
///
/// The specified file can be either a `.musicxml` file or a compressed `.mxl` file.
///
/// # Errors
/// TODO
pub fn read_score_timewise(path: &str) -> Result<ScoreTimewise, String> {
parser::parse_score_timewise_from_file(path)
}

/// Reads MusicXML data and returns a [ScorePartwise] object.
///
/// The specified data should have been read directly from either a `.musicxml` file or a compressed `.mxl` file.
///
/// # Errors
/// TODO
pub fn read_score_data_partwise(data: Vec<u8>) -> Result<ScorePartwise, String> {
parser::parse_score_partwise_from_data(data)
}

/// Reads MusicXML data and returns a [ScoreTimewise] object.
///
/// The specified data should have been read directly from either a `.musicxml` file or a compressed `.mxl` file.
///
/// # Errors
/// TODO
pub fn read_score_data_timewise(data: Vec<u8>) -> Result<ScoreTimewise, String> {
parser::parse_score_timewise_from_data(data)
}

/// Writes a [ScorePartwise] object into a MusicXML file.
///
/// If the `compressed` parameter is set to `true`, the MusicXML file will be written as a compressed `.mxl` file.
/// If the `write_as_timewise` parameter is set to `true`, the MusicXML file will be converted into a timewise format and
/// written as a `<score-timewise>` element.
///
/// # Errors
/// TODO
pub fn write_partwise_score(
path: &str,
score: &ScorePartwise,
Expand All @@ -178,6 +207,9 @@ pub fn write_partwise_score(
/// If the `compressed` parameter is set to `true`, the MusicXML file will be written as a compressed `.mxl` file.
/// If the `write_as_partwise` parameter is set to `true`, the MusicXML file will be converted into a partwise format and
/// written as a `<score-partwise>` element.
///
/// # Errors
/// TODO
pub fn write_timewise_score(
path: &str,
score: &ScoreTimewise,
Expand All @@ -186,3 +218,35 @@ pub fn write_timewise_score(
) -> Result<(), String> {
parser::parse_score_timewise_to_file(path, score, compressed, true, write_as_partwise)
}

/// Writes a [ScorePartwise] object into a MusicXML data buffer.
///
/// If the `compressed` parameter is set to `true`, the MusicXML contents will be written as compressed `.mxl` data.
/// If the `write_as_timewise` parameter is set to `true`, the MusicXML contents will be converted into a timewise
/// format and written as a `<score-timewise>` element.
///
/// # Errors
/// TODO
pub fn write_partwise_score_data(
score: &ScorePartwise,
compressed: bool,
write_as_timewise: bool,
) -> Result<Vec<u8>, String> {
parser::parse_score_partwise_to_data(score, compressed, true, write_as_timewise)
}

/// Writes a [ScoreTimewise] object into a MusicXML data buffer.
///
/// If the `compressed` parameter is set to `true`, the MusicXML contents will be written as compressed `.mxl` data.
/// If the `write_as_partwise` parameter is set to `true`, the MusicXML contents will be converted into a partwise
/// format and written as a `<score-partwise>` element.
///
/// # Errors
/// TODO
pub fn write_timewise_score_data(
score: &ScoreTimewise,
compressed: bool,
write_as_partwise: bool,
) -> Result<Vec<u8>, String> {
parser::parse_score_timewise_to_data(score, compressed, true, write_as_partwise)
}
Loading

0 comments on commit 647b3a1

Please sign in to comment.