1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
use std::collections::HashMap;

/// Formats the documentation for a completion.
/// example: How the completion is expected to be used.
///
/// # Example
///
/// ```
/// use psl_core::datamodel_connector::format_completion_docs;
///
/// let doc = format_completion_docs(
///     r#"relationMode = "foreignKeys" | "prisma""#,
///     r#"Sets the global relation mode for relations."#,
///     None,
/// );
///
/// assert_eq!(
///     "```prisma\nrelationMode = \"foreignKeys\" | \"prisma\"\n```\n___\nSets the global relation mode for relations.\n\n",
///     &doc
/// );
/// ```
pub fn format_completion_docs(example: &str, description: &str, params: Option<HashMap<&str, &str>>) -> String {
    let param_docs: String = match params {
        Some(params) => params
            .into_iter()
            .map(|(param_label, param_doc)| format!("_@param_ {param_label} {param_doc}"))
            .collect::<Vec<String>>()
            .join("\n"),
        None => Default::default(),
    };

    format!("```prisma\n{example}\n```\n___\n{description}\n\n{param_docs}")
}