microcad_lang/parse/
doc_block.rs

1// Copyright © 2025 The µcad authors <info@ucad.xyz>
2// SPDX-License-Identifier: AGPL-3.0-or-later
3
4use crate::{parse::*, parser::*, syntax::DocBlock};
5
6impl Parse for DocBlock {
7    fn parse(pair: Pair) -> ParseResult<Self> {
8        Parser::ensure_rule(&pair, Rule::doc_block);
9
10        let lines: Vec<_> = pair
11            .inner()
12            .filter_map(|pair| {
13                if pair.as_rule() == Rule::doc_comment {
14                    Some(String::from(
15                        pair.as_str()
16                            .trim()
17                            .strip_prefix("/// ")
18                            .unwrap_or_default(),
19                    ))
20                } else {
21                    None
22                }
23            })
24            .collect();
25
26        let (summary, details) =
27            if let Some(pos) = lines.iter().position(|line| line.trim().is_empty()) {
28                (lines[0..pos].join("\n"), Some(lines[pos..].join("\n")))
29            } else {
30                (lines.join("\n"), None)
31            };
32
33        Ok(Self {
34            summary,
35            details,
36            src_ref: pair.into(),
37        })
38    }
39}