mod error;
mod formatter;
mod options;
mod script;
mod template;
pub use error::*;
pub use formatter::*;
pub use options::*;
pub use vize_carton::Allocator;
#[inline]
pub fn format_sfc(source: &str, options: &FormatOptions) -> Result<FormatResult, FormatError> {
let allocator = Allocator::with_capacity(source.len() * 2);
format_sfc_with_allocator(source, options, &allocator)
}
#[inline]
pub fn format_sfc_with_allocator(
source: &str,
options: &FormatOptions,
allocator: &Allocator,
) -> Result<FormatResult, FormatError> {
let formatter = GlyphFormatter::new(options, allocator);
formatter.format(source)
}
#[inline]
pub fn format_script(source: &str, options: &FormatOptions) -> Result<String, FormatError> {
let allocator = Allocator::with_capacity(source.len() * 2);
script::format_script_content(source, options, &allocator)
}
#[inline]
pub fn format_template(source: &str, options: &FormatOptions) -> Result<String, FormatError> {
template::format_template_content(source, options)
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_format_simple_sfc() {
let source = r#"<script setup>
import {ref} from 'vue'
const count=ref(0)
</script>
<template>
<div>{{ count }}</div>
</template>
"#;
let options = FormatOptions::default();
let result = format_sfc(source, &options).unwrap();
assert!(result.code.contains("ref"));
assert!(result.code.contains("vue"));
assert!(result.code.contains("count"));
assert!(result.code.contains("ref(0)"));
}
#[test]
fn test_format_script_only() {
let source = "const x=1;const y={a:1,b:2}";
let options = FormatOptions::default();
let result = format_script(source, &options).unwrap();
assert!(result.contains("const x"));
assert!(result.contains("const y"));
assert!(result.contains("a:"));
assert!(result.contains("b:"));
}
#[test]
fn test_format_sfc_preserves_structure() {
let source = r#"<script setup lang="ts">
const msg = 'hello'
</script>
<template>
<div>{{ msg }}</div>
</template>
<style scoped>
.container { color: red; }
</style>
"#;
let options = FormatOptions::default();
let result = format_sfc(source, &options).unwrap();
assert!(result.code.contains("<script setup lang=\"ts\">"));
assert!(result.code.contains("</script>"));
assert!(result.code.contains("<template>"));
assert!(result.code.contains("</template>"));
assert!(result.code.contains("<style scoped>"));
assert!(result.code.contains("</style>"));
}
#[test]
fn test_allocator_reuse() {
let allocator = Allocator::with_capacity(4096);
let options = FormatOptions::default();
let source1 = "<script>const a = 1</script>";
let source2 = "<script>const b = 2</script>";
let result1 = format_sfc_with_allocator(source1, &options, &allocator).unwrap();
let result2 = format_sfc_with_allocator(source2, &options, &allocator).unwrap();
assert!(result1.code.contains("const a"));
assert!(result2.code.contains("const b"));
}
}