text-document-io 1.7.0

Import/export for text-document: plain text, Markdown, HTML, LaTeX, DOCX
Documentation
// Generated by Qleany v1.5.1 from feature_controller.tera

use crate::ExportDjotDto;
use crate::ExportDocxDto;
use crate::ExportDocxResultDto;
use crate::ExportHtmlDto;
use crate::ExportLatexDto;
use crate::ExportLatexResultDto;
use crate::ExportMarkdownDto;
use crate::ExportPlainTextDto;
use crate::ImportDjotDto;
use crate::ImportDjotResultDto;
use crate::ImportHtmlDto;
use crate::ImportHtmlResultDto;
use crate::ImportMarkdownDto;
use crate::ImportMarkdownResultDto;
use crate::ImportPlainTextDto;
use crate::units_of_work::export_djot_uow::ExportDjotUnitOfWorkFactory;
use crate::units_of_work::export_docx_uow::ExportDocxUnitOfWorkFactory;
use crate::units_of_work::export_html_uow::ExportHtmlUnitOfWorkFactory;
use crate::units_of_work::export_latex_uow::ExportLatexUnitOfWorkFactory;
use crate::units_of_work::export_markdown_uow::ExportMarkdownUnitOfWorkFactory;
use crate::units_of_work::export_plain_text_uow::ExportPlainTextUnitOfWorkFactory;
use crate::units_of_work::import_djot_uow::ImportDjotUnitOfWorkFactory;
use crate::units_of_work::import_html_uow::ImportHtmlUnitOfWorkFactory;
use crate::units_of_work::import_markdown_uow::ImportMarkdownUnitOfWorkFactory;
use crate::units_of_work::import_plain_text_uow::ImportPlainTextUnitOfWorkFactory;
use crate::use_cases::export_djot_uc::ExportDjotUseCase;
use crate::use_cases::export_docx_uc::ExportDocxUseCase;
use crate::use_cases::export_html_uc::ExportHtmlUseCase;
use crate::use_cases::export_latex_uc::ExportLatexUseCase;
use crate::use_cases::export_markdown_uc::ExportMarkdownUseCase;
use crate::use_cases::export_plain_text_uc::ExportPlainTextUseCase;
use crate::use_cases::import_djot_uc::ImportDjotUseCase;
use crate::use_cases::import_html_uc::ImportHtmlUseCase;
use crate::use_cases::import_markdown_uc::ImportMarkdownUseCase;
use crate::use_cases::import_plain_text_uc::ImportPlainTextUseCase;
use anyhow::Result;
use common::event::{Event, Origin};
use common::parser_tools::DjotExportOptions;

use common::event::DocumentIoEvent::ExportDjot;
use common::event::DocumentIoEvent::ExportHtml;
use common::event::DocumentIoEvent::ExportLatex;
use common::event::DocumentIoEvent::ExportMarkdown;
use common::event::DocumentIoEvent::ExportPlainText;
use common::event::DocumentIoEvent::ImportPlainText;

use common::long_operation::{LongOperationManager, OperationProgress};
use common::{database::db_context::DbContext, event::EventHub};
use std::sync::Arc;

pub fn import_plain_text(
    db_context: &DbContext,
    event_hub: &Arc<EventHub>,
    dto: &ImportPlainTextDto,
) -> Result<()> {
    let uow_context = ImportPlainTextUnitOfWorkFactory::new(db_context, event_hub);
    let mut uc = ImportPlainTextUseCase::new(Box::new(uow_context));
    uc.execute(dto)?;
    // Notify that the handling manifest has been loaded
    event_hub.send_event(Event {
        origin: Origin::DocumentIo(ImportPlainText),
        ids: vec![],
        data: None,
    });
    Ok(())
}

