Skip to main content

rpfm_lib/files/unknown/
mod.rs

1//---------------------------------------------------------------------------//
2// Copyright (c) 2017-2026 Ismael Gutiérrez González. All rights reserved.
3//
4// This file is part of the Rusted PackFile Manager (RPFM) project,
5// which can be found here: https://github.com/Frodo45127/rpfm.
6//
7// This file is licensed under the MIT license, which can be found here:
8// https://github.com/Frodo45127/rpfm/blob/master/LICENSE.
9//---------------------------------------------------------------------------//
10
11//! Wildcard handler for unsupported or unrecognized file types.
12//!
13//! This module provides the [`Unknown`] type, which acts as a passthrough for files that
14//! don't have dedicated parsers in rpfm_lib. It stores the raw binary data without attempting
15//! to decode it, allowing safe manipulation of any file type.
16//!
17//! # Use Cases
18//!
19//! - Working with file types that don't have dedicated support
20//! - Reading and re-saving files without modification
21//! - Custom processing of binary data outside rpfm_lib
22//! - Placeholder for future file type implementations
23//!
24//! # Example
25//!
26//! ```ignore
27//! use rpfm_lib::files::{Decodeable, Encodeable, unknown::Unknown, DecodeableExtraData, EncodeableExtraData};
28//! use std::io::Cursor;
29//!
30//! // Read arbitrary binary data
31//! let data = vec![0x01, 0x02, 0x03, 0x04];
32//! let mut reader = Cursor::new(data.clone());
33//! let unknown = Unknown::decode(&mut reader, &None).unwrap();
34//!
35//! // Access raw data
36//! assert_eq!(unknown.data(), &data);
37//! ```
38
39use getset::*;
40use serde_derive::{Serialize, Deserialize};
41
42use crate::binary::{ReadBytes, WriteBytes};
43use crate::error::Result;
44use crate::files::{DecodeableExtraData, Decodeable, EncodeableExtraData, Encodeable};
45
46//---------------------------------------------------------------------------//
47//                              Enum & Structs
48//---------------------------------------------------------------------------//
49
50/// Container for unsupported or unrecognized file data.
51///
52/// Stores raw binary data without any parsing or validation. This allows rpfm_lib
53/// to handle any file type safely, even if it doesn't have a dedicated decoder.
54///
55/// # Fields
56///
57/// * `data` - Raw binary contents of the file
58///
59/// # Getters/Setters
60///
61/// All fields have public getters, mutable getters, and setters via the `getset` crate:
62/// - `data()` - Get reference to data
63/// - `data_mut()` - Get mutable reference to data
64/// - `set_data()` - Set data
65#[derive(Clone, Debug, PartialEq, Eq, Getters, MutGetters, Setters, Serialize, Deserialize)]
66#[getset(get = "pub", get_mut = "pub", set = "pub")]
67pub struct Unknown {
68    /// Raw binary data of the file.
69    data: Vec<u8>,
70}
71
72//---------------------------------------------------------------------------//
73//                              Implementations
74//---------------------------------------------------------------------------//
75
76impl Decodeable for Unknown {
77
78    fn decode<R: ReadBytes>(data: &mut R, _extra_data: &Option<DecodeableExtraData>) -> Result<Self> {
79        let len = data.len()?;
80        let data = data.read_slice(len as usize, false)?;
81        Ok(Self {
82            data,
83        })
84    }
85}
86
87impl Encodeable for Unknown {
88
89    fn encode<W: WriteBytes>(&mut self, buffer: &mut W, _extra_data: &Option<EncodeableExtraData>) -> Result<()> {
90        buffer.write_all(&self.data).map_err(From::from)
91    }
92}