use std::path::PathBuf;
use std::rc::Rc;
use crate::model::*;
#[derive(Clone, Debug)]
pub(crate) enum Statement<D>
where
D: DocReference,
{
Constants(Handle<ConstantSet<D>>),
StructDeclaration(StructDeclarationHandle),
StructDefinition(StructType<D>),
EnumDefinition(Handle<Enum<D>>),
ErrorType(ErrorType<D>),
ClassDeclaration(ClassDeclarationHandle),
ClassDefinition(Handle<Class<D>>),
StaticClassDefinition(Handle<StaticClass<D>>),
InterfaceDefinition(InterfaceType<D>),
IteratorDeclaration(Handle<AbstractIterator<D>>),
CollectionDeclaration(Handle<Collection<D>>),
FunctionDefinition(Handle<Function<D>>),
}
impl Statement<Unvalidated> {
pub(crate) fn unique_name(&self) -> Option<&Name> {
match self {
Statement::Constants(x) => Some(&x.name),
Statement::StructDeclaration(x) => Some(&x.name),
Statement::StructDefinition(_) => {
None
}
Statement::EnumDefinition(x) => Some(&x.name),
Statement::ErrorType(x) => Some(&x.exception_name),
Statement::ClassDeclaration(x) => Some(&x.name),
Statement::ClassDefinition(_) => {
None
}
Statement::StaticClassDefinition(x) => Some(&x.name),
Statement::InterfaceDefinition(x) => Some(&x.untyped().name),
Statement::IteratorDeclaration(_) => {
None
}
Statement::CollectionDeclaration(_) => {
None
}
Statement::FunctionDefinition(x) => Some(&x.name),
}
}
}
pub struct DeveloperInfo {
pub name: String,
pub email: String,
pub organization: String,
pub organization_url: String,
}
pub struct LibraryInfo {
pub description: String,
pub project_url: String,
pub repository: String,
pub license_name: String,
pub license_description: Vec<String>,
pub license_path: PathBuf,
pub developers: Vec<DeveloperInfo>,
pub logo_png: &'static [u8],
}
#[derive(Debug)]
pub struct IteratorSettings {
pub next_function_suffix: Name,
}
impl IteratorSettings {
pub fn new(next_function_suffix: Name) -> IteratorSettings {
Self {
next_function_suffix,
}
}
}
impl Default for IteratorSettings {
fn default() -> Self {
Self {
next_function_suffix: Name::create("next").unwrap(),
}
}
}
#[derive(Debug)]
pub struct InterfaceSettings {
pub context_variable_name: Name,
pub destroy_func_name: Name,
}
impl InterfaceSettings {
pub fn new(context_variable_name: Name, destroy_func_name: Name) -> Self {
Self {
context_variable_name,
destroy_func_name,
}
}
}
impl Default for InterfaceSettings {
fn default() -> Self {
Self {
context_variable_name: Name::create("ctx").unwrap(),
destroy_func_name: Name::create("on_destroy").unwrap(),
}
}
}
#[derive(Debug)]
pub struct ClassSettings {
pub method_instance_argument_name: Name,
pub class_destructor_suffix: Name,
pub class_constructor_suffix: Name,
}
impl ClassSettings {
pub fn new(
method_instance_argument_name: Name,
class_destructor_suffix: Name,
class_constructor_suffix: Name,
) -> Self {
Self {
method_instance_argument_name,
class_destructor_suffix,
class_constructor_suffix,
}
}
}
impl Default for ClassSettings {
fn default() -> ClassSettings {
Self {
method_instance_argument_name: Name::create("instance").unwrap(),
class_destructor_suffix: Name::create("destroy").unwrap(),
class_constructor_suffix: Name::create("create").unwrap(),
}
}
}
#[derive(Debug)]
pub struct FutureSettings {
pub success_callback_method_name: Name,
pub success_single_parameter_name: Name,
pub failure_callback_method_name: Name,
pub failure_single_parameter_name: Name,
pub async_method_callback_parameter_name: Name,
}
impl FutureSettings {
pub fn new(
success_callback_method_name: Name,
success_single_parameter_name: Name,
failure_callback_method_name: Name,
failure_single_parameter_name: Name,
async_method_callback_parameter_name: Name,
) -> Self {
Self {
success_callback_method_name,
success_single_parameter_name,
failure_callback_method_name,
failure_single_parameter_name,
async_method_callback_parameter_name,
}
}
}
impl Default for FutureSettings {
fn default() -> Self {
Self {
success_callback_method_name: Name::create("on_complete").unwrap(),
success_single_parameter_name: Name::create("result").unwrap(),
failure_callback_method_name: Name::create("on_failure").unwrap(),
failure_single_parameter_name: Name::create("error").unwrap(),
async_method_callback_parameter_name: Name::create("callback").unwrap(),
}
}
}
#[derive(Debug)]
pub struct CollectionSettings {
pub create_function_suffix: Name,
pub add_function_suffix: Name,
pub destroy_function_suffix: Name,
}
impl CollectionSettings {
pub fn new(
create_function_suffix: Name,
add_function_suffix: Name,
destroy_function_suffix: Name,
) -> Self {
Self {
create_function_suffix,
add_function_suffix,
destroy_function_suffix,
}
}
}
impl Default for CollectionSettings {
fn default() -> CollectionSettings {
Self {
create_function_suffix: Name::create("create").unwrap(),
add_function_suffix: Name::create("add").unwrap(),
destroy_function_suffix: Name::create("destroy").unwrap(),
}
}
}
#[derive(Debug)]
pub struct LibrarySettings {
pub name: Name,
pub c_ffi_prefix: Name,
pub class: ClassSettings,
pub iterator: IteratorSettings,
pub collection: CollectionSettings,
pub future: FutureSettings,
pub interface: InterfaceSettings,
}
impl LibrarySettings {
pub fn create<S: IntoName, R: IntoName>(
name: S,
c_ffi_prefix: R,
class: ClassSettings,
iterator: IteratorSettings,
collection: CollectionSettings,
future: FutureSettings,
interface: InterfaceSettings,
) -> BindResult<Rc<Self>> {
Ok(Rc::new(Self {
name: name.into_name()?,
c_ffi_prefix: c_ffi_prefix.into_name()?,
class,
iterator,
collection,
future,
interface,
}))
}
}
pub struct Library {
pub(crate) version: Version,
pub(crate) info: Rc<LibraryInfo>,
pub(crate) settings: Rc<LibrarySettings>,
statements: Vec<Statement<Validated>>,
}
impl Library {
pub(crate) fn new(
version: Version,
info: Rc<LibraryInfo>,
settings: Rc<LibrarySettings>,
statements: Vec<Statement<Validated>>,
) -> Self {
Self {
version,
info,
settings,
statements,
}
}
pub(crate) fn statements(&self) -> impl Iterator<Item = &Statement<Validated>> {
self.statements.iter()
}
pub(crate) fn functions(&self) -> impl Iterator<Item = &Handle<Function<Validated>>> {
self.statements().filter_map(|statement| match statement {
Statement::FunctionDefinition(handle) => Some(handle),
_ => None,
})
}
pub(crate) fn structs(&self) -> impl Iterator<Item = &StructType<Validated>> {
self.statements
.iter()
.filter_map(|statement| match statement {
Statement::StructDefinition(x) => Some(x),
_ => None,
})
}
pub(crate) fn constants(&self) -> impl Iterator<Item = &Handle<ConstantSet<Validated>>> {
self.statements().filter_map(|statement| match statement {
Statement::Constants(handle) => Some(handle),
_ => None,
})
}
pub(crate) fn enums(&self) -> impl Iterator<Item = &Handle<Enum<Validated>>> {
self.statements().filter_map(|statement| match statement {
Statement::EnumDefinition(handle) => Some(handle),
_ => None,
})
}
pub(crate) fn classes(&self) -> impl Iterator<Item = &Handle<Class<Validated>>> {
self.statements().filter_map(|statement| match statement {
Statement::ClassDefinition(handle) => Some(handle),
_ => None,
})
}
pub(crate) fn error_types(&self) -> impl Iterator<Item = &ErrorType<Validated>> {
self.statements().filter_map(|statement| match statement {
Statement::ErrorType(err) => Some(err),
_ => None,
})
}
pub(crate) fn static_classes(&self) -> impl Iterator<Item = &Handle<StaticClass<Validated>>> {
self.statements().filter_map(|statement| match statement {
Statement::StaticClassDefinition(handle) => Some(handle),
_ => None,
})
}
pub(crate) fn untyped_interfaces(&self) -> impl Iterator<Item = &Handle<Interface<Validated>>> {
self.interfaces().map(|x| x.untyped())
}
pub(crate) fn interfaces(&self) -> impl Iterator<Item = &InterfaceType<Validated>> {
self.statements
.iter()
.filter_map(|statement| match statement {
Statement::InterfaceDefinition(t) => Some(t),
_ => None,
})
}
pub(crate) fn iterators(&self) -> impl Iterator<Item = &Handle<AbstractIterator<Validated>>> {
self.statements().filter_map(|statement| match statement {
Statement::IteratorDeclaration(handle) => Some(handle),
_ => None,
})
}
pub(crate) fn collections(&self) -> impl Iterator<Item = &Handle<Collection<Validated>>> {
self.statements().filter_map(|statement| match statement {
Statement::CollectionDeclaration(handle) => Some(handle),
_ => None,
})
}
}
impl From<UniversalStructDeclaration> for FunctionReturnStructDeclaration {
fn from(x: UniversalStructDeclaration) -> Self {
FunctionReturnStructDeclaration::new(x.inner)
}
}