Struct Function

Source
pub struct Function {
    pub docstring: Option<Comment>,
    pub name: String,
    pub args: Vec<Argument>,
    /* private fields */
}

Fields§

§docstring: Option<Comment>§name: String§args: Vec<Argument>

Implementations§

Source§

impl Function

Source

pub fn new<T: Display>(name: T, args: Vec<Argument>, parent: &Block) -> Function

Examples found in repository?
examples/hello_world.rs (lines 8-13)
5fn main() {
6    let mut program = Block::new();
7
8    let mut function = Function::new("hello",
9                                     vec![Argument::new("name",
10                                                        Some("\"World\""),
11                                                        Some("str"),
12                                                        Some("The name to say Hello to"))],
13                                     &program);
14    let arguments = vec![Statement::unnamed_variable("Hello"),
15                         Statement::variable("name")];
16    let function_call = Statement::function_call("print", arguments);
17    function.add_statement(function_call);
18    function.add_docstring("hello is a function that prints \"Hello\" with the given \
19                            name argument.");
20
21    program.add_function(function);
22    let function_call = Statement::function_call(
23      "hello",
24      vec![
25        Statement::unnamed_variable("Chris")
26      ]);
27    program.add_statement(function_call);
28    let expected = r#"def hello(name="World"):
29    """
30    hello is a function that prints "Hello" with the given name argument.
31
32    :param name: str The name to say Hello to
33    """
34    print("Hello", name)
35
36hello("Chris")
37"#;
38    let actual = format!("{}",program);
39    assert_eq!(expected, actual);
40
41    println!("{}", actual);
42
43}
More examples
Hide additional examples
examples/classes.rs (line 13)
5fn main() {
6    let mut program = Block::new();
7    let mut class = Class::new("Greeter", &program);
8    class.add_docstring("Greeter is a class to say Hello!");
9    let arguments = vec![
10      Argument::new("self", None, None, None),
11      Argument::new("name", Some("\"World\""), Some("str"), Some("The name to greet")),
12    ];
13    let mut init = Function::new("__init__", arguments, &class.block);
14
15    init.add_docstring("Set up name variable for the Greeter");
16    init.add_statement(
17      Statement::Assignment(
18        Assignment::new(
19          Argument::variable("self.name"), Statement::variable("name"))));
20    class.add_statement(Statement::Function(init));
21    let arguments = vec![
22      Argument::new("self", None, None, None),
23    ];
24    let mut hello = Function::new("hello", arguments, &class.block);
25    hello.add_docstring("Say hello to self.name");
26    let print = Statement::function_call(
27      "print",
28      vec![
29        Statement::unnamed_variable("Hello"),
30        Statement::variable("self.name"),
31        Statement::unnamed_variable("!"),
32      ]);
33    hello.add_statement(print);
34    class.add_statement(Statement::Function(hello));
35    program.add_statement(Statement::Class(class));
36    program.add_statement(
37        Statement::Assignment(Assignment::new(
38            Argument::variable("g"),
39            Statement::function_call(
40                "Greeter",
41                vec![
42                    Statement::KeywordArgument(KeywordArgument::new("name", "\"Chris\""))
43                ]
44            )
45    )));
46    let function_call = Statement::function_call(
47      "g.hello",
48      vec![]);
49    program.add_statement(function_call);
50    let expected = r#"class Greeter:
51    """
52    Greeter is a class to say Hello!
53    """
54
55    def __init__(self, name="World"):
56        """
57        Set up name variable for the Greeter
58
59        :param name: str The name to greet
60        """
61        self.name = name
62
63    def hello(self):
64        """
65        Say hello to self.name
66        """
67        print("Hello", self.name, "!")
68
69
70g = Greeter(name="Chris")
71g.hello()
72"#;
73    let actual = format!("{}", program);
74
75    assert_eq!(expected, actual);
76
77    println!("{}", actual);
78}
Source

pub fn add_statement(&mut self, function_call: Statement)

