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
use crate::io::RenderKotlin;
use crate::spec::{CodeBlock, Name};
use crate::tokens;
/// Function argument, consists of pair name and value.
/// If name is [None] is considered as positional argument.
///
/// Set of function arguments could is usually represented as [Vec<Argument>].
///
/// # Examples
///
/// ## Named argument
/// ```rust
/// use kotlin_poet_rs::spec::{CodeBlock, Name, Argument};
/// use kotlin_poet_rs::io::RenderKotlin;
///
/// let argument = Argument::new_named(
/// Name::from("name"), CodeBlock::atom("value")
/// );
///
/// assert_eq!(argument.render_string(), "name = value");
/// ```
///
/// ## Positional argument
/// ```rust
/// use kotlin_poet_rs::spec::{CodeBlock, Name, Argument};
/// use kotlin_poet_rs::io::RenderKotlin;
///
/// let argument = Argument::new_positional(
/// CodeBlock::statement("value")
/// );
///
/// assert_eq!(argument.render_string(), "value");
/// ```
#[derive(Debug, Clone)]
pub struct Argument {
name: Option<Name>,
value: CodeBlock,
}
impl Argument {
/// Creates new positional argument
pub fn new_positional<CodeBlockLike: Into<CodeBlock>>(value: CodeBlockLike) -> Self {
Argument {
name: None,
value: value.into(),
}
}
/// Creates new named argument
pub fn new_named<NameLike: Into<Name>, CodeBlockLike: Into<CodeBlock>>(name: NameLike, value: CodeBlockLike) -> Self {
Argument {
name: Some(name.into()),
value: value.into(),
}
}
}
impl RenderKotlin for Argument {
fn render_into(&self, block: &mut CodeBlock) {
if let Some(name) = &self.name {
block.push_renderable(name);
block.push_space();
block.push_static_atom(tokens::ASSIGN);
block.push_space();
}
block.push_atom(self.value.to_string().as_str());
}
}
#[cfg(test)]
mod tests {
use crate::spec::{CodeBlock, Name, Argument};
use crate::io::RenderKotlin;
#[test]
fn test_rendering() {
let argument = Argument::new_positional(CodeBlock::statement("value"));
assert_eq!(argument.render_string(), "value");
let argument = Argument::new_named(Name::from("name"), CodeBlock::atom("value"), );
assert_eq!(argument.render_string(), "name = value");
}
}