1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//! Property node.

use anyhow::{format_err, Error};
use fbxcel::{
    low::v7400::AttributeValue,
    tree::v7400::{NodeHandle, NodeId},
};
use log::warn;

use crate::v7400::{object::property::LoadProperty, Document};

/// Node ID of a `P` node under `Properties70` node.
#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
pub struct PropertyNodeId(NodeId);

impl PropertyNodeId {
    /// Creates a new `PropertyNodeId`.
    pub(crate) fn new(node_id: NodeId) -> Self {
        Self(node_id)
    }
}

impl std::ops::Deref for PropertyNodeId {
    type Target = NodeId;

    fn deref(&self) -> &Self::Target {
        &self.0
    }
}

impl From<PropertyNodeId> for NodeId {
    fn from(v: PropertyNodeId) -> Self {
        v.0
    }
}

/// Node handle of a `P` node under `Properties70` node.
#[derive(Debug, Clone, Copy)]
pub struct PropertyHandle<'a> {
    /// Node ID.
    node_id: PropertyNodeId,
    /// Document.
    doc: &'a Document,
}

impl<'a> PropertyHandle<'a> {
    /// Creates a new `PropertyNodeId`.
    pub(crate) fn new(node_id: PropertyNodeId, doc: &'a Document) -> Self {
        Self { node_id, doc }
    }

    /// Returns a node handle.
    pub(crate) fn node(&self) -> NodeHandle<'a> {
        self.node_id.to_handle(self.doc.tree())
    }

    /// Returns a node ID.
    pub fn node_id(&self) -> PropertyNodeId {
        self.node_id
    }

    /// Returns a reference to the document.
    pub fn document(&self) -> &'a Document {
        self.doc
    }

    /// Reads a value from the property handle if possible.
    pub fn load_value<V: LoadProperty<'a>>(&self, loader: V) -> Result<V::Value, V::Error> {
        loader.load(self)
    }

    /// Returns proprety name.
    pub fn name(&self) -> Result<&'a str, Error> {
        self.get_string_attr(0)
            .map_err(|e| format_err!("Failed to get property name: {}", e))
    }

    /// Returns proprety type name.
    pub fn data_type(&self) -> Result<&'a str, Error> {
        self.get_string_attr(1)
            .map_err(|e| format_err!("Failed to get property data type: {}", e))
    }

    /// Returns proprety label.
    pub fn label(&self) -> Result<&'a str, Error> {
        self.get_string_attr(2)
            .map_err(|e| format_err!("Failed to get property label: {}", e))
    }

    /// Returns property value part of node attributes.
    pub fn value_part(&self) -> &'a [AttributeValue] {
        self.node().attributes().get(4..).unwrap_or_else(|| {
            warn!(
                "Ignoring error: Not enough node attribute for proprerty node: \
                 node_id={:?}, num_attrs={}",
                self.node_id,
                self.node().attributes().len()
            );
            &[]
        })
    }

    /// For internal use: returns string attribute.
    fn get_string_attr(&self, index: usize) -> Result<&'a str, Error> {
        self.node()
            .attributes()
            .get(index)
            .ok_or_else(|| {
                format_err!(
                    "No properties found: node_id={:?}, attr_index={:?}",
                    self,
                    index
                )
            })?
            .get_string_or_type()
            .map_err(|ty| {
                format_err!(
                    "Expected string but got {:?}: node_id={:?}, attr_index={:?}",
                    ty,
                    self,
                    index
                )
            })
    }
}