sqlitefs 0.0.6

In-App Filesystem using SQLite
Documentation
#![crate_name = "sqlitefs"]

//! # SQLiteFS
//! SQLiteFS is a Rust Crate to provide you with an In-App Filesystem based on SQLite.
//!
//! The base for this are the SQLite Docs:
//! - SQLite As An Application File Format: <https://sqlite.org/appfileformat.html>
//! - SQLite Archive Files: <https://sqlite.org/sqlar.html>
//!
//! This crate is basically just a wrapper around a SQLite Database.
//!
//! This crate makes use of [`sqlx`] and uses the default (bundled) feature.
//! So SQLite should be included on build and there should be no need to
//! install SQLite separately on your machine.
//!
//! ## Example 1 - In-Memory FS
//! ```rust
//! use sqlitefs::{
//!     datatypes::FsStatus,
//!     fs::{FsMode, SqliteFs},
//! };
//! use std::io::Read;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), FsStatus> {
//!     // read a file so we have some data to work with
//!     let mut s = String::new();
//!     let mut x = std::fs::File::open("Cargo.toml").unwrap();
//!     x.read_to_string(&mut s).unwrap();
//!     drop(x);
//!     // ----------------------------------------------------------------------
//!
//!     // Init the FS. Must be called befor anything usefull can be done with it
//!     SqliteFs::init(FsMode::Memory).await?;
//!
//!     for i in 1..=100 {
//!         let uuid = uuid::Uuid::now_v7();
//!         let f_name = format!("/my/new/file_{i}_{uuid}");
//!
//!         SqliteFs::write(&f_name, &s.as_bytes()).await?;
//!     }
//!
//!     // Optional: Save the memory fs to disk. Otherwise everything will get lost.
//!     // Right now there is no way to load such saved FS back into memory
//!     // let fs_path = "FS.SQLITE".to_string();
//!     // SqliteFs::save_memory_fs_to_disk(&fs_path).await?;
//!
//!     Ok(())
//! }
//! ```
//!
//! ## Example 2 - Persistent FS
//! ```rust
//! use sqlitefs::{
//!     datatypes::FsStatus,
//!     fs::{FsMode, SqliteFs},
//! };
//! use std::io::Read;
//!
//! #[tokio::main]
//! async fn main() -> Result<(), FsStatus> {
//!     // read a file so we have some data to work with
//!     let mut s = String::new();
//!     let mut x = std::fs::File::open("Cargo.toml").unwrap();
//!     x.read_to_string(&mut s).unwrap();
//!
//!     // ----------------------------------------------------------------------
//!
//!     // Init the FS. Must be called befor anything usefull can be done with it
//!     let fs_path = "FS.SQLITE".to_string();
//!     SqliteFs::init(FsMode::Persist(fs_path)).await?;
//!
//!     for i in 1..=100 {
//!         let uuid = uuid::Uuid::now_v7();
//!         let f_name = format!("/my/new/file_{i}_{uuid}");
//!
//!         SqliteFs::write(&f_name, &s.as_bytes()).await?;
//!     }
//!
//!     Ok(())
//! }
//!
//! ```

// ------------------------------------------------------------------------

use fs::SqliteFs;
use std::sync::LazyLock;
use tokio::sync::Mutex;

// ------------------------------------------------------------------------

pub mod datatypes;
pub mod dtos;
// pub mod file;
pub mod fs;
pub(crate) mod query;

// ------------------------------------------------------------------------

///
/// FS Handle
///
static FS: LazyLock<Mutex<Option<SqliteFs>>> = LazyLock::new(|| Mutex::new(None));

///
/// Represents if the FS was already initialized
///
static FS_IS_INIT: LazyLock<Mutex<bool>> = LazyLock::new(|| Mutex::new(false));