Skip to content

Commit

Permalink
Dont expose MaterialColorsError and UniqueIdError (#355)
Browse files Browse the repository at this point in the history
At the moment, the error types for `UniqueId` and `MaterialColors` are
exposed. This changes it so they aren't.
  • Loading branch information
Dekkonot authored Sep 14, 2023
1 parent b9d6a42 commit b6618a0
Show file tree
Hide file tree
Showing 6 changed files with 37 additions and 33 deletions.
3 changes: 3 additions & 0 deletions rbx_types/CHANGELOG.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
# rbx_types Changelog

## Unreleased Changes
* `MaterialColorsError` and `UniqueIdError` are no longer publicly exposed. ([#355])

[#355]: https://github.com/rojo-rbx/rbx-dom/pull/355

* Implement `FromStr` for `TerrainMaterials`. ([#354])

Expand Down
13 changes: 12 additions & 1 deletion rbx_types/src/error.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use thiserror::Error;

use crate::{AttributeError, MaterialColorsError, Matrix3Error};
use crate::{AttributeError, MaterialColorsError, Matrix3Error, UniqueIdError};

/// Represents an error that occurred when using a fallible method.
#[derive(Debug, Error)]
Expand Down Expand Up @@ -33,6 +33,14 @@ impl From<MaterialColorsError> for Error {
}
}

impl From<UniqueIdError> for Error {
fn from(source: UniqueIdError) -> Self {
Self {
source: Box::new(source.into()),
}
}
}

#[derive(Debug, Error)]
enum InnerError {
#[error(transparent)]
Expand All @@ -43,4 +51,7 @@ enum InnerError {

#[error(transparent)]
MaterialColors(#[from] MaterialColorsError),

#[error(transparent)]
UniqueId(#[from] UniqueIdError),
}
2 changes: 1 addition & 1 deletion rbx_types/src/material_colors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -90,7 +90,7 @@ where

/// An error that can occur when deserializing or working with MaterialColors and TerrainMaterials.
#[derive(Debug, Error)]
pub enum MaterialColorsError {
pub(crate) enum MaterialColorsError {
/// The `MaterialColors` blob was the wrong number of bytes.
#[error(
"MaterialColors blob was the wrong length (expected it to be 69 bytes, it was {0} bytes)"
Expand Down
24 changes: 15 additions & 9 deletions rbx_types/src/unique_id.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,8 @@ use std::{
time::{Duration, SystemTime, UNIX_EPOCH},
};

use crate::Error as CrateError;

/// The `UniqueId` epoch (2021-01-01 00:00:00 GMT) in terms of time since the Unix epoch
const EPOCH_AS_UNIX: u64 = 1_609_459_200;

Expand All @@ -21,7 +23,7 @@ lazy_static! {

/// Represents an error that can occur when constructing a new `UniqueId`.
#[derive(Debug, Error)]
pub enum UniqueIdError {
pub(crate) enum UniqueIdError {
#[error("SystemTime generated a timestamp that is before the UniqueId epoch")]
SystemPastTime,
#[error("UniqueId timestamp is more than 2^32 - 1 seconds past epoch")]
Expand Down Expand Up @@ -67,14 +69,15 @@ impl UniqueId {
}

/// Returns a new UniqueId.
pub fn now() -> Result<Self, UniqueIdError> {
pub fn now() -> Result<Self, CrateError> {
let time = SystemTime::now()
.duration_since(*EPOCH)
.map_err(|_| UniqueIdError::SystemPastTime)?;
.map_err(|_| CrateError::from(UniqueIdError::SystemPastTime))?;

Ok(Self {
index: INDEX.fetch_add(1, Ordering::AcqRel),
time: u32::try_from(time.as_secs()).map_err(|_| UniqueIdError::Overflow)?,
time: u32::try_from(time.as_secs())
.map_err(|_| CrateError::from(UniqueIdError::Overflow))?,
// This matches Roblox's behavior, where the value is both an i64
// but is also always positive.
random: thread_rng().gen_range(0..i64::MAX),
Expand Down Expand Up @@ -117,17 +120,20 @@ impl fmt::Display for UniqueId {
}

impl FromStr for UniqueId {
type Err = UniqueIdError;
type Err = CrateError;

fn from_str(s: &str) -> Result<Self, Self::Err> {
if s.len() == 32 {
Ok(UniqueId {
random: i64::from_str_radix(&s[0..16], 16)?,
time: u32::from_str_radix(&s[16..24], 16)?,
index: u32::from_str_radix(&s[24..32], 16)?,
random: i64::from_str_radix(&s[0..16], 16)
.map_err(|e| CrateError::from(UniqueIdError::from(e)))?,
time: u32::from_str_radix(&s[16..24], 16)
.map_err(|e| CrateError::from(UniqueIdError::from(e)))?,
index: u32::from_str_radix(&s[24..32], 16)
.map_err(|e| CrateError::from(UniqueIdError::from(e)))?,
})
} else {
Err(UniqueIdError::FromStrBadLen(s.len()))
Err(UniqueIdError::FromStrBadLen(s.len()).into())
}
}
}
Expand Down
13 changes: 2 additions & 11 deletions rbx_xml/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,7 @@ pub(crate) enum DecodeErrorKind {
ParseInt(std::num::ParseIntError),
DecodeBase64(base64::DecodeError),
MigrationError(rbx_reflection::MigrationError),
TypeError(rbx_dom_weak::types::Error),

// Errors specific to rbx_xml
WrongDocVersion(String),
Expand All @@ -93,10 +94,6 @@ pub(crate) enum DecodeErrorKind {
actual_type: VariantType,
message: String,
},
InvalidPropertyData {
property_type: &'static str,
error: String,
},
}

impl fmt::Display for DecodeErrorKind {
Expand All @@ -109,6 +106,7 @@ impl fmt::Display for DecodeErrorKind {
ParseInt(err) => write!(output, "{}", err),
DecodeBase64(err) => write!(output, "{}", err),
MigrationError(err) => write!(output, "{}", err),
TypeError(err) => write!(output, "{}", err),

WrongDocVersion(version) => {
write!(output, "Invalid version '{}', expected version 4", version)
Expand Down Expand Up @@ -144,13 +142,6 @@ impl fmt::Display for DecodeErrorKind {
When trying to convert, this error occured: {}",
class_name, property_name, expected_type, actual_type, message
),
InvalidPropertyData {
property_type,
error,
} => write!(
output,
"Could not decode property of type {property_type} because: {error}"
),
}
}
}
Expand Down
15 changes: 4 additions & 11 deletions rbx_xml/src/types/unique_id.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use std::io::{Read, Write};

use rbx_dom_weak::types::{UniqueId, UniqueIdError};
use rbx_dom_weak::types::UniqueId;

use crate::{
core::XmlType,
Expand All @@ -15,19 +15,12 @@ impl XmlType for UniqueId {
fn read_xml<R: Read>(reader: &mut XmlEventReader<R>) -> Result<Self, DecodeError> {
let content = reader.read_characters()?;

content.parse().map_err(|e| reader.error(e))
content
.parse()
.map_err(|e| reader.error(DecodeErrorKind::TypeError(e)))
}

fn write_xml<W: Write>(&self, writer: &mut XmlEventWriter<W>) -> Result<(), EncodeError> {
writer.write_value(&self.to_string())
}
}

impl From<UniqueIdError> for DecodeErrorKind {
fn from(value: UniqueIdError) -> Self {
DecodeErrorKind::InvalidPropertyData {
property_type: "UniqueId",
error: value.to_string(),
}
}
}

0 comments on commit b6618a0

Please sign in to comment.