Skip to main content

docattest_rust/language/
mod.rs

1//! What needs no project: the greeting, and the questions about Rust text (blocks, doc
2//! comments, code spans, fact expressions).
3
4pub(crate) mod blocks;
5pub(crate) mod doc;
6pub(crate) mod text;
7
8pub use doc::RUST_WORDS;
9pub use text::{ASSERTING_MACROS, ASSERTING_METHODS};
10
11use crate::protocol::{
12    AdapterName, Answer, Capability, DocProse, Example, ExpressionShape, Hello, SourceFiles, SourceText,
13    VERSION, Vocabulary, decode,
14};
15use serde::Deserialize;
16use serde_json::{Value, json};
17
18/// The verdict on a block tagged `network` when the network is off.
19pub(crate) const NETWORK_SKIPPED: &str =
20    "network example skipped: the network is off (set DOCATTEST_NETWORK=1 to run it)";
21
22/// The info-string word by which a block says that panicking is what it proves.
23pub(crate) const SHOULD_PANIC: &str = "should_panic";
24
25/// Whether a project path is a Rust source file.
26pub(crate) fn is_source(page: &str) -> bool {
27    page.ends_with(".rs")
28}
29
30/// The greeting: what is fixed for the Rust adapter, whatever the project.
31pub fn hello() -> Hello {
32    let words = |words: &[&str]| words.iter().map(|word| word.to_string()).collect();
33    Hello {
34        protocol: VERSION,
35        adapter: AdapterName { name: "rust".into(), version: env!("CARGO_PKG_VERSION").into() },
36        block_languages: words(&["rust", "rs"]),
37        source_files: SourceFiles { include: words(&["**/*.rs"]), exclude: Vec::new() },
38        // Where cargo builds, which is never the project's own.
39        ignored_names: words(&["target"]),
40        capabilities: vec![Capability::Tests, Capability::Observables, Capability::Affected],
41        info_words: words(&[SHOULD_PANIC]),
42        vocabulary: Vocabulary { no_tests: "it declares no test function".into() },
43    }
44}
45
46#[derive(Deserialize)]
47struct Files {
48    files: Vec<SourceText>,
49}
50
51#[derive(Deserialize)]
52struct Code {
53    code: String,
54}
55
56#[derive(Deserialize)]
57struct Examples {
58    examples: Vec<Code>,
59}
60
61#[derive(Deserialize)]
62struct Spans {
63    spans: Vec<String>,
64    symbols: Vec<String>,
65}
66
67#[derive(Deserialize)]
68struct Expressions {
69    expressions: Vec<String>,
70}
71
72/// Answers a question about text; `None` for any other method.
73pub fn answer(method: &str, params: Value) -> Option<Answer> {
74    Some(match method {
75        "assertions" => decode::<Examples>(params)
76            .map(|p| json!(p.examples.iter().map(|e| text::asserts(&e.code)).collect::<Vec<bool>>())),
77        "docComments" => decode::<Files>(params)
78            .map(|p| json!(p.files.iter().flat_map(doc::examples).collect::<Vec<Example>>())),
79        "docProse" => decode::<Files>(params)
80            .map(|p| json!(p.files.iter().map(doc::prose).collect::<Vec<Vec<DocProse>>>())),
81        "mentions" => decode::<Spans>(params).map(|p| json!(text::mentions(&p.spans, &p.symbols))),
82        "expressions" => decode::<Expressions>(params)
83            .map(|p| json!(p.expressions.iter().map(|e| text::shape(e)).collect::<Vec<ExpressionShape>>())),
84        _ => return None,
85    })
86}