in yaml_test_runner/src/step/do.rs [852:928]
fn generate_body(endpoint: &ApiEndpoint, v: &Yaml) -> anyhow::Result<Option<Tokens>> {
match v {
Yaml::Null => Ok(None),
Yaml::String(s) => {
let json = {
let json = replace_set(s);
replace_i64(json)
};
if endpoint.supports_nd_body() {
// a newline delimited API body may be expressed
// as a scalar string literal style where line breaks are significant (using |)
// or where lines breaks are folded to an empty space unless it ends on an
// empty or a more-indented line (using >)
// see https://yaml.org/spec/1.2/spec.html#id2760844
//
// need to trim the trailing newline to be able to differentiate...
let contains_newlines = json.trim_end_matches('\n').contains('\n');
let split = if contains_newlines {
json.split('\n').collect::<Vec<_>>()
} else {
json.split(char::is_whitespace).collect::<Vec<_>>()
};
let values: Vec<Tokens> = split
.into_iter()
.filter(|s| !s.is_empty())
.map(|s| {
let ident = syn::Ident::from(s);
quote! { JsonBody::from(json!(#ident)) }
})
.collect();
Ok(Some(quote!(.body(vec![#(#values),*]))))
} else {
let ident = syn::Ident::from(json);
Ok(Some(quote!(.body(json!{#ident}))))
}
}
_ => {
let mut s = String::new();
{
let mut emitter = YamlEmitter::new(&mut s);
emitter.dump(v).unwrap();
}
if endpoint.supports_nd_body() {
let values: Vec<serde_json::Value> = serde_yaml::from_str(&s)?;
let json: Vec<Tokens> = values
.iter()
.map(|value| {
let mut json = serde_json::to_string(&value).unwrap();
if value.is_string() {
json = replace_set(&json);
let ident = syn::Ident::from(json);
quote!(Box::new(String::from(#ident)))
} else {
json = replace_set(json);
json = replace_i64(json);
let ident = syn::Ident::from(json);
quote!(Box::new(JsonBody::from(json!(#ident))))
}
})
.collect();
Ok(Some(
quote!(.body({ let mut v: Vec<Box<dyn Body>> = Vec::new(); v.append(&mut vec![ #(#json),* ]); v })),
))
} else {
let value: serde_json::Value = serde_yaml::from_str(&s)?;
let mut json = serde_json::to_string_pretty(&value)?;
json = replace_set(json);
json = replace_i64(json);
let ident = syn::Ident::from(json);
Ok(Some(quote!(.body(json!{#ident}))))
}
}
}
}