Skip to main content

transform_spans

Function transform_spans 

Source
pub fn transform_spans(
    source: &[u8],
    language: Language,
    spans: &[(ByteSpan, CommentKind)],
    options: TransformOptions,
) -> Result<TransformResult, ExternalSpanError>
Expand description

Transform a scanner’s already-classified comment spans using the same policy, layout, edit validation, and source-map engine as built-in scans.

This is the safe hand-off point for declarative or WASM scanners. Spans must be non-empty, sorted, non-overlapping, and contained in source.

The report that comes back carries no diagnostics and is always valid: the external scanner, not this crate, judged whether the source lexed.

§Errors

Returns ExternalSpanError naming the first span that reaches past the end of source, covers no bytes, or starts before its predecessor ends, or reporting a keep_regex/remove_regex entry that would not compile. Nothing is transformed when validation fails.

§Examples

use ocomment_core::{
    ByteSpan, CommentKind, ExternalSpanError, Language, TransformOptions, transform_spans,
};

let source = b"a/* ordinary */b/* directive */";
let result = transform_spans(
    source,
    Language::Unknown,
    &[
        (ByteSpan::new(1, 15), CommentKind::Block),
        (ByteSpan::new(16, source.len()), CommentKind::Directive),
    ],
    TransformOptions::default(),
)
.unwrap();
// The same policy the built-in scanners get: the directive is kept.
assert_eq!(result.output, b"a b/* directive */");

let bad = transform_spans(
    source,
    Language::Unknown,
    &[(ByteSpan::new(2, source.len() + 1), CommentKind::Block)],
    TransformOptions::default(),
);
assert!(matches!(bad, Err(ExternalSpanError::OutOfBounds { .. })));