-
Notifications
You must be signed in to change notification settings - Fork 13
/
csharp.rs
62 lines (48 loc) · 2.19 KB
/
csharp.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
use csharp::comment;
use genco::fmt;
use genco::prelude::*;
fn main() -> anyhow::Result<()> {
let console = &csharp::import("System", "Console");
let file = &csharp::import("System.IO", "File");
let stream = &csharp::import("System.IO", "Stream");
let soap_formatter = &csharp::import(
"System.Runtime.Serialization.Formatters.Soap",
"SoapFormatter",
);
let simple_object = "TestSimpleObject";
// Note: Comments have to be escaped as raw expressions, since they are
// filtered out from procedural macros.
let tokens = quote! {
public class Test {
public static void Main() {
$(comment(&["Creates a new TestSimpleObject object."]))
$simple_object obj = new $simple_object();
$console.WriteLine("Before serialization the object contains: ");
obj.Print();
$(comment(&["Opens a file and serializes the object into it in binary format."]))
$stream stream = $file.Open("data.xml", FileMode.Create);
$soap_formatter formatter = new $soap_formatter();
$(comment(&["BinaryFormatter formatter = new BinaryFormatter();"]))
formatter.Serialize(stream, obj);
stream.Close();
$(comment(&["Empties obj."]))
obj = null;
$(comment(&["Opens file \"data.xml\" and deserializes the object from it."]))
stream = $file.Open("data.xml", FileMode.Open);
formatter = new $soap_formatter();
$(comment(&["formatter = new BinaryFormatter();"]))
obj = ($simple_object)formatter.Deserialize(stream);
stream.Close();
$console.WriteLine("");
$console.WriteLine("After deserialization the object contains: ");
obj.Print();
}
}
};
let stdout = std::io::stdout();
let mut w = fmt::IoWriter::new(stdout.lock());
let fmt = fmt::Config::from_lang::<Csharp>().with_indentation(fmt::Indentation::Space(4));
let config = csharp::Config::default();
tokens.format_file(&mut w.as_formatter(&fmt), &config)?;
Ok(())
}