Struct tealr::TypeWalker
source · pub struct TypeWalker {
pub given_types: Vec<TypeGenerator>,
pub global_instances_off: Vec<GlobalInstance>,
pub extra_page: Vec<ExtraPage>,
/* private fields */
}
Expand description
This generates the .d.tl files
Fields§
§given_types: Vec<TypeGenerator>
All the types that are currently registered by the TypeWalker
global_instances_off: Vec<GlobalInstance>
list of items that
extra_page: Vec<ExtraPage>
list of extra pages that need to be generated.
Implementations§
source§impl TypeWalker
impl TypeWalker
sourcepub fn new() -> Self
pub fn new() -> Self
creates the TypeWalker
Examples found in repository?
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
fn main() -> Result<()> {
let file_contents = TypeWalker::new()
//tells it that you want to include the Example type
//chain extra calls to include more types
.process_type::<Example>()
//generate the file
.to_json()
.expect("serde_json failed to serialize our data");
//normally you would now save the file somewhere.
println!("{}\n ", file_contents);
//lua is still using position parameters as normal.
let lua = Lua::new();
let globals = lua.globals();
globals.set("test", Example {})?;
let code = "test:example_method(\"field_1 is a string\", 3)";
lua.load(code).set_name("test?").eval()?;
Ok(())
}
More examples
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
fn main() -> Result<()> {
let file_contents = TypeWalker::new()
//tells it that you want to include the Example type
//chain extra calls to include more types
.process_type::<Example>()
//generate the file
.to_json()
.expect("serde_json failed to serialize our data");
//normally you would now save the file somewhere.
println!("{}\n ", file_contents);
//lua is still using position parameters as normal.
tealr::rlu::rlua::Lua::new().context(|ctx| {
let globals = ctx.globals();
globals.set("test", Example {})?;
let code = "test:example_method(\"field_1 is a string\", 3)";
ctx.load(code).set_name("test?")?.eval()?;
Ok(())
})?;
Ok(())
}
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
fn main() -> Result<()> {
let file_contents = TypeWalker::new()
//tells it that you want to include the Example type
//chain extra calls to include more types
.process_type::<Example>()
//generate the file
.to_json()
.expect("serde_json failed to serialize our data");
//normally you would now save the file somewhere.
//however for this example we just print it.
println!("{}\n ", file_contents);
//how you pass this type to lua hasn't changed:
let lua = Lua::new();
let globals = lua.globals();
globals.set("test", Example(1))?;
let code = "
print(test.help())
print(\"----\")
print(test.help(\"example_method\"))
print(test:example_method(1))
print(test:example_method_mut(2,\"test\"))
print(test.example_function({}))
print(test.example_function_mut(true))
";
lua.load(code).set_name("test?").eval()?;
Ok(())
}
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
fn main() -> Result<()> {
let file_contents = TypeWalker::new()
//tells it that you want to include the Example type
//chain extra calls to include more types
.process_type::<Example>()
//generate the file
.to_json()
.expect("serde_json failed to serialize our data");
//normally you would now save the file somewhere.
//however for this example we just print it.
println!("{}\n ", file_contents);
//how you pass this type to lua hasn't changed:
let lua = Lua::new();
lua.context(|lua_ctx| {
let globals = lua_ctx.globals();
globals.set("test", Example {})?;
let code = "
print(test:example_method(1))
print(test:example_method_mut(2,\"test\"))
print(test.example_function({}))
print(test.example_function_mut(true))
";
lua_ctx.load(code).set_name("test?")?.eval()?;
Ok(())
})?;
Ok(())
}
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
fn main() -> Result<()> {
//we collect the documentation of our API in a json file so `tealr_doc_gen` can generate
//the online documentation
let file_contents = TypeWalker::new()
//tells it that we want to include the `Example` type
//add more calls to process_type to generate more types in the same file
.process_type::<Example>()
//generate the file
.to_json()
.expect("serde_json failed to serialize our data");
//normally you would now save the file somewhere.
println!("{}\n ", file_contents);
//how you pass this type to lua hasn't changed:
let lua = Lua::new();
let globals = lua.globals();
globals.set("test", Example {})?;
let code = "
print(test:example_method(1))
print(test:example_method_mut(2,\"test\"))
print(test.example_function({}))
print(test.example_function_mut(true))
";
lua.load(code).set_name("test?").eval()?;
Ok(())
}
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
fn main() -> Result<()> {
let file_contents = TypeWalker::new()
//tells it that you want to include the Example type
//chain extra calls to include more types
.process_type::<Example>()
//generate the file
.to_json()
.expect("serde_json failed to serialize our data");
//normally you would now save the file somewhere.
//however for this example we just print it.
println!("{}\n ", file_contents);
//how you pass this type to lua hasn't changed:
let lua = Lua::new();
let globals = lua.globals();
globals.set("test", Example(1))?;
let code = "
print(test:example_method(function()return 1 end))
print(test:example_method_mut(2,\"test\"))
print(test.example_function({}))
print(test.example_function_mut(true))
print(\"Example field\", test.example_field)
test.example_field = 2
print(\"After modifying\",test.example_field)
";
lua.load(code).set_name("test?").eval()?;
Ok(())
}
sourcepub fn add_page(self, name: String, content: String) -> Self
pub fn add_page(self, name: String, content: String) -> Self
Adds a new page that should be included in the documentation
sourcepub fn add_page_from(
&mut self,
name: String,
location: impl AsRef<Path>
) -> Result<&mut Self, Error>
pub fn add_page_from( &mut self, name: String, location: impl AsRef<Path> ) -> Result<&mut Self, Error>
reads a file and adds it as an extra page
sourcepub fn iter(&self) -> Iter<'_, TypeGenerator>
pub fn iter(&self) -> Iter<'_, TypeGenerator>
gives an iterator back over every type
sourcepub fn process_type_inline<A: ToTypename + TypeBody>(self) -> Self
pub fn process_type_inline<A: ToTypename + TypeBody>(self) -> Self
Process a type such that the body will be added directly into the module instead of becoming a child record.
When embedding teal/lua there is probably not really a reason to do so. However, it IS needed for the struct that gets exposed directly to teal when using mlua to make a lua/teal library.
sourcepub fn process_type<A: ToTypename + TypeBody>(self) -> Self
pub fn process_type<A: ToTypename + TypeBody>(self) -> Self
prepares a type to have a .d.tl
file generated, and adds it to the list of types to generate.
Examples found in repository?
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
fn main() -> Result<()> {
let file_contents = TypeWalker::new()
//tells it that you want to include the Example type
//chain extra calls to include more types
.process_type::<Example>()
//generate the file
.to_json()
.expect("serde_json failed to serialize our data");
//normally you would now save the file somewhere.
println!("{}\n ", file_contents);
//lua is still using position parameters as normal.
let lua = Lua::new();
let globals = lua.globals();
globals.set("test", Example {})?;
let code = "test:example_method(\"field_1 is a string\", 3)";
lua.load(code).set_name("test?").eval()?;
Ok(())
}
More examples
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
fn main() -> Result<()> {
let file_contents = TypeWalker::new()
//tells it that you want to include the Example type
//chain extra calls to include more types
.process_type::<Example>()
//generate the file
.to_json()
.expect("serde_json failed to serialize our data");
//normally you would now save the file somewhere.
println!("{}\n ", file_contents);
//lua is still using position parameters as normal.
tealr::rlu::rlua::Lua::new().context(|ctx| {
let globals = ctx.globals();
globals.set("test", Example {})?;
let code = "test:example_method(\"field_1 is a string\", 3)";
ctx.load(code).set_name("test?")?.eval()?;
Ok(())
})?;
Ok(())
}
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
fn main() -> Result<()> {
let file_contents = TypeWalker::new()
//tells it that you want to include the Example type
//chain extra calls to include more types
.process_type::<Example>()
//generate the file
.to_json()
.expect("serde_json failed to serialize our data");
//normally you would now save the file somewhere.
//however for this example we just print it.
println!("{}\n ", file_contents);
//how you pass this type to lua hasn't changed:
let lua = Lua::new();
let globals = lua.globals();
globals.set("test", Example(1))?;
let code = "
print(test.help())
print(\"----\")
print(test.help(\"example_method\"))
print(test:example_method(1))
print(test:example_method_mut(2,\"test\"))
print(test.example_function({}))
print(test.example_function_mut(true))
";
lua.load(code).set_name("test?").eval()?;
Ok(())
}
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
fn main() -> Result<()> {
let file_contents = TypeWalker::new()
//tells it that you want to include the Example type
//chain extra calls to include more types
.process_type::<Example>()
//generate the file
.to_json()
.expect("serde_json failed to serialize our data");
//normally you would now save the file somewhere.
//however for this example we just print it.
println!("{}\n ", file_contents);
//how you pass this type to lua hasn't changed:
let lua = Lua::new();
lua.context(|lua_ctx| {
let globals = lua_ctx.globals();
globals.set("test", Example {})?;
let code = "
print(test:example_method(1))
print(test:example_method_mut(2,\"test\"))
print(test.example_function({}))
print(test.example_function_mut(true))
";
lua_ctx.load(code).set_name("test?")?.eval()?;
Ok(())
})?;
Ok(())
}
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
fn main() -> Result<()> {
//we collect the documentation of our API in a json file so `tealr_doc_gen` can generate
//the online documentation
let file_contents = TypeWalker::new()
//tells it that we want to include the `Example` type
//add more calls to process_type to generate more types in the same file
.process_type::<Example>()
//generate the file
.to_json()
.expect("serde_json failed to serialize our data");
//normally you would now save the file somewhere.
println!("{}\n ", file_contents);
//how you pass this type to lua hasn't changed:
let lua = Lua::new();
let globals = lua.globals();
globals.set("test", Example {})?;
let code = "
print(test:example_method(1))
print(test:example_method_mut(2,\"test\"))
print(test.example_function({}))
print(test.example_function_mut(true))
";
lua.load(code).set_name("test?").eval()?;
Ok(())
}
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
fn main() -> Result<()> {
let file_contents = TypeWalker::new()
//tells it that you want to include the Example type
//chain extra calls to include more types
.process_type::<Example>()
//generate the file
.to_json()
.expect("serde_json failed to serialize our data");
//normally you would now save the file somewhere.
//however for this example we just print it.
println!("{}\n ", file_contents);
//how you pass this type to lua hasn't changed:
let lua = Lua::new();
let globals = lua.globals();
globals.set("test", Example(1))?;
let code = "
print(test:example_method(function()return 1 end))
print(test:example_method_mut(2,\"test\"))
print(test.example_function({}))
print(test.example_function_mut(true))
print(\"Example field\", test.example_field)
test.example_field = 2
print(\"After modifying\",test.example_field)
";
lua.load(code).set_name("test?").eval()?;
Ok(())
}
sourcepub fn generate(
self,
outer_name: &str,
is_global: bool
) -> Result<String, FromUtf8Error>
👎Deprecated since 0.9.0: Generation of .d.tl files has been moved to tealr_doc_gen. Use TypeWaller::to_json() instead to get json parsable by tealr_doc_gen.
pub fn generate( self, outer_name: &str, is_global: bool ) -> Result<String, FromUtf8Error>
generates the .d.tl
file. It outputs the string, its up to you to store it.
#[derive(UserData,ToTypename)]
struct Example {}
impl TealData for Example {}
let generated_string = TypeWalker::new().process_type::<Example>().generate("Examples",true);
assert_eq!(generated_string,Ok(String::from("global record Examples
\trecord Example
\t\tuserdata
\tend
end
return Examples"
)));
sourcepub fn generate_global(self, outer_name: &str) -> Result<String, FromUtf8Error>
👎Deprecated since 0.9.0: Generation of .d.tl files has been moved to tealr_doc_gen. Use TypeWaller::to_json() instead to get json parsable by tealr_doc_gen.
pub fn generate_global(self, outer_name: &str) -> Result<String, FromUtf8Error>
Same as calling Typewalker::generate(outer_name,true).
#[derive(UserData,ToTypename)]
struct Example {}
impl TealData for Example {}
let generated_string = TypeWalker::new().process_type::<Example>().generate_global("Examples");
assert_eq!(generated_string,Ok(String::from("global record Examples
\trecord Example
\t\tuserdata
\tend
end
return Examples"
)));
sourcepub fn generate_local(self, outer_name: &str) -> Result<String, FromUtf8Error>
👎Deprecated since 0.9.0: Generation of .d.tl files has been moved to tealr_doc_gen. Use TypeWaller::to_json() instead to get json parsable by tealr_doc_gen.
pub fn generate_local(self, outer_name: &str) -> Result<String, FromUtf8Error>
Same as calling Typewalker::generate(outer_name,false).
#[derive(UserData,ToTypename)]
struct Example {}
impl TealData for Example {}
let generated_string = TypeWalker::new().process_type::<Example>().generate_local("Examples");
assert_eq!(generated_string,Ok(String::from("local record Examples
\trecord Example
\t\tuserdata
\tend
end
return Examples"
)));
sourcepub fn to_json(&self) -> Result<String>
pub fn to_json(&self) -> Result<String>
Generates the json needed by tealr_doc_gen to generate the documentation.
It is up to you to store it properly
Examples found in repository?
29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
fn main() -> Result<()> {
let file_contents = TypeWalker::new()
//tells it that you want to include the Example type
//chain extra calls to include more types
.process_type::<Example>()
//generate the file
.to_json()
.expect("serde_json failed to serialize our data");
//normally you would now save the file somewhere.
println!("{}\n ", file_contents);
//lua is still using position parameters as normal.
let lua = Lua::new();
let globals = lua.globals();
globals.set("test", Example {})?;
let code = "test:example_method(\"field_1 is a string\", 3)";
lua.load(code).set_name("test?").eval()?;
Ok(())
}
More examples
26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47
fn main() -> Result<()> {
let file_contents = TypeWalker::new()
//tells it that you want to include the Example type
//chain extra calls to include more types
.process_type::<Example>()
//generate the file
.to_json()
.expect("serde_json failed to serialize our data");
//normally you would now save the file somewhere.
println!("{}\n ", file_contents);
//lua is still using position parameters as normal.
tealr::rlu::rlua::Lua::new().context(|ctx| {
let globals = ctx.globals();
globals.set("test", Example {})?;
let code = "test:example_method(\"field_1 is a string\", 3)";
ctx.load(code).set_name("test?")?.eval()?;
Ok(())
})?;
Ok(())
}
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
fn main() -> Result<()> {
let file_contents = TypeWalker::new()
//tells it that you want to include the Example type
//chain extra calls to include more types
.process_type::<Example>()
//generate the file
.to_json()
.expect("serde_json failed to serialize our data");
//normally you would now save the file somewhere.
//however for this example we just print it.
println!("{}\n ", file_contents);
//how you pass this type to lua hasn't changed:
let lua = Lua::new();
let globals = lua.globals();
globals.set("test", Example(1))?;
let code = "
print(test.help())
print(\"----\")
print(test.help(\"example_method\"))
print(test:example_method(1))
print(test:example_method_mut(2,\"test\"))
print(test.example_function({}))
print(test.example_function_mut(true))
";
lua.load(code).set_name("test?").eval()?;
Ok(())
}
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
fn main() -> Result<()> {
let file_contents = TypeWalker::new()
//tells it that you want to include the Example type
//chain extra calls to include more types
.process_type::<Example>()
//generate the file
.to_json()
.expect("serde_json failed to serialize our data");
//normally you would now save the file somewhere.
//however for this example we just print it.
println!("{}\n ", file_contents);
//how you pass this type to lua hasn't changed:
let lua = Lua::new();
lua.context(|lua_ctx| {
let globals = lua_ctx.globals();
globals.set("test", Example {})?;
let code = "
print(test:example_method(1))
print(test:example_method_mut(2,\"test\"))
print(test.example_function({}))
print(test.example_function_mut(true))
";
lua_ctx.load(code).set_name("test?")?.eval()?;
Ok(())
})?;
Ok(())
}
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
fn main() -> Result<()> {
//we collect the documentation of our API in a json file so `tealr_doc_gen` can generate
//the online documentation
let file_contents = TypeWalker::new()
//tells it that we want to include the `Example` type
//add more calls to process_type to generate more types in the same file
.process_type::<Example>()
//generate the file
.to_json()
.expect("serde_json failed to serialize our data");
//normally you would now save the file somewhere.
println!("{}\n ", file_contents);
//how you pass this type to lua hasn't changed:
let lua = Lua::new();
let globals = lua.globals();
globals.set("test", Example {})?;
let code = "
print(test:example_method(1))
print(test:example_method_mut(2,\"test\"))
print(test.example_function({}))
print(test.example_function_mut(true))
";
lua.load(code).set_name("test?").eval()?;
Ok(())
}
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
fn main() -> Result<()> {
let file_contents = TypeWalker::new()
//tells it that you want to include the Example type
//chain extra calls to include more types
.process_type::<Example>()
//generate the file
.to_json()
.expect("serde_json failed to serialize our data");
//normally you would now save the file somewhere.
//however for this example we just print it.
println!("{}\n ", file_contents);
//how you pass this type to lua hasn't changed:
let lua = Lua::new();
let globals = lua.globals();
globals.set("test", Example(1))?;
let code = "
print(test:example_method(function()return 1 end))
print(test:example_method_mut(2,\"test\"))
print(test.example_function({}))
print(test.example_function_mut(true))
print(\"Example field\", test.example_field)
test.example_field = 2
print(\"After modifying\",test.example_field)
";
lua.load(code).set_name("test?").eval()?;
Ok(())
}
sourcepub fn to_json_pretty(&self) -> Result<String>
pub fn to_json_pretty(&self) -> Result<String>
Generates the json needed by tealr_doc_gen to generate the documentation in a pretty-printed way.
It is up to you to store it properly.
sourcepub fn check_correct_version(&self) -> bool
pub fn check_correct_version(&self) -> bool
Checks if the version of tealr to create this TypeWalker is the same version as the current tealr version
sourcepub fn get_tealr_version_used(&self) -> &str
pub fn get_tealr_version_used(&self) -> &str
Gets the version of tealr that was used to create this TypeWalker
source§impl TypeWalker
impl TypeWalker
sourcepub fn document_global_instance<T: ExportInstances>(self) -> Result<Self>
pub fn document_global_instance<T: ExportInstances>(self) -> Result<Self>
collect every instance that is getting shared with lua
Examples found in repository?
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
fn main() -> Result<()> {
let file_contents = TypeWalker::new()
//tells it that we want to generate Example
.process_type::<Example>()
//generate the documentation for our proxy, allowing us to
.process_type::<UserDataProxy<Example>>()
// enable documenting the globals
.document_global_instance::<Export>()?
//generate the file
.to_json()
.expect("serde_json failed to serialize our data");
//normally you would now save the file somewhere.
println!("{}\n ", file_contents);
//how you pass this type to lua hasn't changed:
let lua = Lua::new();
tealr::mlu::set_global_env(Export {}, &lua).unwrap();
let globals = lua.globals();
globals.set("test", Example { float: 42.0 })?;
let code = "
print(\" Calling from `test` :\")
print(test:example_method(1))
print(test:example_method_mut(2,\"test\"))
print(test.example_function({}))
print(test.example_function_mut(true))
print(test.example_field)
print(test.example_static_field)
print(\" Calling from global `Example` :\")
print(Example.example_static_field)
print(Example.example_function({}))
";
lua.load(code).set_name("test?").eval()?;
Ok(())
}
Trait Implementations§
source§impl Clone for TypeWalker
impl Clone for TypeWalker
source§fn clone(&self) -> TypeWalker
fn clone(&self) -> TypeWalker
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read more