blob: cac4689e0ee3efcfbc9097b59c7a30dcd14ed968 [file] [log] [blame]
// Copyright 2018 The Fuchsia Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
package codegen
import (
"embed"
"text/template"
"go.fuchsia.dev/fuchsia/tools/fidl/lib/fidlgen"
)
//go:embed *.tmpl
var templates embed.FS
type Generator struct {
fdomain bool
common bool
*fidlgen.Generator
}
func NewGenerator(rustfmtPath, rustfmtConfigPath string, fdomain bool, common bool, use_common string) *Generator {
var args []string
if rustfmtConfigPath != "" {
args = append(args, "--config-path", rustfmtConfigPath)
}
formatter := fidlgen.NewFormatter(rustfmtPath, args...)
return &Generator{
fdomain: fdomain,
common: common,
Generator: fidlgen.NewGenerator("RustTemplates", templates, formatter, template.FuncMap{
"ResourceDialect": func() string {
if fdomain {
return "fdomain_client::fidl::FDomainResourceDialect"
} else {
return "fidl::encoding::DefaultFuchsiaResourceDialect"
}
},
"FDomain": func() bool { return fdomain },
"IsCommon": func() bool { return common },
"UseCommon": func() string { return use_common },
"UsingCommon": func() bool { return use_common != "" },
"EmitType": func(is_value bool) bool {
if is_value {
return common || use_common == ""
} else {
return !common
}
},
"MarkerNamespace": func() string {
if fdomain {
return "fdomain_client::fidl"
} else {
return "fidl::endpoints"
}
},
"ChannelType": func() string {
if fdomain {
return "fdomain_client::Channel"
} else {
return "::fidl::AsyncChannel"
}
},
})}
}
func (gen *Generator) GenerateFidl(ir fidlgen.Root, outputFilename string, includeDrivers bool) error {
tree := Compile(ir, includeDrivers, gen.fdomain, gen.common)
return gen.GenerateFile(outputFilename, "GenerateSourceFile", tree)
}