pub struct Document { /* private fields */ }Expand description
A container for rbx_dom_weak::WeakDom that also takes care of
reading and writing different kinds and formats of roblox files.
Code Sample #1
// Reading a document from a file
let file_path = PathBuf::from("place-file.rbxl");
let file_contents = std::fs::read(&file_path)?;
let document = Document::from_bytes_auto(file_contents)?;
// Writing a document to a file
let file_path = PathBuf::from("place-file")
.with_extension(document.extension()?);
std::fs::write(&file_path, document.to_bytes()?)?;Code Sample #2
// Converting a Document to a DataModel or model child instances
let data_model = document.into_data_model_instance()?;
let model_children = document.into_instance_array()?;
// Converting a DataModel or model child instances into a Document
let place_doc = Document::from_data_model_instance(data_model)?;
let model_doc = Document::from_instance_array(model_children)?;Implementations§
source§impl Document
impl Document
sourcepub fn canonical_extension(
kind: DocumentKind,
format: DocumentFormat
) -> &'static str
pub fn canonical_extension( kind: DocumentKind, format: DocumentFormat ) -> &'static str
Gets the canonical file extension for a given kind and format of document, which will follow this chart:
| Kind | Format | Extension |
|---|---|---|
| Place | Binary | rbxl |
| Place | Xml | rbxlx |
| Model | Binary | rbxm |
| Model | Xml | rbxmx |
sourcepub fn from_bytes_auto(bytes: impl AsRef<[u8]>) -> DocumentResult<Self>
pub fn from_bytes_auto(bytes: impl AsRef<[u8]>) -> DocumentResult<Self>
Decodes and creates a new document from a byte buffer.
This will automatically handle and detect if the document should be decoded using a roblox binary or roblox xml format, and if it is a model or place file.
Note that detection of model vs place file is heavily dependent on the structure
of the file, and a model file with services in it will detect as a place file, so
if possible using Document::from_bytes with an explicit kind should be preferred.
sourcepub fn from_bytes(
bytes: impl AsRef<[u8]>,
kind: DocumentKind
) -> DocumentResult<Self>
pub fn from_bytes( bytes: impl AsRef<[u8]>, kind: DocumentKind ) -> DocumentResult<Self>
Decodes and creates a new document from a byte buffer.
This will automatically handle and detect if the document should be decoded using a roblox binary or roblox xml format.
sourcepub fn to_bytes(&self) -> DocumentResult<Vec<u8>>
pub fn to_bytes(&self) -> DocumentResult<Vec<u8>>
Encodes the document as a vector of bytes, to be written to a file or sent over the network.
This will use the same format that the document was created with, meaning if the document is a binary document the output will be binary, and vice versa for xml and other future formats.
sourcepub fn to_bytes_with_format(
&self,
format: DocumentFormat
) -> DocumentResult<Vec<u8>>
pub fn to_bytes_with_format( &self, format: DocumentFormat ) -> DocumentResult<Vec<u8>>
Encodes the document as a vector of bytes, to be written to a file or sent over the network.
sourcepub fn kind(&self) -> DocumentKind
pub fn kind(&self) -> DocumentKind
Gets the kind this document was created with.
sourcepub fn format(&self) -> DocumentFormat
pub fn format(&self) -> DocumentFormat
Gets the format this document was created with.
sourcepub fn into_data_model_instance(self) -> DocumentResult<Instance>
pub fn into_data_model_instance(self) -> DocumentResult<Instance>
Creates a DataModel instance out of this place document.
Will error if the document is not a place.
sourcepub fn into_instance_array(self) -> DocumentResult<Vec<Instance>>
pub fn into_instance_array(self) -> DocumentResult<Vec<Instance>>
Creates an array of instances out of this model document.
Will error if the document is not a model.
sourcepub fn from_data_model_instance(i: Instance) -> DocumentResult<Self>
pub fn from_data_model_instance(i: Instance) -> DocumentResult<Self>
Creates a place document out of a DataModel instance.
Will error if the instance is not a DataModel.
sourcepub fn from_instance_array(v: Vec<Instance>) -> DocumentResult<Self>
pub fn from_instance_array(v: Vec<Instance>) -> DocumentResult<Self>
Creates a model document out of an array of instances.
Will error if any of the instances is a DataModel.