datafusion_doc/lib.rs
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 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
#[allow(rustdoc::broken_intra_doc_links)]
/// Documentation for use by [`ScalarUDFImpl`](ScalarUDFImpl),
/// [`AggregateUDFImpl`](AggregateUDFImpl) and [`WindowUDFImpl`](WindowUDFImpl) functions.
///
/// See the [`DocumentationBuilder`] to create a new [`Documentation`] struct.
///
/// The DataFusion [SQL function documentation] is automatically generated from these structs.
/// The name of the udf will be pulled from the [`ScalarUDFImpl::name`](ScalarUDFImpl::name),
/// [`AggregateUDFImpl::name`](AggregateUDFImpl::name) or [`WindowUDFImpl::name`](WindowUDFImpl::name)
/// function as appropriate.
///
/// All strings in the documentation are required to be
/// in [markdown format](https://www.markdownguide.org/basic-syntax/).
///
/// Currently, documentation only supports a single language
/// thus all text should be in English.
///
/// [SQL function documentation]: https://datafusion.apache.org/user-guide/sql/index.html
#[derive(Debug, Clone)]
pub struct Documentation {
/// The section in the documentation where the UDF will be documented
pub doc_section: DocSection,
/// The description for the UDF
pub description: String,
/// A brief example of the syntax. For example "ascii(str)"
pub syntax_example: String,
/// A sql example for the UDF, usually in the form of a sql prompt
/// query and output. It is strongly recommended to provide an
/// example for anything but the most basic UDF's
pub sql_example: Option<String>,
/// Arguments for the UDF which will be displayed in array order.
/// Left member of a pair is the argument name, right is a
/// description for the argument
pub arguments: Option<Vec<(String, String)>>,
/// A list of alternative syntax examples for a function
pub alternative_syntax: Option<Vec<String>>,
/// Related functions if any. Values should match the related
/// udf's name exactly. Related udf's must be of the same
/// UDF type (scalar, aggregate or window) for proper linking to
/// occur
pub related_udfs: Option<Vec<String>>,
}
impl Documentation {
/// Returns a new [`DocumentationBuilder`] with no options set.
pub fn builder(
doc_section: DocSection,
description: impl Into<String>,
syntax_example: impl Into<String>,
) -> DocumentationBuilder {
DocumentationBuilder::new_with_details(doc_section, description, syntax_example)
}
/// Output the `Documentation` struct in form of custom Rust documentation attributes
/// It is useful to semi automate during tmigration of UDF documentation
/// generation from code based to attribute based and can be safely removed after
pub fn to_doc_attribute(&self) -> String {
let mut result = String::new();
result.push_str("#[user_doc(");
// Doc Section
result.push_str(
format!(
"\n doc_section({}label = \"{}\"{}),",
if !self.doc_section.include {
"include = \"false\", "
} else {
""
},
self.doc_section.label,
self.doc_section
.description
.map(|s| format!(", description = \"{}\"", s))
.unwrap_or_default(),
)
.as_ref(),
);
// Description
result.push_str(format!("\n description=\"{}\",", self.description).as_ref());
// Syntax Example
result.push_str(
format!("\n syntax_example=\"{}\",", self.syntax_example).as_ref(),
);
// SQL Example
result.push_str(
&self
.sql_example
.clone()
.map(|s| format!("\n sql_example = r#\"{}\"#,", s))
.unwrap_or_default(),
);
let st_arg_token = " expression to operate on. Can be a constant, column, or function, and any combination of operators.";
// Standard Arguments
if let Some(args) = self.arguments.clone() {
args.iter().for_each(|(name, value)| {
if value.contains(st_arg_token) {
if name.starts_with("The ") {
result.push_str(format!("\n standard_argument(\n name = \"{}\"),", name).as_ref());
} else {
result.push_str(format!("\n standard_argument(\n name = \"{}\",\n prefix = \"{}\"\n ),", name, value.replace(st_arg_token, "")).as_ref());
}
}
});
}
// Arguments
if let Some(args) = self.arguments.clone() {
args.iter().for_each(|(name, value)| {
if !value.contains(st_arg_token) {
result.push_str(format!("\n argument(\n name = \"{}\",\n description = \"{}\"\n ),", name, value).as_ref());
}
});
}
if let Some(alt_syntax) = self.alternative_syntax.clone() {
alt_syntax.iter().for_each(|syntax| {
result.push_str(
format!("\n alternative_syntax = \"{}\",", syntax).as_ref(),
);
});
}
// Related UDFs
if let Some(related_udf) = self.related_udfs.clone() {
related_udf.iter().for_each(|udf| {
result
.push_str(format!("\n related_udf(name = \"{}\"),", udf).as_ref());
});
}
result.push_str("\n)]");
result
}
}
#[derive(Debug, Clone, PartialEq)]
pub struct DocSection {
/// True to include this doc section in the public
/// documentation, false otherwise
pub include: bool,
/// A display label for the doc section. For example: "Math Expressions"
pub label: &'static str,
/// An optional description for the doc section
pub description: Option<&'static str>,
}
impl Default for DocSection {
/// Returns a "default" Doc section.
///
/// This is suitable for user defined functions that do not appear in the
/// DataFusion documentation.
fn default() -> Self {
Self {
include: true,
label: "Default",
description: None,
}
}
}
/// A builder for [`Documentation`]'s.
///
/// Example:
///
/// ```rust
///
/// # fn main() {
/// use datafusion_doc::{DocSection, Documentation};
/// let doc_section = DocSection {
/// include: true,
/// label: "Display Label",
/// description: None,
/// };
///
/// let documentation = Documentation::builder(doc_section, "Add one to an int32".to_owned(), "add_one(2)".to_owned())
/// .with_argument("arg_1", "The int32 number to add one to")
/// .build();
/// # }
pub struct DocumentationBuilder {
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 DocumentationBuilder {
#[allow(clippy::new_without_default)]
#[deprecated(
since = "44.0.0",
note = "please use `DocumentationBuilder::new_with_details` instead"
)]
pub fn new() -> Self {
Self::new_with_details(DocSection::default(), "<no description>", "<no example>")
}
/// Creates a new [`DocumentationBuilder`] with all required fields
pub fn new_with_details(
doc_section: DocSection,
description: impl Into<String>,
syntax_example: impl Into<String>,
) -> Self {
Self {
doc_section,
description: description.into(),
syntax_example: syntax_example.into(),
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 = doc_section;
self
}
pub fn with_description(mut self, description: impl Into<String>) -> Self {
self.description = description.into();
self
}
pub fn with_syntax_example(mut self, syntax_example: impl Into<String>) -> Self {
self.syntax_example = 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
}
/// Adds documentation for a specific argument to the documentation.
///
/// Arguments are displayed in the order they are added.
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
}
/// Add a standard "expression" argument to the documentation
///
/// The argument is rendered like below if Some() is passed through:
///
/// ```text
/// <arg_name>:
/// <expression_type> expression to operate on. Can be a constant, column, or function, and any combination of operators.
/// ```
///
/// The argument is rendered like below if None is passed through:
///
/// ```text
/// <arg_name>:
/// The expression to operate on. Can be a constant, column, or function, and any combination of operators.
/// ```
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
}
/// Build the documentation from provided components
///
/// Panics if `doc_section`, `description` or `syntax_example` is not set
pub fn build(self) -> Documentation {
let Self {
doc_section,
description,
syntax_example,
sql_example,
arguments,
alternative_syntax,
related_udfs,
} = self;
Documentation {
doc_section,
description,
syntax_example,
sql_example,
arguments,
alternative_syntax,
related_udfs,
}
}
}