use crate::error::FormatError;
use lex_core::lex::ast::Document;
use std::collections::HashMap;
pub enum SerializedDocument {
Text(String),
Binary(Vec<u8>),
}
impl SerializedDocument {
pub fn into_bytes(self) -> Vec<u8> {
match self {
SerializedDocument::Text(text) => text.into_bytes(),
SerializedDocument::Binary(bytes) => bytes,
}
}
}
pub trait Format: Send + Sync {
fn name(&self) -> &str;
fn description(&self) -> &str {
""
}
fn file_extensions(&self) -> &[&str] {
&[]
}
fn supports_parsing(&self) -> bool {
false
}
fn supports_serialization(&self) -> bool {
false
}
fn parse(&self, _source: &str) -> Result<Document, FormatError> {
Err(FormatError::NotSupported(format!(
"Format '{}' does not support parsing",
self.name()
)))
}
fn serialize(&self, _doc: &Document) -> Result<String, FormatError> {
Err(FormatError::NotSupported(format!(
"Format '{}' does not support serialization",
self.name()
)))
}
fn serialize_with_options(
&self,
doc: &Document,
options: &HashMap<String, String>,
) -> Result<SerializedDocument, FormatError> {
if options.is_empty() {
self.serialize(doc).map(SerializedDocument::Text)
} else {
Err(FormatError::NotSupported(format!(
"Format '{}' does not support extra parameters",
self.name()
)))
}
}
}