#[cfg(feature = "json")]
use std::io::Write;
#[cfg(feature = "json")]
use qubit_budget::json::JsonDecodeLimits;
#[cfg(feature = "json")]
use qubit_budget::json::JsonDecodeSession;
#[cfg(feature = "json")]
use qubit_budget::json::JsonEncodeLimits;
#[cfg(feature = "json")]
use qubit_budget::json::JsonEncodeSession;
#[cfg(feature = "json")]
use qubit_json::decode::JsonDecoder;
#[cfg(feature = "json")]
use qubit_json::encode::JsonEncoder;
use serde::Serialize;
use serde::Serializer;
use super::ValueWireEncodeError;
use super::WireShapeRef;
use super::value_wire_payload_ref_v1::validate_value;
use super::value_wire_payload_ref_v1::validate_values;
#[cfg(feature = "json")]
use super::value_wire_payload_v1_seed::ValueWirePayloadV1Seed;
use crate::MultiValues;
use crate::Value;
use crate::ValueContainer;
#[cfg(feature = "json")]
use crate::ValueWireDecodeError;
#[must_use]
#[derive(Debug, Clone, PartialEq, Eq, Hash)]
pub struct ValueWirePayloadV1 {
value: ValueContainer,
}
impl ValueWirePayloadV1 {
pub(in crate::value_wire) const fn from_decoded(value: ValueContainer) -> Self {
Self { value }
}
fn try_new(value: ValueContainer) -> Result<Self, ValueWireEncodeError> {
match &value {
ValueContainer::Scalar(value) => validate_value(value)?,
ValueContainer::Collection(values) => validate_values(values)?,
}
Ok(Self { value })
}
#[cfg(feature = "json")]
#[must_use = "the V1 JSON profile should be applied to a budget"]
#[inline(always)]
pub fn default_json_decode_limits() -> JsonDecodeLimits {
super::default_json_decode_limits()
}
#[cfg(feature = "json")]
#[must_use = "the V1 JSON profile should be applied to an encode session"]
#[inline(always)]
pub fn default_json_encode_limits() -> JsonEncodeLimits {
super::default_json_encode_limits()
}
#[cfg(feature = "json")]
#[inline(always)]
pub fn decode_json_slice(input: &[u8]) -> Result<Self, ValueWireDecodeError> {
Self::decode_json_slice_with_limits(input, Self::default_json_decode_limits())
}
#[cfg(feature = "json")]
#[inline]
pub fn decode_json_slice_with_limits(input: &[u8], limits: JsonDecodeLimits) -> Result<Self, ValueWireDecodeError> {
let session = JsonDecodeSession::from_limits(limits);
JsonDecoder::new(session)
.decode_seed_utf8(ValueWirePayloadV1Seed::new(), input)
.map_err(ValueWireDecodeError::from)
}
#[cfg(feature = "json")]
#[inline(always)]
pub fn to_json_vec(&self) -> Result<Vec<u8>, ValueWireEncodeError> {
self.to_json_vec_with_limits(Self::default_json_encode_limits())
}
#[cfg(feature = "json")]
pub fn to_json_vec_with_limits(&self, limits: JsonEncodeLimits) -> Result<Vec<u8>, ValueWireEncodeError> {
let session = JsonEncodeSession::from_limits(limits);
JsonEncoder::new(session)
.to_vec(self)
.map_err(ValueWireEncodeError::from)
}
#[cfg(feature = "json")]
#[inline(always)]
pub fn to_json_writer<W>(&self, writer: W) -> Result<(), ValueWireEncodeError>
where
W: Write,
{
self.to_json_writer_with_limits(writer, Self::default_json_encode_limits())
}
#[cfg(feature = "json")]
pub fn to_json_writer_with_limits<W>(&self, writer: W, limits: JsonEncodeLimits) -> Result<(), ValueWireEncodeError>
where
W: Write,
{
let session = JsonEncodeSession::from_limits(limits);
JsonEncoder::new(session)
.write_buffered(writer, self)
.map_err(ValueWireEncodeError::from)
}
#[must_use = "the borrowed value container should be used"]
#[inline(always)]
pub const fn container(&self) -> &ValueContainer {
&self.value
}
#[inline(always)]
pub fn into_container(self) -> ValueContainer {
self.value
}
}
impl TryFrom<Value> for ValueWirePayloadV1 {
type Error = ValueWireEncodeError;
#[inline(always)]
fn try_from(value: Value) -> Result<Self, Self::Error> {
Self::try_new(value.into())
}
}
impl TryFrom<MultiValues> for ValueWirePayloadV1 {
type Error = ValueWireEncodeError;
#[inline(always)]
fn try_from(value: MultiValues) -> Result<Self, Self::Error> {
Self::try_new(value.into())
}
}
impl TryFrom<ValueContainer> for ValueWirePayloadV1 {
type Error = ValueWireEncodeError;
#[inline(always)]
fn try_from(value: ValueContainer) -> Result<Self, Self::Error> {
Self::try_new(value)
}
}
impl From<ValueWirePayloadV1> for ValueContainer {
#[inline(always)]
fn from(value: ValueWirePayloadV1) -> Self {
value.into_container()
}
}
impl Serialize for ValueWirePayloadV1 {
#[inline(always)]
fn serialize<S>(&self, serializer: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
WireShapeRef::from(&self.value).serialize(serializer)
}
}