use datafusion_common::exec_err;
use datafusion_common::Result;
#[derive(Debug, Clone)]
pub struct Documentation {
pub doc_section: DocSection,
pub description: String,
pub syntax_example: String,
pub sql_example: Option<String>,
pub arguments: Option<Vec<(String, String)>>,
pub alternative_syntax: Option<Vec<String>>,
pub related_udfs: Option<Vec<String>>,
}
impl Documentation {
pub fn builder() -> DocumentationBuilder {
DocumentationBuilder::new()
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct DocSection {
pub include: bool,
pub label: &'static str,
pub description: Option<&'static str>,
}
pub struct DocumentationBuilder {
pub doc_section: Option<DocSection>,
pub description: Option<String>,
pub syntax_example: Option<String>,
pub sql_example: Option<String>,
pub arguments: Option<Vec<(String, String)>>,
pub alternative_syntax: Option<Vec<String>>,
pub related_udfs: Option<Vec<String>>,
}
impl DocumentationBuilder {
pub fn new() -> Self {
Self {
doc_section: None,
description: None,
syntax_example: None,
sql_example: None,
arguments: None,
alternative_syntax: None,
related_udfs: None,
}
}
pub fn with_doc_section(mut self, doc_section: DocSection) -> Self {
self.doc_section = Some(doc_section);
self
}
pub fn with_description(mut self, description: impl Into<String>) -> Self {
self.description = Some(description.into());
self
}
pub fn with_syntax_example(mut self, syntax_example: impl Into<String>) -> Self {
self.syntax_example = Some(syntax_example.into());
self
}
pub fn with_sql_example(mut self, sql_example: impl Into<String>) -> Self {
self.sql_example = Some(sql_example.into());
self
}
pub fn with_argument(
mut self,
arg_name: impl Into<String>,
arg_description: impl Into<String>,
) -> Self {
let mut args = self.arguments.unwrap_or_default();
args.push((arg_name.into(), arg_description.into()));
self.arguments = Some(args);
self
}
pub fn with_standard_argument(
self,
arg_name: impl Into<String>,
expression_type: Option<&str>,
) -> Self {
let description = format!(
"{} expression to operate on. Can be a constant, column, or function, and any combination of operators.",
expression_type.unwrap_or("The")
);
self.with_argument(arg_name, description)
}
pub fn with_alternative_syntax(mut self, syntax_name: impl Into<String>) -> Self {
let mut alternative_syntax_array = self.alternative_syntax.unwrap_or_default();
alternative_syntax_array.push(syntax_name.into());
self.alternative_syntax = Some(alternative_syntax_array);
self
}
pub fn with_related_udf(mut self, related_udf: impl Into<String>) -> Self {
let mut related = self.related_udfs.unwrap_or_default();
related.push(related_udf.into());
self.related_udfs = Some(related);
self
}
pub fn build(self) -> Result<Documentation> {
let Self {
doc_section,
description,
syntax_example,
sql_example,
arguments,
alternative_syntax,
related_udfs,
} = self;
if doc_section.is_none() {
return exec_err!("Documentation must have a doc section");
}
if description.is_none() {
return exec_err!("Documentation must have a description");
}
if syntax_example.is_none() {
return exec_err!("Documentation must have a syntax_example");
}
Ok(Documentation {
doc_section: doc_section.unwrap(),
description: description.unwrap(),
syntax_example: syntax_example.unwrap(),
sql_example,
arguments,
alternative_syntax,
related_udfs,
})
}
}
impl Default for DocumentationBuilder {
fn default() -> Self {
Self::new()
}
}