pub struct Class {
pub docstring: Option<Comment>,
pub name: String,
pub block: Block,
pub inherit_from: Option<String>,
}Fields§
§docstring: Option<Comment>§name: String§block: Block§inherit_from: Option<String>Implementations§
Source§impl Class
impl Class
Sourcepub fn new<T: Display>(name: T, parent: &Block) -> Class
pub fn new<T: Display>(name: T, parent: &Block) -> Class
Examples found in repository?
examples/classes.rs (line 7)
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}pub fn inherit_from<T: Display>(&mut self, inherit: T)
Sourcepub fn add_statement(&mut self, statement: Statement)
pub fn add_statement(&mut self, statement: Statement)
Examples found in repository?
examples/classes.rs (line 20)
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}Sourcepub fn add_docstring<T: Display>(&mut self, doc: T)
pub fn add_docstring<T: Display>(&mut self, doc: T)
Examples found in repository?
examples/classes.rs (line 8)
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§
Auto Trait Implementations§
impl Freeze for Class
impl RefUnwindSafe for Class
impl Send for Class
impl Sync for Class
impl Unpin for Class
impl UnwindSafe for Class
Blanket Implementations§
Source§impl<T> BorrowMut<T> for Twhere
T: ?Sized,
impl<T> BorrowMut<T> for Twhere
T: ?Sized,
Source§fn borrow_mut(&mut self) -> &mut T
fn borrow_mut(&mut self) -> &mut T
Mutably borrows from an owned value. Read more