ikarusdef 0.1.4

A tool describing Ikarus' capabilities
Documentation
use crate::types::consts::{buffer_out_param, buffer_out_size_param};
use crate::types::consts::{INTERNAL_ERROR_IDENT, INVALID_PARAM_IDENT};
use crate::types::structs::*;
use indoc::indoc;

pub fn project_type() -> Type {
    const PROJECT_PARAM_NAME: &'static str = "project";
    const PROJECT_PARAM_TYPE: &'static str = "Project";

    let project_param = |desc| -> Parameter {
        Parameter {
            name: PROJECT_PARAM_NAME,
            r#type: PROJECT_PARAM_TYPE,
            description: desc,
            log_level: LogLevel::Info,
            validations: vec![ParameterValidation {
                r#type: ParameterValidationType::NotNull,
                flag_condition: None,
            }],
        }
    };

    Type {
        name: "project",
        description: indoc! {r#"
            A project represents a persistent workspace of Ikarus-related data.
            Each project can contain any number of entities.
        "#},
        functions: vec![
            Function {
                name: "open",
                description: "Opens a project at a specific path in the filesystem.",
                log_level: LogLevel::Info,
                db_access: DbAccess::NoAccess,
                versions: vec![
                    FunctionVersion {
                        description: None,
                        log_level: None,
                        db_access: None,
                        flags: vec![
                            Flag {
                                name: "MustExist",
                                description: indoc! {r#"
                                    Overrides the default behaviour, causing a non-existent project to cause an error instead of the creation of one.
                                "#},
                            },
                            Flag {
                                name: "CreateParents",
                                description: "If specified a path to a non-existent directory will have all its parent-directories created.",
                            },
                        ],
                        parameters: vec![
                            Parameter {
                                name: "path",
                                description: indoc! {r#"
                                    The path on the filesystem where you want to store the project.
                                    The path must point to a valid filename within an existing directory.
                                    If no project exist at the specified path, one is created.
                                "#},
                                r#type: "String",
                                log_level: LogLevel::Info,
                                validations: vec![
                                    ParameterValidation {
                                        r#type: ParameterValidationType::NotNull,
                                        flag_condition: None,
                                    },
                                    ParameterValidation {
                                        r#type: ParameterValidationType::ValidUtf8,
                                        flag_condition: None,
                                    },
                                    ParameterValidation {
                                        r#type: ParameterValidationType::PathExists,
                                        flag_condition: Some(FlagCondition {
                                            inverted: false,
                                            flag: "MustExist",
                                        }),
                                    },
                                    ParameterValidation {
                                        r#type: ParameterValidationType::PathParentMustExist,
                                        flag_condition: Some(FlagCondition {
                                            inverted: true,
                                            flag: "CreateParents",
                                        }),
                                    },
                                ],
                            }
                        ],
                        r#return: Some(ReturnType {
                            name: PROJECT_PARAM_NAME,
                            r#type: PROJECT_PARAM_TYPE,
                            description: indoc! {r#"
                                An opaque handle to the project or an undefined handle if an error occurred.
                                A valid pointer must be freed using either `close_project` or `delete_project`.
                            "#},
                        }),
                        errors: vec![
                            Error {
                                r#type: INTERNAL_ERROR_IDENT,
                                occurrences: vec![
                                    "When we fail to initialise the project.",
                                    "When we fail to open the project.",
                                    "When we fail to migrate the project.",
                                ],
                            },
                            Error {
                                r#type: INVALID_PARAM_IDENT,
                                occurrences: vec![
                                    "When you try to open a project with a future version."
                                ],
                            }
                        ],
                    }
                ],
            },
            Function {
                name: "close",
                description: indoc!{r#"
                    Closes a project previously opened with `open_project`.
                    On success, all data is saved and persisted and the pointer is freed and must not be used afterwards.
                
                    Any error occurring will leave the pointer intact.
                    Error's occurring in this function should be met with a retry mechanism.
                "#},
                log_level: LogLevel::Info,
                db_access: DbAccess::ReadWrite,
                versions: vec![
                    FunctionVersion {
                        description: None,
                        log_level: None,
                        db_access: None,
                        flags: vec![],
                        parameters: vec![
                            project_param("The project you want to close.")
                        ],
                        r#return: None,
                        errors: vec![
                            Error{
                                r#type: INTERNAL_ERROR_IDENT,
                                occurrences: vec![
                                    "When we fail to close the project."
                                ],
                            }
                        ],
                    }
                ],
            },
            Function {
                name: "delete",
                description: indoc!{r#"
                    Closes & deletes a project from the underlying filesystem.

                    On success, all data is erased. The handle is freed and must not be used afterwards.
                    Any error occurring will leave the handle intact.
                "#},
                log_level: LogLevel::Info,
                db_access: DbAccess::ReadWrite,
                versions: vec![
                    FunctionVersion {
                        description: None,
                        log_level: None,
                        db_access: None,
                        flags: vec![],
                        parameters: vec![
                            Parameter{
                                name: "project",
                                r#type: "Project",
                                description: "The project you want to delete.",
                                log_level: LogLevel::Info,
                                validations: vec![
                                    ParameterValidation {
                                        r#type: ParameterValidationType::NotNull,
                                        flag_condition: None,
                                    }
                                ],
                            }
                        ],
                        r#return: None,
                        errors: vec![
                            Error {
                                r#type: INTERNAL_ERROR_IDENT,
                                occurrences: vec![
                                    "When we are unable to close the project before deletion.",
                                    "When we are unable to delete the file from the filesystem."
                                ],
                            }
                        ],
                    }
                ],
            },
            Function {
                name: "get_entities",
                description: indoc!{r#"
                    Fetches a number of entities within a project in an unspecified order.
                    Should be used in conjunction with `get_entities_count` to fetch all entities at once."
                "#},
                log_level: LogLevel::Verbose,
                db_access: DbAccess::ReadOnly,
                versions: vec![
                    FunctionVersion {
                        description: None,
                        log_level: None,
                        db_access: None,
                        flags: vec![],
                        parameters: vec![
                            project_param("The project whose entities you want to fetch."),
                            buffer_out_param("entities", "*mut Id", "The pre-allocated buffer in which we will store the entities."),
                            buffer_out_size_param("entities_out_size", "usize", "The size of the entities_out buffer."),
                            Parameter {
                                name: "entity_types",
                                r#type: "EntityTypes",
                                description: "The types of entities you want to fetch.",
                                log_level: LogLevel::Verbose,
                                validations: vec![],
                            }
                        ],
                        r#return: None,
                        errors: vec![
                            Error {
                                r#type: INTERNAL_ERROR_IDENT,
                                occurrences: vec![
                                    "When we experience an error accessing the project."
                                ],
                            }
                        ],
                    }
                ],
            },
            Function {
                name: "get_entities_count",
                description: indoc!{r#"
                    Fetches the count of entities present in a project.
                    Can be used to figure out the ideal size of the buffer for `get_entities`.
                "#},
                log_level: LogLevel::Verbose,
                db_access: DbAccess::ReadOnly,
                versions: vec![
                    FunctionVersion {
                        description: None,
                        log_level: None,
                        db_access: None,
                        flags: vec![],
                        parameters: vec![
                            project_param("The project whose entity count you want to fetch."),
                            Parameter {
                                name: "entity_types",
                                r#type: "EntityTypes",
                                description: "The types of entities you want to fetch.",
                                log_level: LogLevel::Verbose,
                                validations: vec![],
                            }
                        ],
                        r#return: Some(
                            ReturnType {
                                name: "count",
                                r#type: "usize",
                                description: "The amount of entities present in the project with the given parameters."
                            }
                        ),
                        errors: vec![
                            Error {
                                r#type: INTERNAL_ERROR_IDENT,
                                occurrences: vec![
                                    "When we experience an error accessing the project."
                                ],
                            }
                        ],
                    }
                ],
            }
        ],
    }
}