1// Copyright 2017 The Fuchsia Authors. All rights reserved.
2// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
5#include "generator.h"
6
7bool RustBindingGenerator::header(std::ofstream& os) {
8    if (!Generator::header(os)) {
9        return false;
10    }
11    os << "#[link(name = \"zircon\")]\n";
12    os << "extern {\n";
13    return os.good();
14}
15
16bool RustBindingGenerator::footer(std::ofstream& os) {
17    if (!Generator::footer(os)) {
18        return false;
19    }
20    os << "}\n";
21    return os.good();
22}
23
24bool RustBindingGenerator::syscall(std::ofstream& os, const Syscall& sc) {
25    os << "    pub fn zx_" << sc.name << "(";
26
27    // Writes all arguments.
28    sc.for_each_kernel_arg([&](const TypeSpec& arg) {
29        os << "\n        "
30           << arg.as_rust_declaration() << ",";
31    });
32
33    if (!os.good()) {
34        return false;
35    }
36
37    if (sc.num_kernel_args() > 0) {
38        // remove the comma.
39        os.seekp(-1, std::ios_base::end);
40    }
41    // Finish off list and write return type
42    os << "\n        )";
43    if (sc.return_type() != "void") {
44        os << " -> " << map_override(sc.return_type(), rust_primitives);
45    }
46    os << ";\n\n";
47
48    return os.good();
49}
50