Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

host: add persistence #69

Closed
wants to merge 3 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
170 changes: 169 additions & 1 deletion host/Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions host/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
directories = "5.0.1"
regex = "1.10.2"
rusqlite = { version = "0.30.0", features = ["bundled"] }
semver = { version = "1.0.20", features = ["serde"] }
Expand Down
2 changes: 1 addition & 1 deletion host/src/db.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,7 +22,7 @@ SELECT EXISTS
const SELECT_LATEST_VERSION: &str = "\
SELECT major, minor, patch
FROM schema_version
ORDER BY date_applied DESC
ORDER BY applied_at DESC
LIMIT 1
";

Expand Down
6 changes: 4 additions & 2 deletions host/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,8 @@
mod db;
pub mod message;

use std::path::Path;

use regex::Regex;
use rusqlite::Connection;
use serde_json::Value;
Expand Down Expand Up @@ -85,8 +87,8 @@ fn make_process(re: Regex) -> impl Fn(Query) -> String {
}

impl Context {
pub fn new() -> Result<Self, Error> {
let mut connection = Connection::open_in_memory()?;
pub fn new(db_path: impl AsRef<Path>) -> Result<Self, Error> {
let mut connection = Connection::open(db_path)?;
db::init_tables(&mut connection)?;
let process_regex = Regex::new(r"\W+").unwrap();
let process = Box::new(make_process(process_regex));
Expand Down
16 changes: 15 additions & 1 deletion host/src/main.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
use std::io::{self, BufReader, BufWriter, Read, Write};

use directories::ProjectDirs;
use serde_json::Value;

use noematic::{
Expand All @@ -14,6 +15,7 @@ enum Error {
Io(io::Error),
Json(serde_json::Error),
Noematic(noematic::Error),
MissingHomeDir,
UnsupportedVersion,
UnsupportedLength,
}
Expand All @@ -24,6 +26,7 @@ impl std::fmt::Display for Error {
Error::Io(e) => write!(f, "IO error: {}", e),
Error::Json(e) => write!(f, "JSON error: {}", e),
Error::Noematic(e) => write!(f, "{}", e),
Error::MissingHomeDir => write!(f, "Missing home directory"),
Error::UnsupportedVersion => write!(f, "Unsupported version"),
Error::UnsupportedLength => write!(f, "Unsupported length"),
}
Expand Down Expand Up @@ -93,11 +96,22 @@ fn write_response(writer: &mut impl Write, response: Response) -> Result<(), Err
Ok(())
}

fn get_project_dirs() -> Result<ProjectDirs, Error> {
ProjectDirs::from("com.github", "henrytill", "noematic").ok_or(Error::MissingHomeDir)
}

fn main() -> Result<(), Error> {
let mut reader = BufReader::new(io::stdin());
let mut writer = BufWriter::new(io::stdout());

let mut context = Context::new()?;
let db_path = {
let project_dirs: ProjectDirs = get_project_dirs()?;
let db_dir = project_dirs.data_dir();
std::fs::create_dir_all(&db_dir)?;
db_dir.join("db.sqlite3")
};

let mut context = Context::new(db_path)?;

while let Some(message) = read(&mut reader)? {
let json: Value = serde_json::from_slice(&message)?;
Expand Down
Loading