pub trait CodeGen {
Show 24 methods
// Required methods
fn output_language(&self) -> OutputLanguage;
fn to_method_name_case(&self, name: &str) -> String;
fn to_field_name_case(&self, name: &str) -> String;
fn to_type_name_case(&self, s: &str) -> String;
fn source_formatter(
&self,
formatter: Vec<String>,
) -> Result<Box<dyn SourceFormatter>>;
// Provided methods
fn init(
&mut self,
model: Option<&Model>,
lc: &LanguageConfig,
output_dir: Option<&Path>,
renderer: &mut Renderer<'_>,
) -> Result<(), Error> { ... }
fn generate_file(
&mut self,
w: &mut Writer,
model: &Model,
file_config: &OutputFile,
params: &BTreeMap<String, Value>,
) -> Result<Bytes> { ... }
fn init_file(
&mut self,
w: &mut Writer,
model: &Model,
file_config: &OutputFile,
params: &BTreeMap<String, Value>,
) -> Result<()> { ... }
fn write_source_file_header(
&mut self,
w: &mut Writer,
model: &Model,
params: &BTreeMap<String, Value>,
) -> Result<()> { ... }
fn declare_types(
&mut self,
w: &mut Writer,
model: &Model,
params: &BTreeMap<String, Value>,
) -> Result<()> { ... }
fn write_services(
&mut self,
w: &mut Writer,
model: &Model,
params: &BTreeMap<String, Value>,
) -> Result<()> { ... }
fn finalize(&mut self, w: &mut Writer) -> Result<Bytes> { ... }
fn write_documentation(
&mut self,
w: &mut Writer,
_id: &Identifier,
text: &str,
) { ... }
fn write_comment(&mut self, w: &mut Writer, kind: CommentKind, line: &str) { ... }
fn write_ident(&self, w: &mut Writer, id: &Identifier) { ... }
fn write_ident_with_suffix(
&mut self,
w: &mut Writer,
id: &Identifier,
suffix: &str,
) -> Result<()> { ... }
fn has_rename_trait(&self, traits: &AppliedTraits) -> Option<String> { ... }
fn get_file_extension(&self) -> &'static str { ... }
fn to_method_name(
&self,
method_id: &Identifier,
method_traits: &AppliedTraits,
) -> String { ... }
fn to_field_name(
&self,
member_id: &Identifier,
member_traits: &AppliedTraits,
) -> Result<String, Error> { ... }
fn get_field_name_and_ser_name(
&self,
field: &NumberedMember,
) -> Result<(String, String)> { ... }
fn op_dispatch_name(&self, id: &Identifier) -> String { ... }
fn full_dispatch_name(
&self,
service_id: &Identifier,
method_id: &Identifier,
) -> String { ... }
fn format(&mut self, files: Vec<PathBuf>, lc: &LanguageConfig) -> Result<()> { ... }
}Expand description
A Codegen is used to generate source for a Smithy Model The generator will invoke these functions (in order)
- init()
- write_source_file_header()
- declare_types()
- write_services()
- finalize()
Required Methods§
Sourcefn output_language(&self) -> OutputLanguage
fn output_language(&self) -> OutputLanguage
Returns current output language
Sourcefn to_method_name_case(&self, name: &str) -> String
fn to_method_name_case(&self, name: &str) -> String
Convert method name to its target-language-idiomatic case style
Sourcefn to_field_name_case(&self, name: &str) -> String
fn to_field_name_case(&self, name: &str) -> String
Convert field name to its target-language-idiomatic case style
Sourcefn to_type_name_case(&self, s: &str) -> String
fn to_type_name_case(&self, s: &str) -> String
Convert type name to its target-language-idiomatic case style
fn source_formatter( &self, formatter: Vec<String>, ) -> Result<Box<dyn SourceFormatter>>
Provided Methods§
Sourcefn init(
&mut self,
model: Option<&Model>,
lc: &LanguageConfig,
output_dir: Option<&Path>,
renderer: &mut Renderer<'_>,
) -> Result<(), Error>
fn init( &mut self, model: Option<&Model>, lc: &LanguageConfig, output_dir: Option<&Path>, renderer: &mut Renderer<'_>, ) -> Result<(), Error>
Initialize code generator and renderer for language output.j This hook is called before any code is generated and can be used to initialize code generator and/or perform additional processing before output files are created.
Sourcefn generate_file(
&mut self,
w: &mut Writer,
model: &Model,
file_config: &OutputFile,
params: &BTreeMap<String, Value>,
) -> Result<Bytes>
fn generate_file( &mut self, w: &mut Writer, model: &Model, file_config: &OutputFile, params: &BTreeMap<String, Value>, ) -> Result<Bytes>
This entrypoint drives output-file-specific code generation.
This default implementation invokes init_file, write_source_file_header, declare_types, write_services, and finalize.
The return value is Bytes containing the data that should be written to the output file.
Sourcefn init_file(
&mut self,
w: &mut Writer,
model: &Model,
file_config: &OutputFile,
params: &BTreeMap<String, Value>,
) -> Result<()>
fn init_file( &mut self, w: &mut Writer, model: &Model, file_config: &OutputFile, params: &BTreeMap<String, Value>, ) -> Result<()>
Perform any initialization required prior to code generation for a file
model may be used to check model metadata
id is a tag from codegen.toml that indicates which source file is to be written
namespace is the namespace in the model to generate
Sourcefn write_source_file_header(
&mut self,
w: &mut Writer,
model: &Model,
params: &BTreeMap<String, Value>,
) -> Result<()>
fn write_source_file_header( &mut self, w: &mut Writer, model: &Model, params: &BTreeMap<String, Value>, ) -> Result<()>
generate the source file header
Sourcefn declare_types(
&mut self,
w: &mut Writer,
model: &Model,
params: &BTreeMap<String, Value>,
) -> Result<()>
fn declare_types( &mut self, w: &mut Writer, model: &Model, params: &BTreeMap<String, Value>, ) -> Result<()>
Write declarations for simple types, maps, and structures
Sourcefn write_services(
&mut self,
w: &mut Writer,
model: &Model,
params: &BTreeMap<String, Value>,
) -> Result<()>
fn write_services( &mut self, w: &mut Writer, model: &Model, params: &BTreeMap<String, Value>, ) -> Result<()>
Write service declarations and implementation stubs
Sourcefn finalize(&mut self, w: &mut Writer) -> Result<Bytes>
fn finalize(&mut self, w: &mut Writer) -> Result<Bytes>
Complete generation and return the output bytes
Sourcefn write_documentation(&mut self, w: &mut Writer, _id: &Identifier, text: &str)
fn write_documentation(&mut self, w: &mut Writer, _id: &Identifier, text: &str)
Write documentation for item
Sourcefn write_comment(&mut self, w: &mut Writer, kind: CommentKind, line: &str)
fn write_comment(&mut self, w: &mut Writer, kind: CommentKind, line: &str)
Writes single-line comment beginning with ’// ’ Can be overridden if more specific kinds are needed
fn write_ident(&self, w: &mut Writer, id: &Identifier)
Sourcefn write_ident_with_suffix(
&mut self,
w: &mut Writer,
id: &Identifier,
suffix: &str,
) -> Result<()>
fn write_ident_with_suffix( &mut self, w: &mut Writer, id: &Identifier, suffix: &str, ) -> Result<()>
append suffix to type name, for example “Game”, “Context” -> “GameContext”
fn has_rename_trait(&self, traits: &AppliedTraits) -> Option<String>
Sourcefn get_file_extension(&self) -> &'static str
fn get_file_extension(&self) -> &'static str
returns file extension of source files for this language
Sourcefn to_method_name(
&self,
method_id: &Identifier,
method_traits: &AppliedTraits,
) -> String
fn to_method_name( &self, method_id: &Identifier, method_traits: &AppliedTraits, ) -> String
Convert method name to its target-language-idiomatic case style implementors should override to_method_name_case
Sourcefn to_field_name(
&self,
member_id: &Identifier,
member_traits: &AppliedTraits,
) -> Result<String, Error>
fn to_field_name( &self, member_id: &Identifier, member_traits: &AppliedTraits, ) -> Result<String, Error>
Convert field name to its target-language-idiomatic case style implementors should override to_field_name_case
fn get_field_name_and_ser_name( &self, field: &NumberedMember, ) -> Result<(String, String)>
Sourcefn op_dispatch_name(&self, id: &Identifier) -> String
fn op_dispatch_name(&self, id: &Identifier) -> String
The operation name used in dispatch, from method The default implementation is provided and should not be overridden
Sourcefn full_dispatch_name(
&self,
service_id: &Identifier,
method_id: &Identifier,
) -> String
fn full_dispatch_name( &self, service_id: &Identifier, method_id: &Identifier, ) -> String
The full operation name with service prefix The default implementation is provided and should not be overridden