Struct lune::roblox::document::Document

source ·
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

source

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:

KindFormatExtension
PlaceBinaryrbxl
PlaceXmlrbxlx
ModelBinaryrbxm
ModelXmlrbxmx
source

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.

source

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.

source

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.

source

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.

source

pub fn kind(&self) -> DocumentKind

Gets the kind this document was created with.

source

pub fn format(&self) -> DocumentFormat

Gets the format this document was created with.

source

pub fn extension(&self) -> &'static str

Gets the file extension for this document.

source

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.

source

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.

source

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.

source

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.

Trait Implementations§

source§

impl Debug for Document

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T> Instrument for T

source§

fn instrument(self, span: Span) -> Instrumented<Self>

Instruments this type with the provided Span, returning an Instrumented wrapper. Read more
source§

fn in_current_span(self) -> Instrumented<Self>

Instruments this type with the current Span, returning an Instrumented wrapper. Read more
source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> Same for T

§

type Output = T

Should always be Self
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
§

impl<V, T> VZip<V> for Twhere V: MultiLane<T>,

§

fn vzip(self) -> V

source§

impl<T> WithSubscriber for T

source§

fn with_subscriber<S>(self, subscriber: S) -> WithDispatch<Self>where S: Into<Dispatch>,

Attaches the provided Subscriber to this type, returning a WithDispatch wrapper. Read more
source§

fn with_current_subscriber(self) -> WithDispatch<Self>

Attaches the current default Subscriber to this type, returning a WithDispatch wrapper. Read more