mirror of
https://github.com/mbilker/kbinxml-rs.git
synced 2026-09-09 19:15:27 -05:00
lib: conditional serde dependency, version bump 0.12.0
This commit is contained in:
15
Cargo.toml
15
Cargo.toml
@@ -1,20 +1,25 @@
|
||||
[package]
|
||||
name = "kbinxml"
|
||||
version = "0.11.4"
|
||||
version = "0.12.0"
|
||||
authors = ["Matt Bilker <me@mbilker.us>"]
|
||||
|
||||
[dependencies]
|
||||
byteorder = "1.2.3"
|
||||
bytes = "0.4.10"
|
||||
cfg-if = "0.1"
|
||||
encoding = "0.2"
|
||||
failure = "0.1.1"
|
||||
indexmap = { version = "1.0.1", features = ["serde-1"] }
|
||||
indexmap = "1.0.1"
|
||||
lazy_static = "1.0.0"
|
||||
log = "0.4.1"
|
||||
minidom = "0.9.0"
|
||||
pretty_env_logger = "0.2.3"
|
||||
quick-xml = "0.12.1"
|
||||
rustc-hex = "2.0.1"
|
||||
serde = "1.0.69"
|
||||
serde_bytes = "0.10.4"
|
||||
serde_derive = "1.0.69"
|
||||
|
||||
serde = { version = "1.0.69", optional = true }
|
||||
serde_bytes = { version = "0.10.4", optional = true }
|
||||
serde_derive = { version = "1.0.69", optional = true }
|
||||
|
||||
[features]
|
||||
serde-1 = ["serde", "serde_bytes", "serde_derive", "indexmap/serde-1"]
|
||||
|
||||
@@ -6,43 +6,52 @@ extern crate minidom;
|
||||
extern crate pretty_env_logger;
|
||||
extern crate quick_xml;
|
||||
|
||||
#[macro_use] extern crate serde_derive;
|
||||
#[macro_use] extern crate cfg_if;
|
||||
|
||||
use std::env;
|
||||
use std::fs::File;
|
||||
use std::io::{Cursor, Error as IoError, ErrorKind as IoErrorKind, Read, Write, stdout};
|
||||
use std::net::Ipv4Addr;
|
||||
use std::str;
|
||||
|
||||
use failure::Fail;
|
||||
use kbinxml::{ExtraNodes, Node, Options, Printer, from_bytes, to_bytes};
|
||||
use kbinxml::{Options, Printer};
|
||||
use minidom::Element;
|
||||
use quick_xml::Writer;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(rename = "test2")]
|
||||
pub struct Testing2 {
|
||||
hi: u16,
|
||||
ho: i16,
|
||||
vu: Vec<u8>,
|
||||
opt: Option<u8>,
|
||||
opt2: Option<u8>,
|
||||
ip: Ipv4Addr,
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "serde")] {
|
||||
#[macro_use] extern crate serde_derive;
|
||||
|
||||
#[serde(flatten)]
|
||||
extra: ExtraNodes,
|
||||
}
|
||||
use std::net::Ipv4Addr;
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(rename = "test")]
|
||||
pub struct Testing {
|
||||
#[serde(rename = "attr_the_attr")] the_attr: String,
|
||||
hi: u8,
|
||||
ok: [u8; 3],
|
||||
hhh: (u8, u8),
|
||||
hhg: (u32, u32),
|
||||
foo: String,
|
||||
testing2: Testing2,
|
||||
use kbinxml::{ExtraNodes, Node, from_bytes, to_bytes};
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(rename = "test2")]
|
||||
pub struct Testing2 {
|
||||
hi: u16,
|
||||
ho: i16,
|
||||
vu: Vec<u8>,
|
||||
opt: Option<u8>,
|
||||
opt2: Option<u8>,
|
||||
ip: Ipv4Addr,
|
||||
|
||||
#[serde(flatten)]
|
||||
extra: ExtraNodes,
|
||||
}
|
||||
|
||||
#[derive(Debug, Deserialize, Serialize)]
|
||||
#[serde(rename = "test")]
|
||||
pub struct Testing {
|
||||
#[serde(rename = "attr_the_attr")] the_attr: String,
|
||||
hi: u8,
|
||||
ok: [u8; 3],
|
||||
hhh: (u8, u8),
|
||||
hhg: (u32, u32),
|
||||
foo: String,
|
||||
testing2: Testing2,
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
fn display_err(err: impl Fail) -> IoError {
|
||||
@@ -117,6 +126,73 @@ fn compare_slice(left: &[u8], right: &[u8]) {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
fn test_serde() -> std::io::Result<()> {
|
||||
let obj = Testing {
|
||||
the_attr: "the_value".to_string(),
|
||||
hi: 12,
|
||||
ok: [12, 24, 48],
|
||||
hhh: (55, 66),
|
||||
hhg: (55, 66),
|
||||
foo: "foobarbaz".to_string(),
|
||||
testing2: Testing2 {
|
||||
hi: 32423,
|
||||
ho: 32000,
|
||||
vu: vec![33, 255, 254],
|
||||
opt: None,
|
||||
opt2: Some(111),
|
||||
ip: Ipv4Addr::new(127, 0, 0, 1),
|
||||
extra: ExtraNodes::new(),
|
||||
},
|
||||
};
|
||||
let bytes = to_bytes(&obj).unwrap();
|
||||
eprintln!("bytes: {:02x?}", bytes);
|
||||
|
||||
let mut file = File::create("testing.kbin")?;
|
||||
file.write_all(&bytes)?;
|
||||
|
||||
let obj2 = from_bytes::<Testing>(&bytes);
|
||||
match &obj2 {
|
||||
Ok(obj2) => eprintln!("obj2: {:#?}", obj2),
|
||||
Err(e) => eprintln!("Unable to parse generated kbin back to struct: {:#?}", e),
|
||||
};
|
||||
|
||||
let value = from_bytes::<Node>(&bytes);
|
||||
match &value {
|
||||
Ok(obj2) => eprintln!("obj2: {:#?}", obj2),
|
||||
Err(e) => eprintln!("Unable to parse generated kbin back to `Value`: {:#?}", e),
|
||||
};
|
||||
|
||||
if obj2.is_ok() && value.is_ok() {
|
||||
Printer::run(&bytes).unwrap();
|
||||
}
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "serde"))]
|
||||
fn test_serde() -> std::io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
fn test_serde_node(contents: &[u8]) -> std::io::Result<()> {
|
||||
let node = from_bytes::<Node>(&contents);
|
||||
match &node {
|
||||
Ok(obj2) => {
|
||||
eprintln!("obj2: {:#?}", obj2);
|
||||
},
|
||||
Err(e) => eprintln!("Unable to parse generated kbin back to `Node`: {:#?}", e),
|
||||
};
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
#[cfg(not(feature = "serde"))]
|
||||
fn test_serde_node(_contents: &[u8]) -> std::io::Result<()> {
|
||||
Ok(())
|
||||
}
|
||||
|
||||
fn main() -> std::io::Result<()> {
|
||||
pretty_env_logger::init();
|
||||
|
||||
@@ -138,13 +214,7 @@ fn main() -> std::io::Result<()> {
|
||||
let buf = kbinxml::to_binary_with_options(options, &element).map_err(display_err)?;
|
||||
compare_slice(&buf, &contents);
|
||||
|
||||
let value = from_bytes::<Node>(&contents);
|
||||
match &value {
|
||||
Ok(obj2) => {
|
||||
eprintln!("obj2: {:#?}", obj2);
|
||||
},
|
||||
Err(e) => eprintln!("Unable to parse generated kbin back to `Value`: {:#?}", e),
|
||||
};
|
||||
test_serde_node(&contents)?;
|
||||
} else {
|
||||
let contents = str::from_utf8(&contents).expect("Unable to interpret file contents as UTF-8");
|
||||
let element: Element = contents.parse().expect("Unable to construct DOM for input text XML");
|
||||
@@ -158,44 +228,7 @@ fn main() -> std::io::Result<()> {
|
||||
stdout.lock().write_all(&buf)?;
|
||||
}
|
||||
} else {
|
||||
let obj = Testing {
|
||||
the_attr: "the_value".to_string(),
|
||||
hi: 12,
|
||||
ok: [12, 24, 48],
|
||||
hhh: (55, 66),
|
||||
hhg: (55, 66),
|
||||
foo: "foobarbaz".to_string(),
|
||||
testing2: Testing2 {
|
||||
hi: 32423,
|
||||
ho: 32000,
|
||||
vu: vec![33, 255, 254],
|
||||
opt: None,
|
||||
opt2: Some(111),
|
||||
ip: Ipv4Addr::new(127, 0, 0, 1),
|
||||
extra: ExtraNodes::new(),
|
||||
},
|
||||
};
|
||||
let bytes = to_bytes(&obj).unwrap();
|
||||
eprintln!("bytes: {:02x?}", bytes);
|
||||
|
||||
let mut file = File::create("testing.kbin")?;
|
||||
file.write_all(&bytes)?;
|
||||
|
||||
let obj2 = from_bytes::<Testing>(&bytes);
|
||||
match &obj2 {
|
||||
Ok(obj2) => eprintln!("obj2: {:#?}", obj2),
|
||||
Err(e) => eprintln!("Unable to parse generated kbin back to struct: {:#?}", e),
|
||||
};
|
||||
|
||||
let value = from_bytes::<Node>(&bytes);
|
||||
match &value {
|
||||
Ok(obj2) => eprintln!("obj2: {:#?}", obj2),
|
||||
Err(e) => eprintln!("Unable to parse generated kbin back to `Value`: {:#?}", e),
|
||||
};
|
||||
|
||||
if obj2.is_ok() && value.is_ok() {
|
||||
Printer::run(&bytes).unwrap();
|
||||
}
|
||||
test_serde()?;
|
||||
}
|
||||
Ok(())
|
||||
}
|
||||
|
||||
106
src/error.rs
106
src/error.rs
@@ -1,10 +1,8 @@
|
||||
use std::error::Error as StdError;
|
||||
use std::fmt::{self, Display};
|
||||
use std::fmt;
|
||||
use std::result::Result as StdResult;
|
||||
use std::string::FromUtf8Error;
|
||||
|
||||
use failure::{Backtrace, Compat, Context, Fail};
|
||||
use serde::{de, ser};
|
||||
use failure::{Backtrace, Context, Fail};
|
||||
use value::Value;
|
||||
|
||||
use node_types::{KbinType, StandardType};
|
||||
@@ -163,56 +161,66 @@ impl From<FromUtf8Error> for KbinError {
|
||||
}
|
||||
}
|
||||
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Error {
|
||||
Message(String),
|
||||
StaticMessage(&'static str),
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "serde")] {
|
||||
use std::error::Error as StdError;
|
||||
use std::fmt::Display;
|
||||
|
||||
Wrapped(Compat<KbinError>),
|
||||
}
|
||||
use failure::Compat;
|
||||
use serde::{de, ser};
|
||||
|
||||
impl ser::Error for Error {
|
||||
fn custom<T: Display>(msg: T) -> Self {
|
||||
Error::Message(msg.to_string())
|
||||
}
|
||||
}
|
||||
#[derive(Clone, Debug)]
|
||||
pub enum Error {
|
||||
Message(String),
|
||||
StaticMessage(&'static str),
|
||||
|
||||
impl de::Error for Error {
|
||||
fn custom<T: Display>(msg: T) -> Self {
|
||||
Error::Message(msg.to_string())
|
||||
}
|
||||
}
|
||||
Wrapped(Compat<KbinError>),
|
||||
}
|
||||
|
||||
impl Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.write_str(StdError::description(self))
|
||||
}
|
||||
}
|
||||
impl ser::Error for Error {
|
||||
fn custom<T: Display>(msg: T) -> Self {
|
||||
Error::Message(msg.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl StdError for Error {
|
||||
fn description(&self) -> &str {
|
||||
match *self {
|
||||
Error::Message(ref msg) => msg,
|
||||
Error::StaticMessage(ref msg) => msg,
|
||||
Error::Wrapped(ref err) => err.description(),
|
||||
impl de::Error for Error {
|
||||
fn custom<T: Display>(msg: T) -> Self {
|
||||
Error::Message(msg.to_string())
|
||||
}
|
||||
}
|
||||
|
||||
impl Display for Error {
|
||||
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
|
||||
f.write_str(StdError::description(self))
|
||||
}
|
||||
}
|
||||
|
||||
impl StdError for Error {
|
||||
fn description(&self) -> &str {
|
||||
match *self {
|
||||
Error::Message(ref msg) => msg,
|
||||
Error::StaticMessage(ref msg) => msg,
|
||||
Error::Wrapped(ref err) => err.description(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<KbinError> for Error {
|
||||
fn from(inner: KbinError) -> Self {
|
||||
Error::Wrapped(inner.compat())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<KbinErrorKind> for Error {
|
||||
fn from(inner: KbinErrorKind) -> Self {
|
||||
Error::Wrapped(KbinError::from(inner).compat())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Context<KbinErrorKind>> for Error {
|
||||
fn from(inner: Context<KbinErrorKind>) -> Self {
|
||||
Error::Wrapped(KbinError::from(inner).compat())
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl From<KbinError> for Error {
|
||||
fn from(inner: KbinError) -> Self {
|
||||
Error::Wrapped(inner.compat())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<KbinErrorKind> for Error {
|
||||
fn from(inner: KbinErrorKind) -> Self {
|
||||
Error::Wrapped(KbinError::from(inner).compat())
|
||||
}
|
||||
}
|
||||
|
||||
impl From<Context<KbinErrorKind>> for Error {
|
||||
fn from(inner: Context<KbinErrorKind>) -> Self {
|
||||
Error::Wrapped(KbinError::from(inner).compat())
|
||||
}
|
||||
}
|
||||
|
||||
25
src/lib.rs
25
src/lib.rs
@@ -6,12 +6,11 @@ extern crate encoding;
|
||||
extern crate indexmap;
|
||||
extern crate minidom;
|
||||
extern crate rustc_hex;
|
||||
extern crate serde_bytes;
|
||||
|
||||
#[macro_use] extern crate cfg_if;
|
||||
#[macro_use] extern crate failure;
|
||||
#[macro_use] extern crate lazy_static;
|
||||
#[macro_use] extern crate log;
|
||||
#[macro_use] extern crate serde;
|
||||
|
||||
use std::fmt::Write as FmtWrite;
|
||||
|
||||
@@ -32,9 +31,6 @@ mod to_element;
|
||||
mod value;
|
||||
mod writer;
|
||||
|
||||
mod de;
|
||||
mod ser;
|
||||
|
||||
use node::NodeDefinition;
|
||||
use node_types::StandardType;
|
||||
|
||||
@@ -44,14 +40,27 @@ pub use encoding_type::EncodingType;
|
||||
pub use printer::Printer;
|
||||
pub use reader::Reader;
|
||||
pub use error::{KbinError, KbinErrorKind, Result};
|
||||
pub use node::{ExtraNodes, Node, NodeCollection};
|
||||
pub use node::{Node, NodeCollection};
|
||||
pub use options::Options;
|
||||
pub use de::from_bytes;
|
||||
pub use ser::to_bytes;
|
||||
pub use to_element::ToElement;
|
||||
pub use value::Value;
|
||||
pub use writer::{Writer, Writeable};
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "serde")] {
|
||||
extern crate serde_bytes;
|
||||
|
||||
#[macro_use] extern crate serde;
|
||||
|
||||
mod de;
|
||||
mod ser;
|
||||
|
||||
pub use de::from_bytes;
|
||||
pub use node::ExtraNodes;
|
||||
pub use ser::to_bytes;
|
||||
}
|
||||
}
|
||||
|
||||
const SIGNATURE: u8 = 0xA0;
|
||||
|
||||
const SIG_COMPRESSED: u8 = 0x42;
|
||||
|
||||
@@ -7,16 +7,22 @@ use indexmap::IndexMap;
|
||||
use value::Value;
|
||||
|
||||
mod collection;
|
||||
pub(crate) mod de;
|
||||
mod definition;
|
||||
mod extra;
|
||||
mod marshal;
|
||||
mod ser;
|
||||
|
||||
pub use self::collection::NodeCollection;
|
||||
pub use self::definition::{Key, NodeData, NodeDefinition};
|
||||
pub use self::extra::ExtraNodes;
|
||||
pub use self::marshal::{Marshal, MarshalDeserializer};
|
||||
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "serde")] {
|
||||
pub(crate) mod de;
|
||||
mod extra;
|
||||
mod marshal;
|
||||
mod ser;
|
||||
|
||||
pub use self::marshal::{Marshal, MarshalDeserializer};
|
||||
pub use self::extra::ExtraNodes;
|
||||
}
|
||||
}
|
||||
|
||||
/*
|
||||
match children.entry(key) {
|
||||
|
||||
@@ -5,16 +5,22 @@ use std::str::FromStr;
|
||||
use byteorder::{BigEndian, ByteOrder, WriteBytesExt};
|
||||
use failure::{Fail, ResultExt};
|
||||
use rustc_hex::FromHex;
|
||||
use serde::de::{Deserialize, Deserializer, DeserializeSeed};
|
||||
use serde_bytes::ByteBuf;
|
||||
|
||||
use error::{KbinError, KbinErrorKind};
|
||||
use node::Node;
|
||||
use node::de::NodeSeed;
|
||||
use node_types::{self, StandardType};
|
||||
|
||||
pub(crate) mod de;
|
||||
mod ser;
|
||||
cfg_if! {
|
||||
if #[cfg(feature = "serde")] {
|
||||
use serde::de::{Deserialize, Deserializer, DeserializeSeed};
|
||||
use serde_bytes::ByteBuf;
|
||||
|
||||
pub(crate) mod de;
|
||||
mod ser;
|
||||
|
||||
use node::de::NodeSeed;
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! tuple {
|
||||
(
|
||||
@@ -480,6 +486,7 @@ macro_rules! construct_types {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
impl<'de> DeserializeSeed<'de> for StandardType {
|
||||
type Value = Value;
|
||||
|
||||
@@ -509,6 +516,7 @@ impl From<Vec<u8>> for Value {
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(feature = "serde")]
|
||||
impl From<ByteBuf> for Value {
|
||||
fn from(value: ByteBuf) -> Value {
|
||||
Value::Binary(value.into())
|
||||
|
||||
Reference in New Issue
Block a user