#[cfg(feature = "schema")]
use schemars::JsonSchema;
use serde::{Deserialize, Serialize};
use crate::ids::{AttributeId, BodyId, CoedgeId, EdgeId, FaceId, LoopId, ShellId, VertexId};
use crate::scalar::FiniteReal;
#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(tag = "kind", content = "id", rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
pub enum AttributeTarget {
Document,
Body(BodyId),
Face(FaceId),
Shell(ShellId),
Loop(LoopId),
Coedge(CoedgeId),
Edge(EdgeId),
Vertex(VertexId),
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(tag = "kind", content = "value", rename_all = "snake_case")]
#[serde(deny_unknown_fields)]
pub enum AttributeValue {
Integer(i64),
Float(#[cfg_attr(feature = "schema", schemars(with = "f64"))] FiniteReal),
String(String),
Boolean(bool),
Reference(String),
Vector(#[cfg_attr(feature = "schema", schemars(with = "Vec<f64>"))] Vec<FiniteReal>),
}
impl AttributeValue {
#[must_use]
pub fn float(value: f64) -> Option<Self> {
FiniteReal::new(value).map(Self::Float)
}
pub fn vector(values: impl IntoIterator<Item = f64>) -> Option<Self> {
values
.into_iter()
.map(FiniteReal::new)
.collect::<Option<Vec<_>>>()
.map(Self::Vector)
}
}
#[derive(Debug, Clone, PartialEq, Serialize, Deserialize)]
#[cfg_attr(feature = "schema", derive(JsonSchema))]
#[serde(deny_unknown_fields)]
pub struct SourceAttribute {
pub id: AttributeId,
pub target: AttributeTarget,
pub name: String,
pub values: Vec<AttributeValue>,
}
#[cfg(test)]
mod tests {
use super::AttributeTarget;
#[test]
fn attribute_target_copy_preserves_the_target() {
let target = AttributeTarget::Face(crate::ids::FaceId::mint("test:model:face#17").unwrap());
assert_eq!(target.clone(), target);
assert_eq!(AttributeTarget::Document.clone(), AttributeTarget::Document);
}
}
mod identity_rewrite;