cottas_rs/lib.rs
1//! Main library API for COTTAS-RS based on PYCOTTAS.
2//!
3//! # Modules
4//! - `duckdb`: DuckDB integration and utilities.
5//! - `export`: Exporting data to Cottas format.
6//! - `parser`: RDF file parsing utilities.
7//! - `utils`: Helper functions.
8
9pub mod duckdb;
10pub mod export;
11pub mod parser;
12pub mod utils;
13
14use crate::duckdb::{diff_duckdb, info_duckdb, verify_duckdb, CottasInfo};
15pub use duckdb::{
16 cat_duckdb, connection_in_memory, has_column, load_into_duckdb, search_in_duckdb,
17};
18pub use export::{export_to_cottas, write_quads_to_file};
19pub use parser::parse_rdf_file;
20use std::error::Error;
21use std::fs::File;
22pub use utils::extract_format;
23
24/// Converts an RDF file to a Cottas file, using the specified index.
25///
26/// # Arguments
27/// * `rdf_file_path` - Path to the input RDF file.
28/// * `cottas_file_path` - Path to the output Cottas file.
29/// * `index` - Index type (e.g., "spo").
30///
31/// # Errors
32/// Returns an error if parsing, loading, or exporting fails.
33pub fn rdf2_cottas(
34 rdf_file_path: &str,
35 cottas_file_path: &str,
36 index: &str,
37) -> Result<(), Box<dyn Error>> {
38 let quads = parse_rdf_file(rdf_file_path)?;
39 let quad_mode = quads.iter().any(|q| q.3.is_some());
40 let conn = load_into_duckdb(&quads);
41 export_to_cottas(&conn, index, cottas_file_path, quad_mode)?;
42 Ok(())
43}
44
45/// Converts a Cottas file back to RDF format.
46///
47/// # Arguments
48/// * `cottas_file_path` - Path to the input Cottas file.
49/// * `rdf_file_path` - Path to the output RDF file.
50///
51/// # Errors
52/// Returns an error if file creation or writing fails.
53pub fn cottas2_rdf(cottas_file_path: &str, rdf_file_path: &str) -> Result<(), Box<dyn Error>> {
54 let conn = connection_in_memory();
55 let has_named_graph = has_column(&conn, cottas_file_path, "g")?;
56 let mut file = File::create(rdf_file_path)?;
57 write_quads_to_file(&conn, cottas_file_path, has_named_graph, &mut file)?;
58 Ok(())
59}
60
61/// Searches for triples/quads in a Cottas file matching a pattern.
62///
63/// # Arguments
64/// * `cottas_file_path` - Path to the Cottas file.
65/// * `triple_pattern` - Pattern to search for.
66///
67/// # Returns
68/// A vector of matching results.
69///
70/// # Errors
71/// Returns an error if the search fails.
72pub fn search(
73 cottas_file_path: &str,
74 triple_pattern: &str,
75) -> Result<Vec<Vec<String>>, Box<dyn Error>> {
76 search_in_duckdb(cottas_file_path, triple_pattern)
77}
78
79/// Concatenates multiple Cottas files into one.
80///
81/// # Arguments
82/// * `cottas_file_paths` - Array of input file paths
83/// * `cottas_cat_file_path` - Output file path.
84/// * `index` - Optional index type.
85/// * `remove_input_files` - Optionally remove input files after concatenation.
86///
87/// # Errors
88/// Returns an error if concatenation fails.
89pub fn cat(
90 cottas_file_paths: &[String],
91 cottas_cat_file_path: &str,
92 index: Option<&str>,
93 remove_input_files: Option<bool>,
94) -> Result<(), Box<dyn Error>> {
95 let index = index.unwrap_or("spo");
96 let remove_input_files = remove_input_files.unwrap_or(false);
97 cat_duckdb(
98 cottas_file_paths,
99 cottas_cat_file_path,
100 index,
101 remove_input_files,
102 )
103}
104
105/// Computes the difference between two Cottas files.
106///
107/// # Arguments
108/// * `cottas_file_1_path` - First input file.
109/// * `cottas_file_2_path` - Second input file.
110/// * `cottas_diff_file_path` - Output file for the diff.
111/// * `index` - Optional index type.
112/// * `remove_input_files` - Optionally remove input files after diff.
113///
114/// # Errors
115/// Returns an error if diffing fails.
116pub fn diff(
117 cottas_file_1_path: &str,
118 cottas_file_2_path: &str,
119 cottas_diff_file_path: &str,
120 index: Option<&str>,
121 remove_input_files: Option<bool>,
122) -> Result<(), Box<dyn Error>> {
123 let index = index.unwrap_or("spo");
124 let remove_input_files = remove_input_files.unwrap_or(false);
125 diff_duckdb(
126 cottas_file_1_path,
127 cottas_file_2_path,
128 cottas_diff_file_path,
129 index,
130 remove_input_files,
131 )
132}
133
134/// Retrieves information about a Cottas file.
135///
136/// # Arguments
137/// * `cottas_file_path` - Path to the Cottas file.
138///
139/// # Returns
140/// `CottasInfo` struct with file metadata.
141///
142/// # Errors
143/// Returns an error if info retrieval fails.
144pub fn info(cottas_file_path: &str) -> Result<CottasInfo, Box<dyn Error>> {
145 info_duckdb(cottas_file_path)
146}
147
148/// Verifies the integrity of a Cottas file.
149///
150/// # Arguments
151/// * `cottas_file_path` - Path to the Cottas file.
152///
153/// # Returns
154/// `true` if the file is valid, `false` otherwise.
155///
156/// # Errors
157/// Returns an error if verification fails.
158pub fn verify(cottas_file_path: &str) -> Result<bool, Box<dyn Error>> {
159 verify_duckdb(cottas_file_path)
160}