use crate::schema::canonical::error::ParseError;
use serde_json::Value;
pub fn to_text(payload: &str) -> Result<String, ParseError> {
if payload.is_empty() {
return Err(ParseError::Empty);
}
Ok(payload.to_string())
}
pub fn from_text(s: &str) -> Result<String, ParseError> {
if s.is_empty() {
return Err(ParseError::Empty);
}
Ok(s.to_string())
}
pub fn to_json(payload: &str) -> Result<Value, ParseError> {
if payload.is_empty() {
return Err(ParseError::Empty);
}
Ok(Value::String(payload.to_string()))
}
pub fn from_json(value: &Value) -> Result<String, ParseError> {
match value {
Value::String(s) if s.is_empty() => Err(ParseError::Empty),
Value::String(s) => Ok(s.clone()),
_ => Err(ParseError::TypeField {
expected: "string",
field: None,
}),
}
}
#[cfg(test)]
mod tests {
use super::*;
use proptest::prelude::*;
use test_r::test;
proptest! {
#![proptest_config(ProptestConfig::with_cases(64))]
#[test]
fn text_round_trip(p in "[a-z]+: let s = to_text(&p).expect("to_text");
let back = from_text(&s).expect("from_text");
prop_assert_eq!(back, p);
}
#[test]
fn json_round_trip(p in "[a-z]+://[a-z0-9.]+(/[a-z0-9]+)*") {
let j = to_json(&p).expect("to_json");
let back = from_json(&j).expect("from_json");
prop_assert_eq!(back, p);
}
}
#[test]
fn empty_text_rejected() {
assert_eq!(from_text(""), Err(ParseError::Empty));
}
#[test]
fn empty_text_encode_rejected() {
assert_eq!(to_text(""), Err(ParseError::Empty));
}
#[test]
fn empty_json_encode_rejected() {
assert_eq!(to_json(""), Err(ParseError::Empty));
}
#[test]
fn json_wrong_type() {
assert_eq!(
from_json(&Value::Number(serde_json::Number::from(1u64))),
Err(ParseError::TypeField {
expected: "string",
field: None,
})
);
}
#[test]
fn json_empty_string_rejected() {
assert_eq!(
from_json(&Value::String(String::new())),
Err(ParseError::Empty)
);
}
}