diff options
Diffstat (limited to 'mingling_core/src/renderer/structural')
| -rw-r--r-- | mingling_core/src/renderer/structural/error.rs | 68 | ||||
| -rw-r--r-- | mingling_core/src/renderer/structural/structural_data.rs | 15 |
2 files changed, 83 insertions, 0 deletions
diff --git a/mingling_core/src/renderer/structural/error.rs b/mingling_core/src/renderer/structural/error.rs new file mode 100644 index 0000000..a7fbc75 --- /dev/null +++ b/mingling_core/src/renderer/structural/error.rs @@ -0,0 +1,68 @@ +/// Represents an error that occurs during serialization of a structural renderer. +/// +/// This error stores a human-readable message describing what went wrong +/// during the serialization process. +#[derive(Debug)] +pub struct StructuralRendererSerializeError { + /// The underlying error message. + error: String, +} + +impl StructuralRendererSerializeError { + #[must_use] + pub fn new(error: String) -> Self { + Self { error } + } +} + +impl From<&str> for StructuralRendererSerializeError { + fn from(s: &str) -> Self { + Self::new(s.to_string()) + } +} + +impl std::ops::Deref for StructuralRendererSerializeError { + type Target = String; + + fn deref(&self) -> &Self::Target { + &self.error + } +} + +impl From<StructuralRendererSerializeError> for String { + fn from(val: StructuralRendererSerializeError) -> Self { + val.error + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn new_creates_error_with_message() { + let msg = "serialization failed".to_string(); + let err = StructuralRendererSerializeError::new(msg.clone()); + assert_eq!(err.error, msg); + } + + #[test] + fn from_str_creates_error_from_string_slice() { + let err: StructuralRendererSerializeError = "oops".into(); + assert_eq!(err.error, "oops"); + } + + #[test] + fn deref_accesses_inner_error_string() { + let err = StructuralRendererSerializeError::new("inner message".to_string()); + let derefed: &String = &err; + assert_eq!(derefed, "inner message"); + } + + #[test] + fn into_string_extracts_message() { + let err = StructuralRendererSerializeError::new("extract me".to_string()); + let s: String = err.into(); + assert_eq!(s, "extract me"); + } +} diff --git a/mingling_core/src/renderer/structural/structural_data.rs b/mingling_core/src/renderer/structural/structural_data.rs new file mode 100644 index 0000000..1cafac3 --- /dev/null +++ b/mingling_core/src/renderer/structural/structural_data.rs @@ -0,0 +1,15 @@ +use serde::Serialize; + +/// Marker trait for types that support structured output (JSON / YAML / TOML / RON). +/// +/// This trait is a **supertrait** of `serde::Serialize` and is sealed via +/// `__private::StructuralDataSealed`. It can only be implemented through: +/// +/// - `#[derive(StructuralData)]` +/// - `pack_structural!` +/// - `group_structural!` +/// +/// These entry points also register the type in the global `STRUCTURED_TYPES` +/// registry, which is required for the `structural_render` match arm to be generated. +#[doc(hidden)] +pub trait StructuralData: Serialize + crate::__private::StructuralDataSealed {} |
