NeuxDb API Documentation
Version: 0.1.0
NeuxDb is an embedded database library that stores data in pipe-delimited CSV files with per-table JSON schemas. It provides a simple SQL-like query language for table creation, data manipulation, and retrieval.
Table of Contents
- Re-exports
- Configuration
neuxdb::config - Types
neuxdb::types - Error Handling
neuxdb::error - Core Compiler
neuxdb::core::compiler - Core Syntax
neuxdb::core::syntax - Core Service
neuxdb::core::service - Core Storage
neuxdb::core::storage - Examples
Re-exports
All essential items are re-exported at the crate root:
pub use *;
pub use parse;
pub use *;
pub use ;
pub use ;
pub use ;
pub use ;
Configuration neuxdb::config
fn delimiter_byte
Returns the byte value of the field delimiter used in data files (currently |, as u8).
fn delimiter_char
Returns the character value of the field delimiter used in data files (currently '|').
fn ensure_data_dir
Ensures that the configured data directory exists. If the directory does not exist, it will be created automatically. The data directory location can be controlled by the environment variable NEUXDB_DATA_DIR; if not set, "data" is used.
Errors:
Returns NeuxDbError::Io if the directory cannot be created.
fn sanitize_table_name
Validates and sanitizes a table name.
- Rejects empty strings.
- Rejects path traversal sequences (e.g.,
.., absolute paths). - Only allows ASCII alphanumeric characters, underscores, and hyphens.
Errors:
Returns NeuxDbError::Parse if the name is invalid.
fn table_path
Constructs the path to the CSV data file for a given table. The file extension is .nxdb.
Errors:
Propagates errors from sanitize_table_name.
fn schema_path
Constructs the path to the JSON schema file for a given table. The schema file has extension .schema.json.
Errors:
Propagates errors from sanitize_table_name.
Types neuxdb::types
enum Value
Represents a single cell value. Two variants:
Text(String)– string data.Int(i64)– signed 64-bit integer.
Implements:
From<&str>andFrom<String>– converts from string; if parsing to integer succeeds, createsInt, otherwiseText.Display– formats the value as a string.
Methods:
as_text(&self) -> Option<&str>– extracts the string ifText.as_int(&self) -> Option<i64>– extracts the integer ifInt.to_like_string(&self) -> String– returns a string representation for LIKE pattern matching.
type Row
pub type Row = ;
A row of data is a vector of Values.
enum ColumnType
Defines the expected type of a column.
Implements Display, Serialize, Deserialize.
struct TableSchema
Describes the structure of a table: column names and their types.
Implements Serialize, Deserialize.
Methods:
new(columns: Vec<String>) -> Self– creates a schema where all columns default toColumnType::Text.validate_value(&self, col_index: usize, value: &Value) -> Result<()>– checks that the value matches the column’s type. ReturnsNeuxDbError::TypeMismatchif not.
Error Handling neuxdb::error
enum NeuxDbError
All errors produced by the library are variants of this enum. Each variant carries contextual information.
type Result
pub type Result<T> = Result;
Type alias for results returned by NeuxDb functions.
Core Compiler neuxdb::core::compiler
fn parse
Parses a SQL-like statement string into a Statement enum. The parser is case‑insensitive for keywords and supports:
CREATE TABLE name (col1, col2, ...)DROP TABLE nameSHOW TABLESINSERT INTO name VALUES (val1|val2|...)SELECT columns FROM name [WHERE condition]UPDATE name SET col=val WHERE conditionDELETE FROM name WHERE condition
Conditions (WHERE) support =, !=, <>, <, >, <=, >=, LIKE, and combinations with AND, OR.
Errors: Returns NeuxDbError::Parse with a detailed message if the input is invalid.
Core Syntax neuxdb::core::syntax
enum Statement
Represents a parsed statement. Variants correspond to the supported commands.
enum WhereClause
Expresses a filter condition. Supports nesting with And/Or.
enum ComparisonOp
Operators available in WhereClause.
fn split_quoted
Splits a string by a delimiter, respecting single-quoted substrings. Used internally for parsing value lists.
Core Service neuxdb::core::service
fn create_table
Creates a new table with the given columns. All columns are created with type Text. A corresponding schema file is saved, and an empty data file is written with the column headers.
Errors:
TableAlreadyExistsif the table already exists.- I/O errors during file creation.
fn drop_table
Deletes the table’s data file (name.nxdb) and its schema file (name.schema.json).
Errors: I/O errors if removal fails.
fn show_tables
Returns a sorted list of existing table names (without extensions).
Errors: I/O errors reading the data directory.
fn insert_row
Inserts a single row into an existing table.
- Validates the number of values matches the columns.
- Validates each value against the column’s type (see
TableSchema::validate_value). - Writes the updated data back atomically.
Errors:
TableNotFoundValueCountMismatchTypeMismatch- I/O and lock errors.
fn select_rows
Retrieves rows from a table.
- If columns contains
"*", all columns are returned. - If a condition is provided, only matching rows are included.
- Conditions support all comparison operators (
=,!=,<, etc.) andLIKE(with%and_wildcards), as well asAND/ORcombinations.
Errors: TableNotFound, ColumnNotFound, I/O and lock errors.
fn update_rows
Updates rows that match the condition by setting a column to a new value. Returns the number of modified rows.
Errors: TableNotFound, ColumnNotFound, I/O and lock errors.
fn delete_rows
Deletes rows that match the condition. Returns the number of deleted rows.
Errors: TableNotFound, I/O and lock errors.
fn format_table
Formats a header and data rows into a human-readable table with aligned columns and separators. If no rows are present, the output includes (no rows).
Example output:
id | name
---+-----
1 | Alice
2 | Bob
fn parse_condition
Parses a simple column=value string (supporting single‑quoted values) into a tuple.
Used for backward compatibility; prefer using parse and WhereClause directly.
fn parse_assignment
Parses a column=value string into a tuple.
fn run_script
Reads a file line by line, skipping comments (#) and empty lines, and calls the callback for each SQL statement. If the callback returns an error, it is wrapped with a line number.
Core Storage neuxdb::core::storage
fn read_table
Reads the entire table, returning column headers and all rows.
- Acquires a shared lock while reading.
- Values are parsed according to the column types defined in the schema.
Errors: TableNotFound, I/O error, schema error, CSV error.
fn write_table
Writes headers and rows atomically to the table file.
- Creates a temporary file, writes the data, then renames it to the target path.
- Acquires an exclusive lock during write.
fn create_table_schema
Creates a new table file with the given columns and writes the corresponding schema. Used internally by create_table.
Examples
Basic Table Creation and Query
use *;
// Ensure the data directory exists
ensure_data_dir.unwrap;
// Create a table
create_table.unwrap;
// Insert a row
insert_row.unwrap;
// Select all rows
let rows = select_rows.unwrap;
let = read_table.unwrap;
println!;
Using WHERE Conditions
use *;
let condition = Condition ;
let rows = select_rows.unwrap;
for row in rows
Drop and Show Tables
drop_table.unwrap;
let tables = show_tables.unwrap;
println!;