mirror of
https://github.com/lune-org/lune.git
synced 2024-12-12 13:00:37 +00:00
Minor fixes, improve docs for document struct
This commit is contained in:
parent
cda2d46ed9
commit
1129d32b7b
2 changed files with 45 additions and 9 deletions
|
@ -14,6 +14,28 @@ pub use error::*;
|
||||||
pub use format::*;
|
pub use format::*;
|
||||||
pub use kind::*;
|
pub use kind::*;
|
||||||
|
|
||||||
|
pub type DocumentResult<T> = Result<T, DocumentError>;
|
||||||
|
|
||||||
|
/**
|
||||||
|
A wrapper for [`rbx_dom_weak::WeakDom`] that also takes care of
|
||||||
|
reading and writing different kinds and formats of roblox files.
|
||||||
|
|
||||||
|
```rust ignore
|
||||||
|
// 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()?)?;
|
||||||
|
```
|
||||||
|
*/
|
||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct Document {
|
pub struct Document {
|
||||||
kind: DocumentKind,
|
kind: DocumentKind,
|
||||||
|
@ -51,9 +73,7 @@ impl Document {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn from_bytes_inner(
|
fn from_bytes_inner(bytes: impl AsRef<[u8]>) -> DocumentResult<(DocumentFormat, WeakDom)> {
|
||||||
bytes: impl AsRef<[u8]>,
|
|
||||||
) -> Result<(DocumentFormat, WeakDom), DocumentError> {
|
|
||||||
let bytes = bytes.as_ref();
|
let bytes = bytes.as_ref();
|
||||||
let format = DocumentFormat::from_bytes(bytes).ok_or(DocumentError::UnknownFormat)?;
|
let format = DocumentFormat::from_bytes(bytes).ok_or(DocumentError::UnknownFormat)?;
|
||||||
let dom = match format {
|
let dom = match format {
|
||||||
|
@ -80,7 +100,7 @@ impl Document {
|
||||||
of the file, and a model file with services in it will detect as a place file, so
|
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.
|
if possible using [`Document::from_bytes`] with an explicit kind should be preferred.
|
||||||
*/
|
*/
|
||||||
pub fn from_bytes_auto(bytes: impl AsRef<[u8]>) -> Result<Self, DocumentError> {
|
pub fn from_bytes_auto(bytes: impl AsRef<[u8]>) -> DocumentResult<Self> {
|
||||||
let (format, dom) = Self::from_bytes_inner(bytes)?;
|
let (format, dom) = Self::from_bytes_inner(bytes)?;
|
||||||
let kind = DocumentKind::from_weak_dom(&dom).ok_or(DocumentError::UnknownKind)?;
|
let kind = DocumentKind::from_weak_dom(&dom).ok_or(DocumentError::UnknownKind)?;
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
|
@ -95,8 +115,11 @@ impl Document {
|
||||||
|
|
||||||
This will automatically handle and detect if the document
|
This will automatically handle and detect if the document
|
||||||
should be decoded using a roblox binary or roblox xml format.
|
should be decoded using a roblox binary or roblox xml format.
|
||||||
|
|
||||||
|
Note that passing [`DocumentKind`] enum values other than [`DocumentKind::Place`] and
|
||||||
|
[`DocumentKind::Model`] is possible but should only be done within the `lune-roblox` crate.
|
||||||
*/
|
*/
|
||||||
pub fn from_bytes(bytes: impl AsRef<[u8]>, kind: DocumentKind) -> Result<Self, DocumentError> {
|
pub fn from_bytes(bytes: impl AsRef<[u8]>, kind: DocumentKind) -> DocumentResult<Self> {
|
||||||
let (format, dom) = Self::from_bytes_inner(bytes)?;
|
let (format, dom) = Self::from_bytes_inner(bytes)?;
|
||||||
Ok(Self {
|
Ok(Self {
|
||||||
kind,
|
kind,
|
||||||
|
@ -113,7 +136,7 @@ impl Document {
|
||||||
with, meaning if the document is a binary document the output
|
with, meaning if the document is a binary document the output
|
||||||
will be binary, and vice versa for xml and other future formats.
|
will be binary, and vice versa for xml and other future formats.
|
||||||
*/
|
*/
|
||||||
pub fn to_bytes(&self) -> Result<Vec<u8>, DocumentError> {
|
pub fn to_bytes(&self) -> DocumentResult<Vec<u8>> {
|
||||||
self.to_bytes_with_format(self.format)
|
self.to_bytes_with_format(self.format)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
@ -121,7 +144,7 @@ impl Document {
|
||||||
Encodes the document as a vector of bytes, to
|
Encodes the document as a vector of bytes, to
|
||||||
be written to a file or sent over the network.
|
be written to a file or sent over the network.
|
||||||
*/
|
*/
|
||||||
pub fn to_bytes_with_format(&self, format: DocumentFormat) -> Result<Vec<u8>, DocumentError> {
|
pub fn to_bytes_with_format(&self, format: DocumentFormat) -> DocumentResult<Vec<u8>> {
|
||||||
let dom = self.dom.try_read().expect("Failed to lock dom");
|
let dom = self.dom.try_read().expect("Failed to lock dom");
|
||||||
let mut bytes = Vec::new();
|
let mut bytes = Vec::new();
|
||||||
match format {
|
match format {
|
||||||
|
@ -152,6 +175,19 @@ impl Document {
|
||||||
self.format
|
self.format
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/**
|
||||||
|
Gets the file extension for this document.
|
||||||
|
|
||||||
|
Note that this will return `None` for an internal root
|
||||||
|
document, otherwise it will always return `Some`.
|
||||||
|
|
||||||
|
As such, if it is known that no internal root document is
|
||||||
|
being used here, the return value can be safely unwrapped.
|
||||||
|
*/
|
||||||
|
pub fn extension(&self) -> Option<&'static str> {
|
||||||
|
Self::canonical_extension(self.kind, self.format)
|
||||||
|
}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
Retrieves the root referent of the underlying weak dom.
|
Retrieves the root referent of the underlying weak dom.
|
||||||
*/
|
*/
|
||||||
|
|
|
@ -101,7 +101,7 @@ mod tests {
|
||||||
fn is_a_service_invalid() {
|
fn is_a_service_invalid() {
|
||||||
assert_eq!(instance_is_a_service("Camera"), Some(false));
|
assert_eq!(instance_is_a_service("Camera"), Some(false));
|
||||||
assert_eq!(instance_is_a_service("Terrain"), Some(false));
|
assert_eq!(instance_is_a_service("Terrain"), Some(false));
|
||||||
assert_eq!(instance_is_a_service("Work-space"), Some(false));
|
assert_eq!(instance_is_a_service("Work-space"), None);
|
||||||
assert_eq!(instance_is_a_service("CSG Dictionary Service"), Some(false));
|
assert_eq!(instance_is_a_service("CSG Dictionary Service"), None);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue