use askama::Template;
use super::plan::{CSharpModule, CSharpRecord};
#[derive(Template)]
#[template(path = "render_csharp/preamble.txt", escape = "none")]
pub struct PreambleTemplate<'a> {
pub module: &'a CSharpModule,
}
#[derive(Template)]
#[template(path = "render_csharp/functions.txt", escape = "none")]
pub struct FunctionsTemplate<'a> {
pub module: &'a CSharpModule,
}
#[derive(Template)]
#[template(path = "render_csharp/native.txt", escape = "none")]
pub struct NativeTemplate<'a> {
pub module: &'a CSharpModule,
}
#[derive(Template)]
#[template(path = "render_csharp/record.txt", escape = "none")]
pub struct RecordTemplate<'a> {
pub record: &'a CSharpRecord,
pub namespace: &'a str,
}
#[cfg(test)]
mod tests {
use super::*;
use crate::render::csharp::plan::{CSharpRecord, CSharpRecordField, CSharpType};
fn record_field(
name: &str,
csharp_type: CSharpType,
decode: &str,
size: &str,
encode: &str,
) -> CSharpRecordField {
CSharpRecordField {
name: name.to_string(),
csharp_type,
wire_decode_expr: decode.to_string(),
wire_size_expr: size.to_string(),
wire_encode_expr: encode.to_string(),
}
}
#[test]
fn snapshot_blittable_record_point() {
let record = CSharpRecord {
class_name: "Point".to_string(),
is_blittable: true,
fields: vec![
record_field(
"X",
CSharpType::Double,
"reader.ReadF64()",
"8",
"wire.WriteF64(this.X)",
),
record_field(
"Y",
CSharpType::Double,
"reader.ReadF64()",
"8",
"wire.WriteF64(this.Y)",
),
],
};
let template = RecordTemplate {
record: &record,
namespace: "Demo",
};
insta::assert_snapshot!(template.render().unwrap());
}
#[test]
fn snapshot_non_blittable_record_person_with_string() {
let record = CSharpRecord {
class_name: "Person".to_string(),
is_blittable: false,
fields: vec![
record_field(
"Name",
CSharpType::String,
"reader.ReadString()",
"(4 + Encoding.UTF8.GetByteCount(this.Name))",
"wire.WriteString(this.Name)",
),
record_field(
"Age",
CSharpType::UInt,
"reader.ReadU32()",
"4",
"wire.WriteU32(this.Age)",
),
],
};
let template = RecordTemplate {
record: &record,
namespace: "Demo",
};
insta::assert_snapshot!(template.render().unwrap());
}
#[test]
fn snapshot_nested_record_line() {
let record = CSharpRecord {
class_name: "Line".to_string(),
is_blittable: false,
fields: vec![
record_field(
"Start",
CSharpType::Record("Point".to_string()),
"Point.Decode(reader)",
"16",
"this.Start.WireEncodeTo(wire)",
),
record_field(
"End",
CSharpType::Record("Point".to_string()),
"Point.Decode(reader)",
"16",
"this.End.WireEncodeTo(wire)",
),
],
};
let template = RecordTemplate {
record: &record,
namespace: "Demo",
};
insta::assert_snapshot!(template.render().unwrap());
}
#[test]
fn snapshot_empty_record() {
let record = CSharpRecord {
class_name: "Unit".to_string(),
is_blittable: true,
fields: vec![],
};
let template = RecordTemplate {
record: &record,
namespace: "Demo",
};
insta::assert_snapshot!(template.render().unwrap());
}
}