fluidlite/
types.rs

1use std::{
2    error::Error as StdError,
3    fmt::{Display, Formatter, Result as FmtResult},
4    result::Result as StdResult,
5};
6
7/// Channel number
8pub type Chan = u32;
9
10/// Key number
11pub type Key = u32;
12
13/// Velocity value
14pub type Vel = u32;
15
16/// Control number
17pub type Ctrl = u32;
18
19/// Control value
20pub type Val = u32;
21
22/// Program number (`0..=127`)
23pub type Prog = u32;
24
25/// Bank number (`0..=127`)
26pub type Bank = u32;
27
28/// Font Id
29pub type FontId = u32;
30
31/// Preset Id
32pub type PresetId = u32;
33
34/// Generic result type
35pub type Result<T> = StdResult<T, Error>;
36
37/// Result without value
38pub type Status = Result<()>;
39
40/// Common error type
41#[derive(Debug, Clone, PartialEq, Eq, Hash)]
42pub enum Error {
43    Alloc,
44    Fluid(String),
45    Path,
46}
47
48impl StdError for Error {}
49
50impl Display for Error {
51    fn fmt(&self, f: &mut Formatter) -> FmtResult {
52        use self::Error::*;
53        match self {
54            Alloc => "Allocation error".fmt(f),
55            Fluid(error) => {
56                "Fluidlite error: ".fmt(f)?;
57                error.fmt(f)
58            }
59            Path => "Invalid path".fmt(f),
60        }
61    }
62}
63
64pub(crate) fn result_from_ptr<T>(ptr: *mut T) -> Result<*mut T> {
65    if ptr.is_null() {
66        Err(Error::Alloc)
67    } else {
68        Ok(ptr)
69    }
70}
71
72pub(crate) fn option_from_ptr<T>(ptr: *mut T) -> Option<*mut T> {
73    if ptr.is_null() {
74        None
75    } else {
76        Some(ptr)
77    }
78}