use odk_schema::{CompiledSchema, SourceMap, SourceSpan};
use std::path::Path;
pub fn generate_source_map(
schema: &CompiledSchema,
tsp_file: &str,
source_file: &str,
) -> SourceMap {
let mut map = SourceMap::new(tsp_file);
map.add_source(source_file);
let mut current_line: u32 = 1;
current_line += 3; current_line += schema.imports.len() as u32;
current_line += 2;
for union in &schema.unions {
if let Some(ref source) = union.source {
map.add_mapping(current_line, 0, source.clone());
}
current_line += 1; current_line += 1; current_line += union.variants.len() as u32;
current_line += 2; }
for model in &schema.models {
if let Some(ref source) = model.source {
map.add_mapping(current_line, 0, source.clone());
}
current_line += 1; current_line += model.extensions.decorators().len() as u32; current_line += 1; current_line += 1;
for field in &model.fields {
if let Some(ref source) = field.source {
map.add_mapping(current_line, 2, source.clone());
}
current_line += 1;
}
current_line += 2; }
for raw_block in &schema.raw_blocks {
if let Some(ref source) = raw_block.source {
map.add_mapping(current_line, 0, source.clone());
}
let raw_lines = raw_block.content.lines().count() as u32;
current_line += raw_lines + 1; }
map
}
pub fn write_source_map(map: &SourceMap, tsp_path: &Path) -> std::io::Result<()> {
let map_path = tsp_path.with_extension("tsp.map");
let json = map
.to_json()
.map_err(|e| std::io::Error::new(std::io::ErrorKind::InvalidData, e.to_string()))?;
std::fs::write(map_path, json)
}
#[cfg(test)]
mod tests {
use super::*;
use odk_schema::{CompiledField, CompiledModel, CompiledType};
#[test]
fn test_generate_source_map() {
let mut schema = CompiledSchema::new("TestAPI");
let mut user = CompiledModel::new("User").with_source(SourceSpan::new("schema.yaml", 5, 2));
user.add_field(
CompiledField::new("id", CompiledType::string()).with_source(SourceSpan::new(
"schema.yaml",
7,
4,
)),
);
schema.add_model(user);
let map = generate_source_map(&schema, "main.tsp", "schema.yaml");
assert_eq!(map.sources.len(), 1);
assert!(map.mappings.len() > 0);
}
}