Skip to main content

from_str

Function from_str 

Source
pub fn from_str<T: DeserializeOwned + Serialize + Validate>(
    text: &str,
) -> Result<T, DecodeError>
Expand description

Decodes and validates one payload from JSON text, strictly: what a test, a fixture or a replayed log line needs, without a transport handler around it.

use ocpp_kit::{decode, v2_1};

let alarm: v2_1::NotifyDERAlarmRequest = decode::from_str(
    r#"{"controlType":"VoltWatt","timestamp":"2026-11-20T17:05:00Z"}"#,
)?;
assert_eq!(alarm.control_type, v2_1::DERControl::VoltWatt);

// Validated, not just parsed: a missing member is the error a CSMS would answer with.
let error = decode::from_str::<v2_1::NotifyDERAlarmRequest>(r#"{"controlType":"VoltWatt"}"#)
    .unwrap_err();
assert_eq!(error.kind, decode::DecodeErrorKind::Occurrence);

ยงErrors

DecodeError exactly as decode_payload with DecodeOptions::strict reports it.