You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
use serde::{de::DeserializeOwned,Deserialize};#[derive(PartialEq,Eq,Deserialize,Debug)]structOuterWithOpt{alpha:u64,beta:u64,#[serde(flatten)]inner:Option<Inner>,}implOuterWithOpt{fnnew_with_inner(alpha:u64,beta:u64,gamma:u64,delta:u64) -> Self{let inner = Some(Inner{ gamma, delta });Self{ alpha, beta, inner }}fnnew_without_inner(alpha:u64,beta:u64) -> Self{let inner = None;Self{ alpha, beta, inner }}}#[derive(PartialEq,Eq,Deserialize,Debug)]structInner{gamma:u64,delta:u64,}fnassert_de_error<T:DeserializeOwned + std::fmt::Debug>(toml_src:&str,expexted_error:&str){let err = match toml::from_str::<T>(toml_src){Err(e) => e,Ok(val) => {panic!("Deserialization should have raised error, but produced a result instead.\n Expected error message: {expexted_error}\n Result produced instead: {val:?}");}};assert_eq!(err.message(), expexted_error)}fnassert_de_succ<T:DeserializeOwned + std::fmt::Debug + Eq>(toml_src:&str,expexted_value:&T){let res = toml::from_str::<T>(toml_src).unwrap();assert_eq!(&res, expexted_value)}#[test]fntest() -> anyhow::Result<()>{let t_ = "";let t_a = "alpha = 42\n";let t_ab = "alpha = 42\n beta = 42\n";let t_abg = "alpha = 42\n beta = 42\n gamma = 42";let t_abgd = "alpha = 42\n beta = 42\n gamma = 42\n delta = 42";let t_ab_d = "alpha = 42\n beta = 42\n delta = 42";// OuterWithOpt can be successfully deserializedlet withopt_set = OuterWithOpt::new_with_inner(42,42,42,42);let withopt_unset = OuterWithOpt::new_without_inner(42,42);assert_de_succ(t_abgd,&withopt_set);assert_de_succ(t_ab,&withopt_unset);// Deserializing OuterNoOpt with missing fields in outer struct produces appropriate errorsassert_de_error::<OuterWithOpt>(t_,"missing field `alpha`");assert_de_error::<OuterWithOpt>(t_a,"missing field `beta`");// Deserializing OuterNoOpt with missing fields in inner struct produces appropriate errors// when only some but not all of Inner's required fields are setassert_de_error::<OuterWithOpt>(t_abg,"missing field `delta`. Field `gamma` must be used togetehr with field `delta`. Either remove field `gamma` or add field `delta`.");assert_de_error::<OuterWithOpt>(t_ab_d,"missing field `gamma`. Field `delta` must be used togetehr with field `gamma`. Either remove field `delta` or add field `gamma`.");Ok(())}
Test case error output
thread 'test' panicked at lib.rs:32:13:
Deserialization should have raised error, but produced a result instead.
Expected error message: missing field `delta`. Field `gamma` must be used togetehr with field `delta`. Either remove field `gamma` or add field `delta`.
Result produced instead: OuterWithOpt { alpha: 42, beta: 42, inner: None }
The text was updated successfully, but these errors were encountered:
Feature request demonstrated through accompanying test cases.
Thanks for maintaining this fantastic library :)
Demonstrating test case
Self-contained, easily executed accompanying material as a gist
Test case error output
The text was updated successfully, but these errors were encountered: