use crate::binary::{self, MAGIC};
use crate::text::SyntaxError;
pub fn is_binary(bytes: &[u8]) -> bool {
bytes.starts_with(MAGIC)
}
pub fn text(bytes: &[u8]) -> Result<String, String> {
if !is_binary(bytes) {
return String::from_utf8(bytes.to_vec()).map_err(|e| e.to_string());
}
let value = binary::decode(bytes).map_err(|e| e.to_string())?;
Ok(format!("{}\n", crate::patch::format(&value, 0)))
}
pub fn binary(text: &str) -> Result<Vec<u8>, SyntaxError> {
crate::text::with_comments(text).map(|v| binary::encode(&v))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn a_source_round_trips_through_its_binary_form() {
let text = "{\n // Hard.\n hardness: 3,\n drop: \"@item:ruby\"\n}";
let bytes = binary(text).unwrap();
assert!(is_binary(&bytes));
let shown = super::text(&bytes).unwrap();
assert!(
shown.contains("$hardness") && shown.contains("@item:ruby"),
"{shown}"
);
assert_eq!(
binary(&shown).unwrap(),
bytes,
"its text is the same file again"
);
assert_eq!(
super::text(text.as_bytes()).unwrap(),
text,
"text stays text"
);
}
}