wolfrpg_map_parser/db_parser/parsers/
project_parser.rs

1use crate::byte_utils::as_u32_le;
2use crate::db_parser::models::type_info::TypeInfo;
3use std::fs;
4use std::io::Result;
5use std::path::Path;
6
7/// Parse a .project file containing information on a WolfRPG Editor internal database.
8///
9/// Returns the schema of a database table. For the actual data, use [`data_parser::parse`]
10/// If you already have read the bytes, consider using [`parse_bytes`].
11/// 
12/// [`data_parser::parse`]: crate::data_parser::parse
13pub fn parse(project: &Path) -> Result<Vec<TypeInfo>> {
14    match fs::read(project) {
15        Ok(contents) => {
16            Ok(parse_bytes(&contents))
17        }
18        Err(e) => {
19            Err(e)
20        }
21    }
22}
23
24
25/// Parse bytes containing information on a WolfRPG Editor internal database.
26///
27/// Returns the schema of a database table. For the actual data, use [`data_parser::parse_bytes`]
28/// If you already have read the bytes, consider using [`parse`].
29///
30/// [`data_parser::parse_bytes`]: crate::data_parser::parse_bytes
31pub fn parse_bytes(bytes: &[u8]) -> Vec<TypeInfo> {
32    let mut offset: usize = 0;
33    
34    let type_count: usize = as_u32_le(bytes) as usize;
35    offset += 4;
36    
37    let mut types: Vec<TypeInfo> = Vec::with_capacity(type_count);
38    
39    for i in 0..type_count {
40        let (bytes_read, type_info): (usize, TypeInfo) = TypeInfo::parse(&bytes[offset..], i);
41        offset += bytes_read;
42        types.push(type_info);
43    }
44    
45    types
46}