openc2 0.2.0

Rust types for OpenC2 commands and responses
Documentation
use std::fmt::Debug;

use serde::{Deserialize, Serialize};

/// An abstraction over different value types, such as JSON or CBOR.
pub trait Value: Sized {
    /// The error type returned when converting to or from the value type.
    type Error: Debug + serde::de::Error + serde::ser::Error;

    /// Serialize a value to the value type.
    fn from_typed<V: Serialize>(value: &V) -> Result<Self, Self::Error>;

    /// Deserialize a value from the value type.
    fn to_typed<'a, T: Deserialize<'a>>(&'a self) -> Result<T, Self::Error>;
}

#[cfg(feature = "json")]
impl Value for serde_json::Value {
    type Error = serde_json::Error;

    fn from_typed<V: Serialize>(value: &V) -> Result<Self, Self::Error> {
        serde_json::to_value(value)
    }

    fn to_typed<'a, T: Deserialize<'a>>(&'a self) -> Result<T, Self::Error> {
        T::deserialize(self)
    }
}

#[cfg(feature = "cbor")]
impl Value for serde_cbor::Value {
    type Error = serde_cbor::Error;

    fn from_typed<V: Serialize>(value: &V) -> Result<Self, Self::Error> {
        serde_cbor::value::to_value(value)
    }

    fn to_typed<'a, T: Deserialize<'a>>(&'a self) -> Result<T, Self::Error> {
        todo!()
    }
}