|
| 1 | +//! Metadata installation stage. |
| 2 | +
|
| 3 | +use std::fs::File; |
| 4 | +use std::path::{Path, PathBuf}; |
| 5 | + |
| 6 | +use super::{serde, Context, EventError}; |
| 7 | + |
| 8 | + |
| 9 | +/// This stage loads the hierarchy of version metadata, this acts as a state-machine and |
| 10 | +/// will iteratively resolve version metadata and returns event. |
| 11 | +#[derive(Debug)] |
| 12 | +pub struct MetadataLoader<'ctx> { |
| 13 | + context: &'ctx Context, |
| 14 | + next: Option<Next>, |
| 15 | +} |
| 16 | + |
| 17 | +/// Internal description of the next version to resolve. |
| 18 | +#[derive(Debug)] |
| 19 | +struct Next { |
| 20 | + id: String, |
| 21 | + file: Option<PathBuf>, |
| 22 | +} |
| 23 | + |
| 24 | +impl<'ctx> MetadataLoader<'ctx> { |
| 25 | + |
| 26 | + /// Create a new metadata installer with the given context for the given version id. |
| 27 | + pub fn new(context: &'ctx Context, id: String) -> Self { |
| 28 | + Self { |
| 29 | + context, |
| 30 | + next: Some(Next { |
| 31 | + id, |
| 32 | + file: None, |
| 33 | + }), |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + /// Advance this |
| 38 | + pub fn advance(&mut self) -> MetadataEvent<'_> { |
| 39 | + |
| 40 | + let Some(next) = &mut self.next else { |
| 41 | + return MetadataEvent::Done |
| 42 | + }; |
| 43 | + |
| 44 | + let Some(file) = &next.file else { |
| 45 | + let file = next.file.insert(self.context.version_file(&next.id, "json")); |
| 46 | + return MetadataEvent::Loading { |
| 47 | + id: &next.id, |
| 48 | + file: &file, |
| 49 | + }; |
| 50 | + }; |
| 51 | + |
| 52 | + /// Read version metadata and wrap event error if relevant. |
| 53 | + fn read_metadata(file: &Path) -> Result<serde::VersionMetadata, EventError> { |
| 54 | + |
| 55 | + let metadata_reader = File::open(&file) |
| 56 | + .map_err(EventError::Io)?; |
| 57 | + |
| 58 | + serde_path_to_error::deserialize(&mut serde_json::Deserializer::from_reader(metadata_reader)) |
| 59 | + .map_err(EventError::Json) |
| 60 | + |
| 61 | + } |
| 62 | + |
| 63 | + // Use the wrapper and reset state to "version loading" in case of error to allow |
| 64 | + // fixing the issue. |
| 65 | + let metadata = match read_metadata(&file) { |
| 66 | + Ok(metadata) => metadata, |
| 67 | + Err(error) => { |
| 68 | + return MetadataEvent::LoadingFailed { |
| 69 | + id: &next.id, |
| 70 | + file: &file, |
| 71 | + error, |
| 72 | + } |
| 73 | + } |
| 74 | + }; |
| 75 | + |
| 76 | + // Take next entry to own the id. |
| 77 | + let next = self.next.take().unwrap(); |
| 78 | + |
| 79 | + // We start by changing the current state to load the inherited metadata. |
| 80 | + // If there is no inherited version, we advance to assets state. |
| 81 | + if let Some(next_version_id) = &metadata.inherits_from { |
| 82 | + |
| 83 | + } else { |
| 84 | + |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | + |
| 89 | +} |
| 90 | + |
| 91 | +#[derive(Debug)] |
| 92 | +pub enum MetadataEvent<'a> { |
| 93 | + /// A version is being loaded. |
| 94 | + Loading { |
| 95 | + id: &'a str, |
| 96 | + file: &'a Path, |
| 97 | + }, |
| 98 | + /// Parsing of the version JSON failed, the step can be retrieve indefinitely and can |
| 99 | + /// be fixed by writing a valid file at the path, if the error underlying error is |
| 100 | + /// recoverable (file not found, syntax error). |
| 101 | + LoadingFailed { |
| 102 | + id: &'a str, |
| 103 | + file: &'a Path, |
| 104 | + error: EventError, |
| 105 | + }, |
| 106 | + /// A version has been loaded from its JSON definition, it is possible to modify the |
| 107 | + /// metadata before releasing borrowing and advancing installation. |
| 108 | + Loaded { |
| 109 | + id: String, |
| 110 | + metadata: Box<serde::VersionMetadata>, |
| 111 | + }, |
| 112 | + /// There are no more metadata to iteratively load. |
| 113 | + Done, |
| 114 | +} |
0 commit comments