use crate::ir::IrFile;
use crate::parser::{self, ParserResult};
use crate::utils::misc::{read_rust_file, BlockIndex};
use convert_case::{Case, Casing};
use std::path::{Path, PathBuf};
#[derive(Debug, Clone, serde::Serialize)]
pub struct Opts {
pub rust_input_path: String,
pub dart_output_path: String,
pub dart_decl_output_path: Option<String>,
pub c_output_path: Vec<String>,
pub rust_crate_dir: String,
pub rust_output_path: String,
pub class_name: String,
pub dart_format_line_length: u32,
pub dart_enums_style: bool,
pub skip_add_mod_to_lib: bool,
pub llvm_path: Vec<String>,
pub llvm_compiler_opts: String,
pub manifest_path: String,
pub dart_root: Option<String>,
pub build_runner: bool,
pub block_index: BlockIndex,
pub skip_deps_check: bool,
pub wasm_enabled: bool,
pub inline_rust: bool,
pub bridge_in_method: bool,
pub extra_headers: String,
pub dart3: bool,
pub keep_going: bool,
}
impl Opts {
pub fn get_ir_file(&self) -> ParserResult<IrFile> {
let source_rust_content = read_rust_file(&PathBuf::from(&self.rust_input_path));
let file_ast = syn::parse_file(&source_rust_content)?;
parser::parse(&source_rust_content, file_ast, &self.manifest_path)
}
pub fn dart_api_class_name(&self) -> &str {
&self.class_name
}
pub fn dart_api_impl_class_name(&self) -> String {
format!("{}Impl", self.class_name)
}
pub fn dart_wire_class_name(&self) -> String {
format!("{}Wire", self.class_name)
}
pub fn dart_platform_class_name(&self) -> String {
format!("{}Platform", self.class_name)
}
pub fn dart_wasm_module(&self) -> String {
format!("{}WasmModule", self.class_name)
}
pub(crate) fn dart_output_root(&self) -> Option<&str> {
Path::new(
self.dart_decl_output_path
.as_ref()
.unwrap_or(&self.dart_output_path),
)
.file_stem()?
.to_str()
}
pub fn dart_wasm_output_path(&self) -> PathBuf {
Path::new(&self.dart_output_path).with_extension("web.dart")
}
pub fn dart_io_output_path(&self) -> PathBuf {
Path::new(&self.dart_output_path).with_extension("io.dart")
}
pub fn dart_common_output_path(&self) -> PathBuf {
Path::new(&self.dart_output_path).with_extension("common.dart")
}
pub fn rust_wasm_output_path(&self) -> PathBuf {
Path::new(&self.rust_output_path).with_extension("web.rs")
}
pub fn rust_io_output_path(&self) -> PathBuf {
Path::new(&self.rust_output_path).with_extension("io.rs")
}
pub fn dart_root_or_default(&self) -> String {
self.dart_root
.clone()
.unwrap_or_else(|| env!("CARGO_MANIFEST_DIR").to_string())
}
pub fn dart_freezed_path(&self) -> PathBuf {
PathBuf::from(
self.dart_decl_output_path
.as_deref()
.unwrap_or(&self.dart_output_path),
)
.with_extension("freezed.dart")
}
pub fn get_rust_output_paths(&self) -> PathForGeneration {
PathForGeneration {
base_path: PathBuf::from(self.rust_output_path.clone()),
io_path: self.rust_io_output_path(),
wasm_path: self.rust_wasm_output_path(),
}
}
pub fn get_dart_output_paths(&self) -> PathForGeneration {
PathForGeneration {
base_path: PathBuf::from(self.dart_output_path.clone()),
io_path: self.dart_io_output_path(),
wasm_path: self.dart_wasm_output_path(),
}
}
pub fn get_dart_api_bridge_name(&self) -> String {
if self.bridge_in_method {
"bridge".to_owned()
} else {
Path::new(&self.rust_input_path)
.file_stem()
.unwrap()
.to_str()
.unwrap()
.to_owned()
.to_case(Case::Camel)
}
}
}
pub struct PathForGeneration {
pub base_path: PathBuf,
pub io_path: PathBuf,
pub wasm_path: PathBuf,
}