use alloc::borrow::Cow;
use alloc::boxed::Box;
use alloc::string::String;
use crate::ra;
use crate::util::GlobalKind;
#[derive(Clone, Debug)]
pub struct Config {
pub(crate) flags: WriterFlags,
pub(crate) runtime_path: Cow<'static, str>,
pub(crate) global_struct: Option<String>,
pub(crate) resource_struct: Option<String>,
#[allow(dead_code, reason = "reminding ourselves of the future")]
pub(crate) edition: Edition,
}
impl Default for Config {
fn default() -> Self {
Self::new()
}
}
impl Config {
#[must_use]
pub const fn new() -> Self {
Self {
flags: WriterFlags::empty(),
runtime_path: Cow::Borrowed("::naga_rust_rt"),
global_struct: None,
resource_struct: None,
edition: Edition::Rust2024,
}
}
#[must_use]
pub fn explicit_types(mut self, value: bool) -> Self {
self.flags.set(WriterFlags::EXPLICIT_TYPES, value);
self
}
#[must_use]
pub fn raw_pointers(mut self, value: bool) -> Self {
self.flags.set(WriterFlags::RAW_POINTERS, value);
self
}
#[must_use]
pub fn public_items(mut self, value: bool) -> Self {
self.flags.set(WriterFlags::PUBLIC, value);
self
}
#[must_use]
pub fn allow_unimplemented(mut self, value: bool) -> Self {
self.flags.set(WriterFlags::ALLOW_UNIMPLEMENTED, value);
self
}
#[must_use]
pub fn runtime_path(mut self, value: impl Into<Cow<'static, str>>) -> Self {
let value = value.into();
assert!(
value.starts_with("::") || value.starts_with("crate::"),
"path should be an absolute path"
);
self.runtime_path = value;
self
}
#[must_use]
pub fn global_struct(mut self, name: impl Into<String>) -> Self {
self.global_struct = Some(name.into());
self
}
#[must_use]
pub fn resource_struct(mut self, name: impl Into<String>) -> Self {
self.resource_struct = Some(name.into());
self
}
}
impl Config {
pub(crate) fn functions_are_methods(&self) -> bool {
self.global_struct.is_some() || self.resource_struct.is_some()
}
pub(crate) fn impl_type(&self) -> Option<&str> {
match self.global_struct {
Some(ref name) => Some(name),
None => self.resource_struct.as_deref(),
}
}
pub(crate) fn global_field_access_expr(&self, variable: &naga::GlobalVariable) -> ra::Expr {
match (GlobalKind::of_variable(variable), &self.global_struct) {
(Some(GlobalKind::Resource), Some(_)) => {
ra::Expr::NamedField(Box::new(ra::Expr::Self_), "resources".into())
}
(Some(GlobalKind::Resource), None) | (Some(GlobalKind::Variable), _) => ra::Expr::Self_,
_ => unreachable!(),
}
}
}
bitflags::bitflags! {
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
pub(crate) struct WriterFlags: u32 {
const EXPLICIT_TYPES = 0x1;
const RAW_POINTERS = 0x2;
const PUBLIC = 0x4;
const ALLOW_UNIMPLEMENTED = 0x8;
}
}
#[derive(Clone, Copy, Debug)]
pub(crate) enum Edition {
Rust2024,
}