#![allow(dead_code)]
use anyhow::Result;
use crate::sdf::{FieldKey, Path, Value};
use crate::usd::{Prim, Stage};
pub(crate) fn attr_value(stage: &Stage, prim: &Path, name: &str) -> Result<Option<Value>> {
stage.field::<Value>(prim.append_property(name)?, FieldKey::Default)
}
pub(crate) fn read_token(stage: &Stage, prim: &Path, name: &str) -> Result<Option<String>> {
Ok(match attr_value(stage, prim, name)? {
Some(Value::Token(s) | Value::String(s)) => Some(s),
_ => None,
})
}
pub(crate) fn get_typed(stage: &Stage, path: impl Into<Path>, type_name: &str) -> Result<Option<Prim>> {
let prim = stage.prim_at(path);
if prim.type_name()?.as_deref() != Some(type_name) {
return Ok(None);
}
Ok(Some(prim))
}
pub(crate) fn get_typed_any(stage: &Stage, path: impl Into<Path>, type_names: &[&str]) -> Result<Option<Prim>> {
let prim = stage.prim_at(path);
match prim.type_name()? {
Some(t) if type_names.contains(&t.as_str()) => Ok(Some(prim)),
_ => Ok(None),
}
}
pub(crate) fn get_with_api(stage: &Stage, path: impl Into<Path>, apis: &[&str]) -> Result<Option<Prim>> {
let prim = stage.prim_at(path);
let applied = prim.api_schemas()?;
if apis.iter().any(|a| applied.iter().any(|s| s == a)) {
Ok(Some(prim))
} else {
Ok(None)
}
}
macro_rules! impl_token_value {
($($ty:ty),+ $(,)?) => {$(
impl From<$ty> for $crate::sdf::Value {
fn from(value: $ty) -> Self {
$crate::sdf::Value::Token(value.as_token().to_string())
}
}
impl TryFrom<$crate::sdf::Value> for $ty {
type Error = $crate::sdf::ValueConversionError;
fn try_from(value: $crate::sdf::Value) -> Result<Self, Self::Error> {
match &value {
$crate::sdf::Value::Token(s) | $crate::sdf::Value::String(s) => <$ty>::from_token(s),
_ => None,
}
.ok_or_else(|| $crate::sdf::ValueConversionError::new(stringify!($ty), &value))
}
}
)+};
}
pub(crate) use impl_token_value;