ot_tools_io/projects/
metadata.rs

1/*
2SPDX-License-Identifier: GPL-3.0-or-later
3Copyright © 2024 Mike Robeson [dijksterhuis]
4*/
5
6//! Model and parsing of a project's OS metadata.
7//! Used in the [crate::projects::ProjectFile] type.
8
9use crate::RBoxErr;
10use serde::{Deserialize, Serialize};
11use std::collections::HashMap;
12
13use crate::projects::{
14    parse_hashmap_string_value, string_to_hashmap, ProjectFromString, ProjectRawFileSection,
15    ProjectToString,
16};
17
18/*
19Example data:
20[META]\r\nTYPE=OCTATRACK DPS-1 PROJECT\r\nVERSION=19\r\nOS_VERSION=R0177     1.40B\r\n[/META]
21------
22[META]
23TYPE=OCTATRACK DPS-1 PROJECT
24VERSION=19
25OS_VERSION=R0177     1.40B
26[/META]
27*/
28
29/// Project metadata read from a parsed Octatrack Project file
30#[derive(Serialize, Deserialize, PartialEq, Debug, Clone)]
31pub struct ProjectMetadata {
32    /// Type of file (always a 'project').
33    ///
34    /// Example ASCII data:
35    /// ```text
36    /// TYPE=OCTATRACK DPS-1 PROJECT
37    /// ```
38    pub filetype: String,
39
40    /// Unknown. Probably refers to an internal Elektron release version number.
41    ///
42    /// Example ASCII data:
43    /// ```text
44    /// VERSION=19
45    /// ```
46    pub project_version: u32,
47
48    /// Version of the Octatrack OS (that the project was created with?).
49    ///
50    /// Example ASCII data:
51    /// ```text
52    /// OS_VERSION=R0177     1.40B
53    /// ```
54    pub os_version: String,
55}
56
57impl Default for ProjectMetadata {
58    fn default() -> Self {
59        Self {
60            filetype: "OCTATRACK DPS-1 PROJECT".to_string(),
61            project_version: 19,
62            os_version: "R0177     1.40B".to_string(),
63        }
64    }
65}
66
67impl ProjectFromString for ProjectMetadata {
68    type T = Self;
69
70    /// Extract `OctatrackProjectMetadata` fields from the project file's ASCII data
71    fn from_string(data: &str) -> RBoxErr<Self> {
72        let hmap: HashMap<String, String> = string_to_hashmap(data, &ProjectRawFileSection::Meta)?;
73
74        Ok(Self {
75            filetype: parse_hashmap_string_value::<String>(&hmap, "type", None)?,
76            project_version: parse_hashmap_string_value::<u32>(&hmap, "version", None)?,
77            os_version: parse_hashmap_string_value::<String>(&hmap, "os_version", None)?,
78        })
79    }
80}
81
82impl ProjectToString for ProjectMetadata {
83    /// Extract `OctatrackProjectMetadata` fields from the project file's ASCII data
84    fn to_string(&self) -> RBoxErr<String> {
85        let mut s = "".to_string();
86        s.push_str("[META]\r\n");
87        s.push_str(format!("TYPE={}", self.filetype).as_str());
88        s.push_str("\r\n");
89        s.push_str(format!("VERSION={}", self.project_version).as_str());
90        s.push_str("\r\n");
91        s.push_str(format!("OS_VERSION={}", self.os_version).as_str());
92        s.push_str("\r\n[/META]");
93
94        Ok(s)
95    }
96}