ligen_ir/library/
mod.rs

1//! Library representation.
2
3pub mod metadata;
4use is_tree::IsTree;
5pub use metadata::*;
6
7use crate::Identifier;
8use crate::Module;
9use crate::prelude::*;
10
11/// Library representation.
12#[allow(missing_docs)]
13#[derive(Debug, Default, Clone, Serialize, Deserialize, PartialEq, IsTree)]
14pub struct Library {
15    #[tree(path_segment)]
16    pub identifier: Identifier,
17    pub metadata: Metadata,
18    #[tree(branch)]
19    pub root_module: Module,
20}
21
22impl Library {
23    /// Save library to file.
24    pub fn save(&self, path: impl AsRef<std::path::Path>) -> Result<()> {
25        let contents = serde_json::to_string_pretty(self)?;
26        std::fs::write(path, contents)?;
27        Ok(())
28    }
29
30    /// Load library from file.
31    pub fn load(path: impl AsRef<std::path::Path>) -> Result<Self> {
32        let json = std::fs::read_to_string(path)?;
33        Ok(serde_json::from_str(&json)?)
34    }
35}