use crate::error::Result;
use serde::{Deserialize, Serialize};
#[derive(Debug)]
pub struct SourceMapGenerator;
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SourceMap {
pub version: u32,
pub sources: Vec<String>,
pub names: Vec<String>,
pub mappings: String,
pub file: Option<String>,
pub source_root: Option<String>,
pub sources_content: Option<Vec<String>>,
}
#[derive(Debug, Clone)]
pub struct SourceMapOptions {
pub inline: bool,
pub file: Option<String>,
pub source_root: Option<String>,
pub sources_content: bool,
}
impl SourceMapGenerator {
pub fn new() -> Self {
Self
}
pub fn generate(
&self,
input: &str,
output: &str,
options: &SourceMapOptions,
) -> Result<SourceMap> {
let sources = if options.sources_content {
vec![input.to_string()]
} else {
vec![]
};
let sources_content = if options.sources_content {
Some(vec![input.to_string()])
} else {
None
};
let mappings = self.generate_mappings(input, output);
Ok(SourceMap {
version: 3,
sources,
names: Vec::new(),
mappings,
file: options.file.clone(),
source_root: options.source_root.clone(),
sources_content,
})
}
fn generate_mappings(&self, input: &str, output: &str) -> String {
let input_lines = input.lines().count();
let output_lines = output.lines().count();
if input_lines == output_lines {
format!("AAAA;{}", input_lines)
} else {
"AAAA".to_string()
}
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_source_map_generator() {
let generator = SourceMapGenerator::new();
let options = SourceMapOptions {
inline: false,
file: None,
source_root: None,
sources_content: true,
};
let result = generator.generate("input", "output", &options);
assert!(result.is_ok());
}
}