Skip to main content

validate_comments

Function validate_comments 

Source
pub fn validate_comments(
    source_len: usize,
    api_version: u32,
    comments: &[PluginComment],
) -> Result<(), ValidationError>
Expand description

Check everything a plugin returned before the host acts on any of it.

The version is checked first, so a guest built against another revision is refused before its spans are read at all. The spans must then each be non-empty, inside the source, and start no earlier than the previous one ended — the same contract transform_spans enforces, checked here so a host can refuse a plugin’s whole answer rather than a single span of it. The count is capped as well: no source can hold more comments than it has bytes, plus one.

§Errors

Returns the ValidationError for the first fault found. On any error the batch is refused whole; there is no partial acceptance.

§Examples

use ocomment_core::{ByteSpan, CommentKind};
use ocomment_plugin_sdk::{API_VERSION, PluginComment, ValidationError, validate_comments};

let comment = |start, end| PluginComment {
    span: ByteSpan::new(start, end),
    kind: CommentKind::Line,
};

assert!(validate_comments(10, API_VERSION, &[comment(0, 2), comment(2, 10)]).is_ok());
assert_eq!(
    validate_comments(10, API_VERSION, &[comment(4, 7), comment(6, 8)]),
    Err(ValidationError::Overlap),
);
assert_eq!(
    validate_comments(10, API_VERSION, &[comment(9, 11)]),
    Err(ValidationError::OutOfBounds { source_len: 10 }),
);