use openusd::Result;
use openusd::{sdf, usd};
use super::impl_shading_attribute;
use super::tokens::{META_CONNECTABILITY, NS_INPUTS};
use super::{Connectability, ConnectionTarget, ShadingAttribute};
#[derive(Clone, derive_more::Deref)]
pub struct Input {
attribute: usd::Attribute,
}
impl_shading_attribute!(Input, NS_INPUTS);
impl Input {
pub fn connectability(&self) -> Result<Connectability> {
Ok(self
.attribute
.get_metadata::<Connectability>(META_CONNECTABILITY)?
.unwrap_or_default())
}
pub fn set_connectability(self, connectability: Connectability) -> Result<Self, usd::StageAuthoringError> {
Ok(Self {
attribute: self.attribute.set_metadata(META_CONNECTABILITY, connectability)?,
})
}
}
impl From<Input> for ShadingAttribute {
fn from(input: Input) -> Self {
ShadingAttribute::Input(input)
}
}
impl ConnectionTarget for Input {
fn target_path(&self) -> &sdf::Path {
self.attribute().path()
}
}
#[cfg(test)]
mod tests {
use openusd::Result;
use crate::shade::{Connectable, Shader};
use openusd::usd::Stage;
#[test]
fn invalid_base_name_reads_empty() -> Result<()> {
let stage = Stage::builder().in_memory("anon.usda")?;
let shader = Shader::define(&stage, "/Mat/Surface")?;
let input = shader.input("diffuse color");
assert_eq!(input.full_name(), "");
assert_eq!(input.base_name(), "");
assert_eq!(input.get::<f32>()?, None);
assert!(input.connected_sources()?.sources().is_empty());
Ok(())
}
}