sim_lib_music_notation/
model.rs1use sim_kernel::{Diagnostic, Severity, SourceId, Span};
2use sim_lib_music_core::{MusicError, Score};
3use thiserror::Error;
4
5#[derive(Clone, Debug, PartialEq, Eq)]
11pub struct NotationReport<T> {
12 pub value: T,
14 pub diagnostics: Vec<Diagnostic>,
16}
17
18#[derive(Debug, Error, Clone, PartialEq, Eq)]
20pub enum NotationError {
21 #[error("unsupported duration {0}")]
23 UnsupportedDuration(String),
24 #[error("unsupported music object {0}")]
26 UnsupportedMusicObject(&'static str),
27 #[error("invalid key signature {0}")]
29 InvalidKey(String),
30 #[error("unsupported lilypond syntax")]
32 UnsupportedSyntax {
33 diagnostics: Vec<Diagnostic>,
35 },
36 #[error(transparent)]
38 Music(#[from] MusicError),
39}
40
41#[derive(Copy, Clone, Debug, Default, PartialEq, Eq)]
46pub struct NotationCodec;
47
48impl NotationCodec {
49 pub fn export_lilypond_report(
51 &self,
52 score: &Score,
53 ) -> Result<NotationReport<String>, NotationError> {
54 crate::export::export_lilypond_report(score)
55 }
56
57 pub fn export_lilypond(&self, score: &Score) -> Result<String, NotationError> {
59 crate::export::export_lilypond(score)
60 }
61
62 pub fn import_lilypond_report(
64 &self,
65 source: &str,
66 ) -> Result<NotationReport<Score>, NotationError> {
67 crate::import::import_lilypond_report(source)
68 }
69
70 pub fn import_lilypond(&self, source: &str) -> Result<Score, NotationError> {
72 crate::import::import_lilypond(source)
73 }
74}
75
76pub(crate) fn error_at(message: impl Into<String>, span: Span) -> NotationError {
77 NotationError::UnsupportedSyntax {
78 diagnostics: vec![Diagnostic {
79 severity: Severity::Error,
80 message: message.into(),
81 source: Some(SourceId("notation:lilypond".to_owned())),
82 span: Some(span),
83 code: None,
84 related: Vec::new(),
85 }],
86 }
87}