Struct php_codegen::function::Function
source · pub struct Function {
pub documentation: Option<Document>,
pub attributes: Vec<AttributeGroup>,
pub name: String,
pub parameters: Vec<Parameter>,
pub return_type: Option<DataType>,
pub body: Body,
}
Fields§
§documentation: Option<Document>
§attributes: Vec<AttributeGroup>
§name: String
§parameters: Vec<Parameter>
§return_type: Option<DataType>
§body: Body
Implementations§
source§impl Function
impl Function
sourcepub fn new<T: ToString>(name: T) -> Self
pub fn new<T: ToString>(name: T) -> Self
Examples found in repository?
examples/simple.rs (line 12)
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
fn main() {
let file = File::new()
.namespaced("App")
.declare("strict_types", 1)
.function(
Function::new("format")
.document(
Document::new()
.text("Format a string with the given arguments using sprintf.")
.empty_line()
.tag("param", "non-empty-string $template")
.empty_line()
.simple_tag("pure"),
)
.parameter(Parameter::new("template").typed(DataType::String))
.parameter(
Parameter::new("args")
.variadic()
.typed(DataType::Union(vec![
DataType::Integer,
DataType::Float,
DataType::String,
DataType::Null,
])),
)
.returns(DataType::String)
.body(vec![
"return sprintf($template, ...array_map(",
" static fn ($arg) => is_float($arg) ? number_format($arg, 2) : $arg,",
" array_filter($args, static fn ($arg) => $arg !== null)",
"));",
]),
);
print!("{file}");
}
sourcepub fn document(self, documentation: Document) -> Self
pub fn document(self, documentation: Document) -> Self
Examples found in repository?
examples/simple.rs (lines 13-20)
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
fn main() {
let file = File::new()
.namespaced("App")
.declare("strict_types", 1)
.function(
Function::new("format")
.document(
Document::new()
.text("Format a string with the given arguments using sprintf.")
.empty_line()
.tag("param", "non-empty-string $template")
.empty_line()
.simple_tag("pure"),
)
.parameter(Parameter::new("template").typed(DataType::String))
.parameter(
Parameter::new("args")
.variadic()
.typed(DataType::Union(vec![
DataType::Integer,
DataType::Float,
DataType::String,
DataType::Null,
])),
)
.returns(DataType::String)
.body(vec![
"return sprintf($template, ...array_map(",
" static fn ($arg) => is_float($arg) ? number_format($arg, 2) : $arg,",
" array_filter($args, static fn ($arg) => $arg !== null)",
"));",
]),
);
print!("{file}");
}
pub fn attributes(self, attributes: AttributeGroup) -> Self
sourcepub fn parameter(self, parameter: Parameter) -> Self
pub fn parameter(self, parameter: Parameter) -> Self
Examples found in repository?
examples/simple.rs (line 21)
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
fn main() {
let file = File::new()
.namespaced("App")
.declare("strict_types", 1)
.function(
Function::new("format")
.document(
Document::new()
.text("Format a string with the given arguments using sprintf.")
.empty_line()
.tag("param", "non-empty-string $template")
.empty_line()
.simple_tag("pure"),
)
.parameter(Parameter::new("template").typed(DataType::String))
.parameter(
Parameter::new("args")
.variadic()
.typed(DataType::Union(vec![
DataType::Integer,
DataType::Float,
DataType::String,
DataType::Null,
])),
)
.returns(DataType::String)
.body(vec![
"return sprintf($template, ...array_map(",
" static fn ($arg) => is_float($arg) ? number_format($arg, 2) : $arg,",
" array_filter($args, static fn ($arg) => $arg !== null)",
"));",
]),
);
print!("{file}");
}
sourcepub fn returns(self, return_type: DataType) -> Self
pub fn returns(self, return_type: DataType) -> Self
Examples found in repository?
examples/simple.rs (line 32)
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
fn main() {
let file = File::new()
.namespaced("App")
.declare("strict_types", 1)
.function(
Function::new("format")
.document(
Document::new()
.text("Format a string with the given arguments using sprintf.")
.empty_line()
.tag("param", "non-empty-string $template")
.empty_line()
.simple_tag("pure"),
)
.parameter(Parameter::new("template").typed(DataType::String))
.parameter(
Parameter::new("args")
.variadic()
.typed(DataType::Union(vec![
DataType::Integer,
DataType::Float,
DataType::String,
DataType::Null,
])),
)
.returns(DataType::String)
.body(vec![
"return sprintf($template, ...array_map(",
" static fn ($arg) => is_float($arg) ? number_format($arg, 2) : $arg,",
" array_filter($args, static fn ($arg) => $arg !== null)",
"));",
]),
);
print!("{file}");
}
sourcepub fn body<T: Into<Body>>(self, body: T) -> Self
pub fn body<T: Into<Body>>(self, body: T) -> Self
Examples found in repository?
examples/simple.rs (lines 33-38)
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
fn main() {
let file = File::new()
.namespaced("App")
.declare("strict_types", 1)
.function(
Function::new("format")
.document(
Document::new()
.text("Format a string with the given arguments using sprintf.")
.empty_line()
.tag("param", "non-empty-string $template")
.empty_line()
.simple_tag("pure"),
)
.parameter(Parameter::new("template").typed(DataType::String))
.parameter(
Parameter::new("args")
.variadic()
.typed(DataType::Union(vec![
DataType::Integer,
DataType::Float,
DataType::String,
DataType::Null,
])),
)
.returns(DataType::String)
.body(vec![
"return sprintf($template, ...array_map(",
" static fn ($arg) => is_float($arg) ? number_format($arg, 2) : $arg,",
" array_filter($args, static fn ($arg) => $arg !== null)",
"));",
]),
);
print!("{file}");
}