title: Diaryx Core Library
author: adammharris
audience:
- public
part_of: ../../README.md
Diaryx Core Library
This is the diaryx_core library! It contains shared code for the Diaryx clients.
Async-first Architecture
This library uses an async-first design. All core modules (Workspace, Validator, Exporter, Searcher, Publisher) use the AsyncFileSystem trait for filesystem operations.
For CLI/native code: Wrap a sync filesystem with SyncToAsyncFs and use futures_lite::future::block_on():
use ;
use Workspace;
let fs = new;
let workspace = new;
// Use block_on for sync contexts
let tree = block_on;
For WASM: Implement AsyncFileSystem directly using JS promises/IndexedDB.
Quick overview
diaryx_core
└── src
Provided functionality
Managing frontmatter
Full key-value operations for managing frontmatter properties:
set_frontmatter_propertyget_frontmatter_propertyrename_frontmatter_propertyremove_frontmatter_propertyget_all_frontmatter
Also, sorting frontmatter properties:
sort_frontmattersort_alphabeticallysort_by_pattern
Managing file content
Operations for managing content of markdown files separate from frontmatter:
set_contentget_contentappend_contentclear_content
Search
Search frontmatter or content separately:
SearchQuery::contentSearchQuery::frontmatter
Export
use ;
use ;
use Path;
let workspace_root = new;
let audience = "public";
let destination = new;
let fs = new;
let exporter = new;
// Use futures_lite::future::block_on for sync contexts
let plan = block_on.unwrap;
let force = false;
let keep_audience = false;
let options = ExportOptions ;
let result = block_on;
match result
Validation
The validate module provides functionality to check workspace link integrity and automatically fix issues.
Validator
The Validator struct checks part_of and contents references within a workspace:
use Validator;
use ;
use Path;
let fs = new;
let validator = new;
// Validate entire workspace starting from root index
let root_path = new;
let result = block_on.unwrap;
// Or validate a single file
let file_path = new;
let result = block_on.unwrap;
if result.is_ok else
Validation Errors
BrokenPartOf- A file'spart_ofpoints to a non-existent fileBrokenContentsRef- An index'scontentsreferences a non-existent fileBrokenAttachment- A file'sattachmentsreferences a non-existent file
Validation Warnings
OrphanFile- A markdown file not referenced by any indexUnlinkedEntry- A file/directory not in the contents hierarchyUnlistedFile- A markdown file in a directory but not in the index's contentsCircularReference- Circular reference detected in workspace hierarchyNonPortablePath- A path contains absolute paths or./..componentsMultipleIndexes- Multiple index files in the same directoryOrphanBinaryFile- A binary file not referenced by any attachmentsMissingPartOf- A non-index file has nopart_ofproperty
ValidationFixer
The ValidationFixer struct provides methods to automatically fix validation issues:
use ;
use ;
use Path;
let fs = new;
let validator = new;
let fixer = new;
// Validate workspace
let root_path = new;
let result = block_on.unwrap;
// Fix all issues at once
let = block_on;
for fix in error_fixes.iter.chain
// Or fix individual issues (all methods are async)
block_on;
Publish
Templates
Workspaces
Date parsing
Shared errors
Configuration
Filesystem abstraction
The fs module provides filesystem abstraction through two traits: FileSystem (synchronous) and AsyncFileSystem (asynchronous).
Note: As of the async-first refactor, all core modules (Workspace, Validator, Exporter, Searcher, Publisher) use AsyncFileSystem. For synchronous contexts (CLI, tests), wrap a sync filesystem with SyncToAsyncFs and use futures_lite::future::block_on().
FileSystem trait
The synchronous FileSystem trait provides basic implementations:
RealFileSystem- Native filesystem usingstd::fs(not available on WASM)InMemoryFileSystem- In-memory implementation, useful for WASM and testing
use ;
use Path;
// Create an in-memory filesystem
let fs = new;
// Write a file (sync)
fs.write_file.unwrap;
// Read it back
let content = fs.read_to_string.unwrap;
assert_eq!;
AsyncFileSystem trait (Primary API)
The AsyncFileSystem trait is the primary API for all core modules:
- WASM environments where JavaScript APIs (like IndexedDB) are async
- Native code using async runtimes like tokio
- All workspace operations (Workspace, Validator, Exporter, etc.)
use ;
use Workspace;
use Path;
// Wrap a sync filesystem for use with async APIs
let sync_fs = new;
let async_fs = new;
// Use with Workspace (async)
let workspace = new;
// For sync contexts, use block_on
let tree = block_on;
SyncToAsyncFs adapter
The SyncToAsyncFs struct wraps any synchronous FileSystem implementation to provide an AsyncFileSystem interface. This is the recommended way to use the async-first API in synchronous contexts:
use ;
use Workspace;
// For native code
let fs = new;
let workspace = new;
// For tests/WASM
let fs = new;
let workspace = new;
// Access the inner sync filesystem if needed
// let inner = async_fs.inner();