pub trait OctatrackFileIO: Serialize + for<'a> Deserialize<'a> {
Show 15 methods
// Provided methods
fn repr(&self, newlines: Option<bool>)
where Self: Debug { ... }
fn from_data_file(path: &Path) -> Result<Self, OtToolsIoError> { ... }
fn from_bytes(bytes: &[u8]) -> Result<Self, OtToolsIoError> { ... }
fn to_data_file(&self, path: &Path) -> Result<(), OtToolsIoError> { ... }
fn to_bytes(&self) -> Result<Vec<u8>, OtToolsIoError> { ... }
fn from_yaml_file(path: &Path) -> Result<Self, OtToolsIoError> { ... }
fn from_yaml_str(yaml: &str) -> Result<Self, OtToolsIoError> { ... }
fn to_yaml_file(&self, path: &Path) -> Result<(), OtToolsIoError> { ... }
fn to_yaml_string(&self) -> Result<String, OtToolsIoError> { ... }
fn from_json_file(path: &Path) -> Result<Self, OtToolsIoError> { ... }
fn from_json_str(json: &str) -> Result<Self, OtToolsIoError> { ... }
fn to_json_file(&self, path: &Path) -> Result<(), OtToolsIoError> { ... }
fn to_json_string(&self) -> Result<String, OtToolsIoError> { ... }
fn encode(&self) -> Result<Vec<u8>, OtToolsIoError>
where Self: Serialize { ... }
fn decode(bytes: &[u8]) -> Result<Self, OtToolsIoError>
where Self: Sized + for<'a> Deserialize<'a> { ... }
}Expand description
Convenience trait for types which directly correspond to Elektron Octatrack binary data files.
Associated functions and methods etc. for File I/O, plus a repr method for debugging.
Provided Methods§
Sourcefn repr(&self, newlines: Option<bool>)where
Self: Debug,
fn repr(&self, newlines: Option<bool>)where
Self: Debug,
Read type from an Octatrack data file at path
use ot_tools_io::{BankFile, OctatrackFileIO};
// no newlines
BankFile::from_data_file(&path).unwrap().repr(None);
// no newlines
BankFile::from_data_file(&path).unwrap().repr(Some(false));
// with newlines
BankFile::from_data_file(&path).unwrap().repr(Some(true));Examples found in repository?
More examples
Sourcefn from_data_file(path: &Path) -> Result<Self, OtToolsIoError>
fn from_data_file(path: &Path) -> Result<Self, OtToolsIoError>
Read type from an Octatrack data file at path
use ot_tools_io::{BankFile, OctatrackFileIO};
let bank = BankFile::from_data_file(&path).unwrap();
assert_eq!(bank.datatype_version, 23);Examples found in repository?
More examples
12fn arrangement_check() -> Result<(), OtToolsIoError> {
13 let fp = PathBuf::from("test-data")
14 .join("arrange")
15 .join("full-options.work");
16 let bin = ArrangementFile::from_data_file(&fp)?;
17 println!("checksum from arrangement file: {:?}", bin.checksum);
18
19 let checksum = bin.calculate_checksum()?;
20 println!("calculated checksum for arrangement file: {checksum:?}");
21
22 if bin.check_checksum()? {
23 println!("arrangement file checksum is ok");
24 } else {
25 println!("bad arrangement file checksum");
26 panic!()
27 };
28 Ok(())
29}
30
31fn bank_check() -> Result<(), OtToolsIoError> {
32 let fp = PathBuf::from("test-data")
33 .join("blank-project")
34 .join("bank01.work");
35 let bin = BankFile::from_data_file(&fp)?;
36 println!("checksum from bank file: {:?}", bin.checksum);
37
38 let checksum = bin.calculate_checksum()?;
39 println!("calculated checksum for bank file: {checksum:?}");
40
41 if bin.check_checksum()? {
42 println!("bank file checksum is ok");
43 } else {
44 println!("bad bank file checksum");
45 panic!()
46 };
47 Ok(())
48}
49
50fn markers_check() -> Result<(), OtToolsIoError> {
51 let fp = PathBuf::from("test-data")
52 .join("blank-project")
53 .join("markers.work");
54 let bin = ArrangementFile::from_data_file(&fp)?;
55 println!("checksum from markers file: {:?}", bin.checksum);
56
57 let checksum = bin.calculate_checksum()?;
58 println!("calculated checksum for markers file: {checksum:?}");
59
60 if bin.check_checksum()? {
61 println!("markers file checksum is ok");
62 } else {
63 println!("bad markers file checksum");
64 panic!()
65 };
66
67 Ok(())
68}
69
70fn sample_check() -> Result<(), OtToolsIoError> {
71 let fp = PathBuf::from("test-data").join("samples").join("sample.ot");
72 let bin = SampleSettingsFile::from_data_file(&fp)?;
73 println!("checksum from sample settings file: {:?}", bin.checksum);
74
75 let checksum = bin.calculate_checksum()?;
76 println!("calculated checksum for sample settings file: {checksum:?}");
77
78 if bin.check_checksum()? {
79 println!("sample settings file checksum is ok");
80 } else {
81 println!("bad sample settings file checksum");
82 panic!()
83 };
84 Ok(())
85}9fn main() -> Result<(), OtToolsIoError> {
10 let blank_project_dirpath = PathBuf::from("test-data").join("blank-project");
11 let project_fpath = blank_project_dirpath.join("project.work");
12 let project = ProjectFile::from_data_file(&project_fpath)?;
13
14 println!("====================================");
15 println!("| Project Sample Slots |");
16 println!("====================================");
17 println!("-------------- Static --------------");
18 for (idx, sample_slot) in project.slots.static_slots.iter().enumerate() {
19 println!("Static Slot {}: {sample_slot:?}", idx + 1)
20 }
21 println!("--------------- Flex ---------------");
22 for (idx, sample_slot) in project.slots.flex_slots.iter().enumerate() {
23 println!("Flex Slot {}: {sample_slot:?}", idx + 1)
24 }
25 Ok(())
26}Sourcefn from_bytes(bytes: &[u8]) -> Result<Self, OtToolsIoError>
fn from_bytes(bytes: &[u8]) -> Result<Self, OtToolsIoError>
Read type from bytes
// need to create everything from scratch in this example as many bytes otherwise
use serde::{Deserialize, Serialize};
use ot_tools_io::{OctatrackFileIO, OtToolsIoError};
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Something {
value: u8,
}
impl OctatrackFileIO for Something {}
let x = [10];
let r = Something::from_bytes(&x);
assert!(r.is_ok());
assert_eq!(r.unwrap(), Something { value: 10});Sourcefn to_data_file(&self, path: &Path) -> Result<(), OtToolsIoError>
fn to_data_file(&self, path: &Path) -> Result<(), OtToolsIoError>
Write type to an Octatrack data file at path
use ot_tools_io::{OctatrackFileIO, BankFile};
let r = BankFile::default().to_data_file(&path);
assert!(r.is_ok());
assert!(path.exists());Examples found in repository?
19fn main() -> Result<(), OtToolsIoError> {
20 let project_dirpath = PathBuf::from("example-new-project");
21
22 std::fs::create_dir_all(&project_dirpath)?;
23
24 let proj_fpath = project_dirpath.join("project.work");
25 ProjectFile::default().to_data_file(&proj_fpath)?;
26
27 let markers_fpath = project_dirpath.join("markers.work");
28 MarkersFile::default().to_data_file(&markers_fpath)?;
29
30 for i in 1..=16 {
31 let bank_fpath = project_dirpath.join(bank_fname_from_id(i));
32 BankFile::default().to_data_file(&bank_fpath)?;
33 }
34
35 for i in 1..=8 {
36 let arr_fpath = project_dirpath.join(arr_fname_from_id(i));
37 ArrangementFile::default().to_data_file(&arr_fpath)?;
38 }
39
40 println!("New project created: {project_dirpath:?}");
41 Ok(())
42}More examples
19fn new_proj(banks: &[BankFile], proj_id: u32) -> Result<(), OtToolsIoError> {
20 let project_dirpath = PathBuf::from("dbg").join(format!["PROJECT-{proj_id}"]);
21
22 std::fs::create_dir_all(&project_dirpath)?;
23
24 let proj_fpath = project_dirpath.join("project.work");
25 ProjectFile::default().to_data_file(&proj_fpath)?;
26
27 let markers_fpath = project_dirpath.join("markers.work");
28 MarkersFile::default().to_data_file(&markers_fpath)?;
29
30 for (idx, bank) in banks.iter().enumerate() {
31 let bank_fpath = project_dirpath.join(bank_fname_from_id(idx + 1));
32 bank.to_data_file(&bank_fpath)?;
33 }
34
35 for i in 1..=8 {
36 let arr_fpath = project_dirpath.join(arr_fname_from_id(i));
37 ArrangementFile::default().to_data_file(&arr_fpath)?;
38 }
39
40 println!("New project created: {project_dirpath:?}");
41 Ok(())
42}Sourcefn to_bytes(&self) -> Result<Vec<u8>, OtToolsIoError>
fn to_bytes(&self) -> Result<Vec<u8>, OtToolsIoError>
Create bytes from type
// need to create everything from scratch in this example as many bytes otherwise
use serde::{Deserialize, Serialize};
use ot_tools_io::OctatrackFileIO;
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Something {
value: u8,
}
impl OctatrackFileIO for Something {}
let x = Something { value: 10 };
let r = x.to_bytes();
assert!(r.is_ok());
assert_eq!(r.unwrap(), [10]);Sourcefn from_yaml_file(path: &Path) -> Result<Self, OtToolsIoError>
fn from_yaml_file(path: &Path) -> Result<Self, OtToolsIoError>
Read type from a YAML file at path
use ot_tools_io::{ProjectFile, OctatrackFileIO};
let project = ProjectFile::from_yaml_file(&path).unwrap();
assert_eq!(project.metadata.project_version, 19);Examples found in repository?
More examples
Sourcefn from_yaml_str(yaml: &str) -> Result<Self, OtToolsIoError>
fn from_yaml_str(yaml: &str) -> Result<Self, OtToolsIoError>
Read type from YAML string
// need to create everything from scratch in this example
use serde::{Deserialize, Serialize};
use ot_tools_io::OctatrackFileIO;
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Something {
value: u8,
}
impl OctatrackFileIO for Something {}
let r = Something::from_yaml_str("value: 10\n");
assert!(r.is_ok());
assert_eq!(r.unwrap(), Something { value: 10 });Sourcefn to_yaml_file(&self, path: &Path) -> Result<(), OtToolsIoError>
fn to_yaml_file(&self, path: &Path) -> Result<(), OtToolsIoError>
Write type to a YAML file at path
use ot_tools_io::{OctatrackFileIO, BankFile};
let r = BankFile::default().to_yaml_file(&path);
assert!(r.is_ok());
assert!(path.exists());Sourcefn to_yaml_string(&self) -> Result<String, OtToolsIoError>
fn to_yaml_string(&self) -> Result<String, OtToolsIoError>
Create YAML string from type
use ot_tools_io::{BankFile, OctatrackFileIO};
let bank = BankFile::default().to_yaml_string().unwrap();
assert_eq!(bank.len(), 12532024);
assert_eq!(bank[0..15], "header:\n- 70\n- ".to_string());Sourcefn from_json_file(path: &Path) -> Result<Self, OtToolsIoError>
fn from_json_file(path: &Path) -> Result<Self, OtToolsIoError>
Read type from a JSON file at path
Sourcefn from_json_str(json: &str) -> Result<Self, OtToolsIoError>
fn from_json_str(json: &str) -> Result<Self, OtToolsIoError>
Create type from JSON string
// need to create everything from scratch in this example
use serde::{Deserialize, Serialize};
use ot_tools_io::OctatrackFileIO;
#[derive(Serialize, Deserialize, Debug, PartialEq)]
struct Something {
value: u8,
}
impl OctatrackFileIO for Something {}
let r = Something::from_json_str("{\"value\":10}");
assert!(r.is_ok());
assert_eq!(r.unwrap(), Something { value: 10 });Sourcefn to_json_file(&self, path: &Path) -> Result<(), OtToolsIoError>
fn to_json_file(&self, path: &Path) -> Result<(), OtToolsIoError>
Write type to a JSON file at path
use ot_tools_io::{OctatrackFileIO, BankFile};
let r = BankFile::default().to_json_file(&path);
assert!(r.is_ok());
assert!(path.exists());Sourcefn to_json_string(&self) -> Result<String, OtToolsIoError>
fn to_json_string(&self) -> Result<String, OtToolsIoError>
Create JSON string from type
use ot_tools_io::{BankFile, OctatrackFileIO};
let json = BankFile::default().to_json_string().unwrap();
assert_eq!(json.len(), 7761363);
assert_eq!(json[0..15], "{\"header\":[70,7".to_string());Sourcefn encode(&self) -> Result<Vec<u8>, OtToolsIoError>where
Self: Serialize,
fn encode(&self) -> Result<Vec<u8>, OtToolsIoError>where
Self: Serialize,
Adds serialization via bincode and serde to a type. Must be present on all major file
types.
use std::error::Error;
use ot_tools_io::OctatrackFileIO;
#[derive(serde::Deserialize, serde::Serialize, std::fmt::Debug)]
struct SomeType {
x: u8,
y: u32,
}
// default implementation
impl OctatrackFileIO for SomeType {}
let x = SomeType { x : 8, y: 32857983 };
let encoded = x.encode().unwrap();
assert_eq!(
vec![8, 127, 95, 245, 1],
encoded,
);Sourcefn decode(bytes: &[u8]) -> Result<Self, OtToolsIoError>where
Self: Sized + for<'a> Deserialize<'a>,
fn decode(bytes: &[u8]) -> Result<Self, OtToolsIoError>where
Self: Sized + for<'a> Deserialize<'a>,
Deserialization via bincode and serde.
use std::error::Error;
use ot_tools_io::{OctatrackFileIO, OtToolsIoError};
#[derive(serde::Deserialize, serde::Serialize, std::fmt::Debug, PartialEq)]
struct SomeType {
x: u8,
y: u32,
}
// default implementation
impl OctatrackFileIO for SomeType {}
let bytes: Vec<u8> = vec![8, 127, 95, 245, 1];
let decoded = SomeType::decode(&bytes).unwrap();
assert_eq!(
SomeType { x : 8, y: 32857983 },
decoded,
);Dyn Compatibility§
This trait is not dyn compatible.
In older versions of Rust, dyn compatibility was called "object safety", so this trait is not object safe.