use std::fmt;
use cxx::UniquePtr;
use crate::bridge::ffi;
#[cfg(feature = "json")]
use crate::bridge::style_value;
#[cfg(feature = "json")]
use crate::style::{value::build_style_value, StyleError};
pub struct OpaqueSource {
source_id: String,
source: UniquePtr<ffi::CxxSource>,
}
impl OpaqueSource {
#[must_use]
pub fn source_id(&self) -> &str {
&self.source_id
}
pub(crate) fn into_inner(self) -> UniquePtr<ffi::CxxSource> {
self.source
}
}
impl fmt::Debug for OpaqueSource {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.debug_struct("OpaqueSource").field("source_id", &self.source_id).finish_non_exhaustive()
}
}
#[derive(Debug)]
#[non_exhaustive]
pub enum AnySource {
Opaque(OpaqueSource),
}
impl AnySource {
#[cfg(feature = "json")]
pub(crate) fn from_source_ptr(
source_id: String,
source: UniquePtr<ffi::CxxSource>,
) -> Option<Self> {
if source.is_null() {
return None;
}
Some(Self::Opaque(OpaqueSource { source_id, source }))
}
#[cfg(feature = "json")]
pub fn from_json_str(id: impl AsRef<str>, json: &str) -> Result<Self, StyleError> {
let value: serde_json::Value = serde_json::from_str(json)?;
Self::from_json_value(id, &value)
}
#[cfg(feature = "json")]
pub fn from_json_value(
id: impl AsRef<str>,
value: &serde_json::Value,
) -> Result<Self, StyleError> {
let id = id.as_ref();
let style_value = build_style_value(value)?;
let mut error_message = String::new();
let source = style_value::source_from_value(id, &style_value, &mut error_message);
if source.is_null() {
return Err(StyleError::Native(error_message));
}
Self::from_source_ptr(id.to_owned(), source).ok_or(StyleError::Native(error_message))
}
#[must_use]
pub fn source_id(&self) -> &str {
match self {
Self::Opaque(s) => s.source_id(),
}
}
pub(crate) fn into_inner(self) -> UniquePtr<ffi::CxxSource> {
match self {
Self::Opaque(s) => s.into_inner(),
}
}
}