pub fn export_plain_text(
    db_context: &DbContext,
    event_hub: &Arc<EventHub>,
) -> Result<ExportPlainTextDto> {
    let uow_context = ExportPlainTextUnitOfWorkFactory::new(db_context);
    let mut uc = ExportPlainTextUseCase::new(Box::new(uow_context));
    let return_dto = uc.execute()?;
    // Notify that the handling manifest has been loaded
    event_hub.send_event(Event {
        origin: Origin::DocumentIo(ExportPlainText),
        ids: vec![],
        data: None,
    });
    Ok(return_dto)
}

pub fn import_markdown(
    db_context: &DbContext,
    event_hub: &Arc<EventHub>,
    long_operation_manager: &mut LongOperationManager,
    dto: &ImportMarkdownDto,
) -> Result<String> {
    let uow_context = ImportMarkdownUnitOfWorkFactory::new(db_context, event_hub);
    let uc = ImportMarkdownUseCase::new(Box::new(uow_context), dto);
    let operation_id = long_operation_manager.start_operation(uc);
    Ok(operation_id)
}

pub fn get_import_markdown_progress(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Option<OperationProgress> {
    long_operation_manager.get_operation_progress(operation_id)
}

pub fn get_import_markdown_result(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Result<Option<ImportMarkdownResultDto>> {
    // Get the operation result as a JSON string
    let result_json = long_operation_manager.get_operation_result(operation_id);

    // If there's no result, return None
    if result_json.is_none() {
        return Ok(None);
    }
    // Parse the JSON string into a ImportMarkdownResultDto
    let result_dto: ImportMarkdownResultDto = serde_json::from_str(&result_json.unwrap())?;

    Ok(Some(result_dto))
}

pub fn export_markdown(
    db_context: &DbContext,
    event_hub: &Arc<EventHub>,
) -> Result<ExportMarkdownDto> {
    let uow_context = ExportMarkdownUnitOfWorkFactory::new(db_context);
    let mut uc = ExportMarkdownUseCase::new(Box::new(uow_context));
    let return_dto = uc.execute()?;
    // Notify that the handling manifest has been loaded
    event_hub.send_event(Event {
        origin: Origin::DocumentIo(ExportMarkdown),
        ids: vec![],
        data: None,
    });
    Ok(return_dto)
}

pub fn import_djot(
    db_context: &DbContext,
    event_hub: &Arc<EventHub>,
    long_operation_manager: &mut LongOperationManager,
    dto: &ImportDjotDto,
) -> Result<String> {
    let uow_context = ImportDjotUnitOfWorkFactory::new(db_context, event_hub);
    let uc = ImportDjotUseCase::new(Box::new(uow_context), dto);
    let operation_id = long_operation_manager.start_operation(uc);
    Ok(operation_id)
}

pub fn get_import_djot_progress(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Option<OperationProgress> {
    long_operation_manager.get_operation_progress(operation_id)
}

pub fn get_import_djot_result(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Result<Option<ImportDjotResultDto>> {
    let result_json = long_operation_manager.get_operation_result(operation_id);
    if result_json.is_none() {
        return Ok(None);
    }
    let result_dto: ImportDjotResultDto = serde_json::from_str(&result_json.unwrap())?;
    Ok(Some(result_dto))
}

pub fn export_djot(
    db_context: &DbContext,
    event_hub: &Arc<EventHub>,
    options: &DjotExportOptions,
) -> Result<ExportDjotDto> {
    let uow_context = ExportDjotUnitOfWorkFactory::new(db_context);
    let mut uc = ExportDjotUseCase::new(Box::new(uow_context));
    let return_dto = uc.execute(options)?;
    event_hub.send_event(Event {
        origin: Origin::DocumentIo(ExportDjot),
        ids: vec![],
        data: None,
    });
    Ok(return_dto)
}

pub fn import_html(
    db_context: &DbContext,
    event_hub: &Arc<EventHub>,
    long_operation_manager: &mut LongOperationManager,
    dto: &ImportHtmlDto,
) -> Result<String> {
    let uow_context = ImportHtmlUnitOfWorkFactory::new(db_context, event_hub);
    let uc = ImportHtmlUseCase::new(Box::new(uow_context), dto);
    let operation_id = long_operation_manager.start_operation(uc);
    Ok(operation_id)
}

pub fn get_import_html_progress(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Option<OperationProgress> {
    long_operation_manager.get_operation_progress(operation_id)
}

pub fn get_import_html_result(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Result<Option<ImportHtmlResultDto>> {
    // Get the operation result as a JSON string
    let result_json = long_operation_manager.get_operation_result(operation_id);

    // If there's no result, return None
    if result_json.is_none() {
        return Ok(None);
    }
    // Parse the JSON string into a ImportHtmlResultDto
    let result_dto: ImportHtmlResultDto = serde_json::from_str(&result_json.unwrap())?;

    Ok(Some(result_dto))
}

pub fn export_html(db_context: &DbContext, event_hub: &Arc<EventHub>) -> Result<ExportHtmlDto> {
    let uow_context = ExportHtmlUnitOfWorkFactory::new(db_context);
    let mut uc = ExportHtmlUseCase::new(Box::new(uow_context));
    let return_dto = uc.execute()?;
    // Notify that the handling manifest has been loaded
    event_hub.send_event(Event {
        origin: Origin::DocumentIo(ExportHtml),
        ids: vec![],
        data: None,
    });
    Ok(return_dto)
}

pub fn export_latex(
    db_context: &DbContext,
    event_hub: &Arc<EventHub>,
    dto: &ExportLatexDto,
) -> Result<ExportLatexResultDto> {
    let uow_context = ExportLatexUnitOfWorkFactory::new(db_context);
    let mut uc = ExportLatexUseCase::new(Box::new(uow_context));
    let return_dto = uc.execute(dto)?;
    // Notify that the handling manifest has been loaded
    event_hub.send_event(Event {
        origin: Origin::DocumentIo(ExportLatex),
        ids: vec![],
        data: None,
    });
    Ok(return_dto)
}

pub fn export_docx(
    db_context: &DbContext,
    _event_hub: &Arc<EventHub>,
    long_operation_manager: &mut LongOperationManager,
    dto: &ExportDocxDto,
) -> Result<String> {
    let uow_context = ExportDocxUnitOfWorkFactory::new(db_context);
    let uc = ExportDocxUseCase::new(Box::new(uow_context), dto);
    let operation_id = long_operation_manager.start_operation(uc);
    Ok(operation_id)
}

/// Build the in-memory DOCX document for `db_context` without writing a file.
///
/// This runs the exact same builder used by [`export_docx`] but returns the
/// assembled [`docx_rs::Docx`] instead of packing it to disk, so callers can
/// inspect the produced structure. Intended for tests; the type is re-exported
/// as [`crate::docx_rs`] so callers can name it.
#[doc(hidden)]
pub fn build_docx_document(db_context: &DbContext, dto: &ExportDocxDto) -> Result<docx_rs::Docx> {
    let uow_context = ExportDocxUnitOfWorkFactory::new(db_context);
    let uc = ExportDocxUseCase::new(Box::new(uow_context), dto);
    let (docx, _paragraph_count) = uc.build_document()?;
    Ok(docx)
}

pub fn get_export_docx_progress(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Option<OperationProgress> {
    long_operation_manager.get_operation_progress(operation_id)
}

pub fn get_export_docx_result(
    long_operation_manager: &LongOperationManager,
    operation_id: &str,
) -> Result<Option<ExportDocxResultDto>> {
    // Get the operation result as a JSON string
    let result_json = long_operation_manager.get_operation_result(operation_id);

    // If there's no result, return None
    if result_json.is_none() {
        return Ok(None);
    }
    // Parse the JSON string into a ExportDocxResultDto
    let result_dto: ExportDocxResultDto = serde_json::from_str(&result_json.unwrap())?;

    Ok(Some(result_dto))
}