Examples found in repository?
examples/hello_world.rs (line 17)
5fn main() {
6    let mut program = Block::new();
7
8    let mut function = Function::new("hello",
9                                     vec![Argument::new("name",
10                                                        Some("\"World\""),
11                                                        Some("str"),
12                                                        Some("The name to say Hello to"))],
13                                     &program);
14    let arguments = vec![Statement::unnamed_variable("Hello"),
15                         Statement::variable("name")];
16    let function_call = Statement::function_call("print", arguments);
17    function.add_statement(function_call);
18    function.add_docstring("hello is a function that prints \"Hello\" with the given \
19                            name argument.");
20
21    program.add_function(function);
22    let function_call = Statement::function_call(
23      "hello",
24      vec![
25        Statement::unnamed_variable("Chris")
26      ]);
27    program.add_statement(function_call);
28    let expected = r#"def hello(name="World"):
29    """
30    hello is a function that prints "Hello" with the given name argument.
31
32    :param name: str The name to say Hello to
33    """
34    print("Hello", name)
35
36hello("Chris")
37"#;
38    let actual = format!("{}",program);
39    assert_eq!(expected, actual);
40
41    println!("{}", actual);
42
43}
More examples
Hide additional examples
examples/classes.rs (lines 16-19)
5fn main() {
6    let mut program = Block::new();
7    let mut class = Class::new("Greeter", &program);
8    class.add_docstring("Greeter is a class to say Hello!");
9    let arguments = vec![
10      Argument::new("self", None, None, None),
11      Argument::new("name", Some("\"World\""), Some("str"), Some("The name to greet")),
12    ];
13    let mut init = Function::new("__init__", arguments, &class.block);
14
15    init.add_docstring("Set up name variable for the Greeter");
16    init.add_statement(
17      Statement::Assignment(
18        Assignment::new(
19          Argument::variable("self.name"), Statement::variable("name"))));
20    class.add_statement(Statement::Function(init));
21    let arguments = vec![
22      Argument::new("self", None, None, None),
23    ];
24    let mut hello = Function::new("hello", arguments, &class.block);
25    hello.add_docstring("Say hello to self.name");
26    let print = Statement::function_call(
27      "print",
28      vec![
29        Statement::unnamed_variable("Hello"),
30        Statement::variable("self.name"),
31        Statement::unnamed_variable("!"),
32      ]);
33    hello.add_statement(print);
34    class.add_statement(Statement::Function(hello));
35    program.add_statement(Statement::Class(class));
36    program.add_statement(
37        Statement::Assignment(Assignment::new(
38            Argument::variable("g"),
39            Statement::function_call(
40                "Greeter",
41                vec![
42                    Statement::KeywordArgument(KeywordArgument::new("name", "\"Chris\""))
43                ]
44            )
45    )));
46    let function_call = Statement::function_call(
47      "g.hello",
48      vec![]);
49    program.add_statement(function_call);
50    let expected = r#"class Greeter:
51    """
52    Greeter is a class to say Hello!
53    """
54
55    def __init__(self, name="World"):
56        """
57        Set up name variable for the Greeter
58
59        :param name: str The name to greet
60        """
61        self.name = name
62
63    def hello(self):
64        """
65        Say hello to self.name
66        """
67        print("Hello", self.name, "!")
68
69
70g = Greeter(name="Chris")
71g.hello()
72"#;
73    let actual = format!("{}", program);
74
75    assert_eq!(expected, actual);
76
77    println!("{}", actual);
78}
Source

pub fn add_docstring<T: Display>(&mut self, doc: T)

Examples found in repository?
examples/hello_world.rs (lines 18-19)
5fn main() {
6    let mut program = Block::new();
7
8    let mut function = Function::new("hello",
9                                     vec![Argument::new("name",
10                                                        Some("\"World\""),
11                                                        Some("str"),
12                                                        Some("The name to say Hello to"))],
13                                     &program);
14    let arguments = vec![Statement::unnamed_variable("Hello"),
15                         Statement::variable("name")];
16    let function_call = Statement::function_call("print", arguments);
17    function.add_statement(function_call);
18    function.add_docstring("hello is a function that prints \"Hello\" with the given \
19                            name argument.");
20
21    program.add_function(function);
22    let function_call = Statement::function_call(
23      "hello",
24      vec![
25        Statement::unnamed_variable("Chris")
26      ]);
27    program.add_statement(function_call);
28    let expected = r#"def hello(name="World"):
29    """
30    hello is a function that prints "Hello" with the given name argument.
31
32    :param name: str The name to say Hello to
33    """
34    print("Hello", name)
35
36hello("Chris")
37"#;
38    let actual = format!("{}",program);
39    assert_eq!(expected, actual);
40
41    println!("{}", actual);
42
43}
More examples
Hide additional examples
examples/classes.rs (line 15)
5fn main() {
6    let mut program = Block::new();
7    let mut class = Class::new("Greeter", &program);
8    class.add_docstring("Greeter is a class to say Hello!");
9    let arguments = vec![
10      Argument::new("self", None, None, None),
11      Argument::new("name", Some("\"World\""), Some("str"), Some("The name to greet")),
12    ];
13    let mut init = Function::new("__init__", arguments, &class.block);
14
15    init.add_docstring("Set up name variable for the Greeter");
16    init.add_statement(
17      Statement::Assignment(
18        Assignment::new(
19          Argument::variable("self.name"), Statement::variable("name"))));
20    class.add_statement(Statement::Function(init));
21    let arguments = vec![
22      Argument::new("self", None, None, None),
23    ];
24    let mut hello = Function::new("hello", arguments, &class.block);
25    hello.add_docstring("Say hello to self.name");
26    let print = Statement::function_call(
27      "print",
28      vec![
29        Statement::unnamed_variable("Hello"),
30        Statement::variable("self.name"),
31        Statement::unnamed_variable("!"),
32      ]);
33    hello.add_statement(print);
34    class.add_statement(Statement::Function(hello));
35    program.add_statement(Statement::Class(class));
36    program.add_statement(
37        Statement::Assignment(Assignment::new(
38            Argument::variable("g"),
39            Statement::function_call(
40                "Greeter",
41                vec![
42                    Statement::KeywordArgument(KeywordArgument::new("name", "\"Chris\""))
43                ]
44            )
45    )));
46    let function_call = Statement::function_call(
47      "g.hello",
48      vec![]);
49    program.add_statement(function_call);
50    let expected = r#"class Greeter:
51    """
52    Greeter is a class to say Hello!
53    """
54
55    def __init__(self, name="World"):
56        """
57        Set up name variable for the Greeter
58
59        :param name: str The name to greet
60        """
61        self.name = name
62
63    def hello(self):
64        """
65        Say hello to self.name
66        """
67        print("Hello", self.name, "!")
68
69
70g = Greeter(name="Chris")
71g.hello()
72"#;
73    let actual = format!("{}", program);
74
75    assert_eq!(expected, actual);
76
77    println!("{}", actual);
78}

Trait Implementations§

Source§

impl Clone for Function

Source§

fn clone(&self) -> Function

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for Function

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl Display for Function

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.