mirror of
https://github.com/mbilker/kbinxml-rs.git
synced 2026-09-10 11:35:22 -05:00
node(definition): capturing into_node -> non-capturing as_node
This commit is contained in:
@@ -8,7 +8,7 @@ use node_types::StandardType;
|
||||
use sixbit::{Sixbit, SixbitSize};
|
||||
use value::Value;
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum Key<'buf> {
|
||||
Compressed {
|
||||
size: SixbitSize,
|
||||
@@ -20,7 +20,7 @@ pub enum Key<'buf> {
|
||||
},
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub enum NodeData<'buf> {
|
||||
Some {
|
||||
key: Key<'buf>,
|
||||
@@ -29,7 +29,7 @@ pub enum NodeData<'buf> {
|
||||
None,
|
||||
}
|
||||
|
||||
#[derive(Debug)]
|
||||
#[derive(Clone, Copy, Debug)]
|
||||
pub struct NodeDefinition<'buf> {
|
||||
encoding: EncodingType,
|
||||
pub node_type: StandardType,
|
||||
@@ -82,36 +82,24 @@ impl<'buf> NodeDefinition<'buf> {
|
||||
}
|
||||
}
|
||||
|
||||
pub fn into_node(self) -> Result<Node, KbinError> {
|
||||
trace!("parsing definition: {:?}", self);
|
||||
pub fn value(&self) -> Result<Value, KbinError> {
|
||||
match (self.node_type, self.data) {
|
||||
(StandardType::NodeEnd, _) |
|
||||
(StandardType::FileEnd, _) => {
|
||||
return Err(KbinErrorKind::InvalidNodeType(self.node_type).into());
|
||||
},
|
||||
(StandardType::NodeStart, NodeData::Some { key, .. }) => {
|
||||
let key = key.to_string()?;
|
||||
Ok(Node::new(key))
|
||||
},
|
||||
(StandardType::Attribute, NodeData::Some { key, value_data }) => {
|
||||
let key = key.to_string()?;
|
||||
(StandardType::Attribute, NodeData::Some { ref value_data, .. }) => {
|
||||
let data = strip_trailing_null_bytes(value_data);
|
||||
let value = self.encoding.decode_bytes(data)?;
|
||||
Ok(Node::with_value(key, Value::Attribute(value)))
|
||||
Ok(Value::Attribute(value))
|
||||
},
|
||||
(StandardType::String, NodeData::Some { key, value_data }) => {
|
||||
let key = key.to_string()?;
|
||||
(StandardType::String, NodeData::Some { ref value_data, .. }) => {
|
||||
let data = strip_trailing_null_bytes(value_data);
|
||||
let value = self.encoding.decode_bytes(data)?;
|
||||
Ok(Node::with_value(key, Value::String(value)))
|
||||
Ok(Value::String(value))
|
||||
},
|
||||
(node_type, NodeData::Some { key, value_data }) => {
|
||||
let key = key.to_string()?;
|
||||
(node_type, NodeData::Some { ref value_data, .. }) => {
|
||||
let value = Value::from_standard_type(node_type, self.is_array, value_data)?;
|
||||
debug!("value: {:?}", value);
|
||||
match value {
|
||||
Some(value) => Ok(Node::with_value(key, value)),
|
||||
None => Ok(Node::new(key)),
|
||||
Some(value) => Ok(value),
|
||||
None => Err(KbinErrorKind::InvalidNodeType(node_type).into()),
|
||||
}
|
||||
},
|
||||
(node_type, NodeData::None) => {
|
||||
@@ -119,4 +107,27 @@ impl<'buf> NodeDefinition<'buf> {
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
pub fn as_node(&self) -> Result<Node, KbinError> {
|
||||
trace!("parsing definition: {:?}", self);
|
||||
match (self.node_type, self.data) {
|
||||
(StandardType::NodeEnd, _) |
|
||||
(StandardType::FileEnd, _) => {
|
||||
Err(KbinErrorKind::InvalidNodeType(self.node_type).into())
|
||||
},
|
||||
(StandardType::NodeStart, NodeData::Some { key, .. }) => {
|
||||
let key = key.to_string()?;
|
||||
Ok(Node::new(key))
|
||||
},
|
||||
(_, NodeData::Some { key, .. }) => {
|
||||
let key = key.to_string()?;
|
||||
let value = self.value()?;
|
||||
debug!("value: {:?}", value);
|
||||
Ok(Node::with_value(key, value))
|
||||
},
|
||||
(node_type, NodeData::None) => {
|
||||
Err(KbinErrorKind::InvalidNodeType(node_type).into())
|
||||
},
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
@@ -45,6 +45,16 @@ impl Printer {
|
||||
};
|
||||
}
|
||||
|
||||
for def in &definitions {
|
||||
match def.node_type {
|
||||
StandardType::NodeEnd |
|
||||
StandardType::FileEnd => {},
|
||||
_ => {
|
||||
eprintln!("node: {:?}", def.as_node());
|
||||
},
|
||||
}
|
||||
}
|
||||
|
||||
let collection = NodeCollection::from_iter(definitions.into_iter());
|
||||
eprintln!("collection: {:#?}", collection);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user