Browse Source

Added stuff for DB support

main
Diane 7 months ago
parent
commit
8169b31cea
No known key found for this signature in database GPG Key ID: 2D0649E76A7C8D25
  1. 9
      .editorconfig
  2. 127
      Cargo.lock
  3. 2
      Cargo.toml
  4. 9
      README.md
  5. 2
      diesel.toml
  6. 1
      migrations/2021-05-09-115830_create_recipes/down.sql
  7. 7
      migrations/2021-05-09-115830_create_recipes/up.sql
  8. 23
      src/main.rs
  9. 9
      src/store/db.rs
  10. 3
      src/store/mod.rs
  11. 9
      src/store/models.rs
  12. 8
      src/store/schema.rs

9
.editorconfig

@ -0,0 +1,9 @@
root = true
[*]
charset = utf-8
end_of_line = lf
indent_size = tab
indent_style = tab
insert_final_newline = true
trim_trailing_whitespace = true

127
Cargo.lock

@ -2,6 +2,133 @@
# It is not intended for manual editing.
version = 3
[[package]]
name = "byteorder"
version = "1.4.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "14c189c53d098945499cdfa7ecc63567cf3886b3332b312a5b4585d8d3a6a610"
[[package]]
name = "cfg-if"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd"
[[package]]
name = "diesel"
version = "1.4.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "047bfc4d5c3bd2ef6ca6f981941046113524b9a9f9a7cbdfdd7ff40f58e6f542"
dependencies = [
"byteorder",
"diesel_derives",
"libsqlite3-sys",
]
[[package]]
name = "diesel_derives"
version = "1.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "45f5098f628d02a7a0f68ddba586fb61e80edec3bdc1be3b921f4ceec60858d3"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "getrandom"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c9495705279e7140bf035dde1f6e750c162df8b625267cd52cc44e0b156732c8"
dependencies = [
"cfg-if",
"libc",
"wasi",
]
[[package]]
name = "libc"
version = "0.2.94"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "18794a8ad5b29321f790b55d93dfba91e125cb1a9edbd4f8e3150acc771c1a5e"
[[package]]
name = "libsqlite3-sys"
version = "0.20.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "64d31059f22935e6c31830db5249ba2b7ecd54fd73a9909286f0a67aa55c2fbd"
dependencies = [
"pkg-config",
"vcpkg",
]
[[package]]
name = "pkg-config"
version = "0.3.19"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3831453b3449ceb48b6d9c7ad7c96d5ea673e9b470a1dc578c2ce6521230884c"
[[package]]
name = "proc-macro2"
version = "1.0.26"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a152013215dca273577e18d2bf00fa862b89b24169fb78c4c95aeb07992c9cec"
dependencies = [
"unicode-xid",
]
[[package]]
name = "quote"
version = "1.0.9"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c3d0b9745dc2debf507c8422de05d7226cc1f0644216dfdfead988f9b1ab32a7"
dependencies = [
"proc-macro2",
]
[[package]]
name = "recipes"
version = "0.1.0"
dependencies = [
"diesel",
"uuid",
]
[[package]]
name = "syn"
version = "1.0.72"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a1e8cdbefb79a9a5a65e0db8b47b723ee907b7c7f8496c76a1770b5c310bab82"
dependencies = [
"proc-macro2",
"quote",
"unicode-xid",
]
[[package]]
name = "unicode-xid"
version = "0.2.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8ccb82d61f80a663efe1f787a51b16b5a51e3314d6ac365b08639f52387b33f3"
[[package]]
name = "uuid"
version = "0.8.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bc5cf98d8186244414c848017f0e2676b3fcb46807f6668a97dfe67359a3c4b7"
dependencies = [
"getrandom",
]
[[package]]
name = "vcpkg"
version = "0.2.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cbdbff6266a24120518560b5dc983096efb98462e51d0d68169895b237be3e5d"
[[package]]
name = "wasi"
version = "0.10.2+wasi-snapshot-preview1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fd6fbd9a79829dd1ad0cc20627bf1ed606756a7f77edff7b66b7064f9cb327c6"

2
Cargo.toml

@ -6,3 +6,5 @@ edition = "2018"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
diesel = { version = "1.4.4", features = ["sqlite"] }
uuid = { version = "0.8", features = ["v4"] }

9
README.md

@ -2,4 +2,11 @@
![This project is licensed under the AGPLv3 terms](.agplv3.png)
WIP
I don't know yet where I'm going with this, but it's basically another take at my recipe manager thingy, except in rust,
because I'd like to learn rust.
__Most `diesel`-bound projects use `dotenv` for the configuration, why don't you?__
Because `dotenv` is a completely irresponsible idiocy, completely inverting the problem.
If I want to provide something to my software through the environment, it is up to the environment to ensure that it's
provided, not my software.

2
diesel.toml

@ -0,0 +1,2 @@
[print_schema]
file = "src/store/schema.rs"

1
migrations/2021-05-09-115830_create_recipes/down.sql

@ -0,0 +1 @@
DROP TABLE recipes;

7
migrations/2021-05-09-115830_create_recipes/up.sql

@ -0,0 +1,7 @@
CREATE TABLE recipes
(
id VARCHAR NOT NULL PRIMARY KEY,
title VARCHAR NOT NULL,
description VARCHAR NOT NULL,
instructions VARCHAR NOT NULL
);

23
src/main.rs

@ -1,3 +1,24 @@
use crate::store::db::connect;
use crate::store::models::Recipe;
#[macro_use]
extern crate diesel;
use store::schema::recipes::dsl::*;
use diesel::prelude::*;
mod store;
fn main() {
println!("Hello, world!");
let db = connect();
let res = recipes
.limit(10)
.load::<Recipe>(&db)
.unwrap();
println!("{} recipes", res.len());
for recipe in res {
println!("{}: {}", recipe.id, recipe.title);
}
}

9
src/store/db.rs

@ -0,0 +1,9 @@
use diesel::{Connection, SqliteConnection};
use std::env;
pub fn connect() -> SqliteConnection {
let database_url = env::var("DATABASE_URL")
.expect("DATABASE_URL must be set");
SqliteConnection::establish(&database_url)
.unwrap_or_else(|err| panic!("Error connecting to {}: {}", database_url, err))
}

3
src/store/mod.rs

@ -0,0 +1,3 @@
pub mod schema;
pub mod models;
pub mod db;

9
src/store/models.rs

@ -0,0 +1,9 @@
use super::schema::recipes;
#[derive(Queryable, Insertable)]
pub struct Recipe {
pub id: String,
pub title: String,
pub description: String,
pub instructions: String,
}

8
src/store/schema.rs

@ -0,0 +1,8 @@
table! {
recipes (id) {
id -> Text,
title -> Text,
description -> Text,
instructions -> Text,
}
}
Loading…
Cancel
Save