Compare commits
No commits in common. "72fb3b11ee870f406b28aa2b2c77561e43dab199" and "0f98dc452370f0fc2052e37684b7b49e7880145c" have entirely different histories.
72fb3b11ee
...
0f98dc4523
|
@ -3,10 +3,7 @@ name = "nm-store-cli"
|
||||||
version = "0.1.0"
|
version = "0.1.0"
|
||||||
authors = ["Elf M. Sternberg <elf.sternberg@gmail.com>"]
|
authors = ["Elf M. Sternberg <elf.sternberg@gmail.com>"]
|
||||||
edition = "2018"
|
edition = "2018"
|
||||||
description = "Command-line direct access to the notesmachine store."
|
|
||||||
readme = "./README.org"
|
|
||||||
|
|
||||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||||
|
|
||||||
[dependencies]
|
[dependencies]
|
||||||
|
|
||||||
|
|
|
@ -14,10 +14,14 @@ representations:
|
||||||
|
|
||||||
** Plans
|
** Plans
|
||||||
|
|
||||||
|
*** TODO Make it possible to save a note
|
||||||
|
*** TODO Make it possible to retrieve a note
|
||||||
|
*** TODO Read how others use SQLX to initialize the database
|
||||||
|
*** TODO Implement CLI features
|
||||||
*** TODO Make it possible to connect two notes
|
*** TODO Make it possible to connect two notes
|
||||||
|
*** TODO Make it possible to save a page
|
||||||
*** TODO Make it possible to connect a note to a page
|
*** TODO Make it possible to connect a note to a page
|
||||||
*** TODO Make it possible to retrieve a collection of notes
|
*** TODO Make it possible to retrieve a collection of notes
|
||||||
*** TODO Make it possible to retrieve a page
|
*** TODO Make it possible to retrieve a page
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
|
|
@ -1,46 +0,0 @@
|
||||||
# Storage layer for Notesmachine
|
|
||||||
|
|
||||||
This library implements the core functionality of Notesmachine and
|
|
||||||
describes that functionality to a storage layer. There's a bit of
|
|
||||||
intermingling in here which can't be helped, although it may make sense
|
|
||||||
in the future to separate the decomposition of the note content into a
|
|
||||||
higher layer.
|
|
||||||
|
|
||||||
Notesmachine storage notes consist of two items: Zettle and Kasten,
|
|
||||||
which are German for "Note" and "Box". Here are the basic rules:
|
|
||||||
|
|
||||||
- Boxes have titles (and date metadata)
|
|
||||||
- Notes have content and a type (and date metadata)
|
|
||||||
- Notes are stored in boxes
|
|
||||||
- Notes are positioned with respect to other notes.
|
|
||||||
- There are two positions:
|
|
||||||
- Siblings, creating lists
|
|
||||||
- Children, creating trees like this one
|
|
||||||
- Notes may have references (pointers) to other boxes
|
|
||||||
- Notes may be moved around
|
|
||||||
- Notes may be deleted
|
|
||||||
- Boxes may be deleted
|
|
||||||
- When a box is renamed, every reference to that box is auto-edited to
|
|
||||||
reflect the change. If a box is renamed to match an existing box, the
|
|
||||||
notes in both boxes are merged.
|
|
||||||
|
|
||||||
Note-to-note relationships form trees, and are kept in a SQL database of
|
|
||||||
(`parent_id`, `child_id`, `position`, `relationship_type`). The
|
|
||||||
`position` is a monotonic index on the parent (that is, every pair
|
|
||||||
(`parent_id`, `position`) must be unique). The `relationship_type` is
|
|
||||||
an enum and can specify that the relationship is *original*,
|
|
||||||
*embedding*, or *referencing*. An embedded or referenced note may be
|
|
||||||
read/write or read-only with respect to the original, but there is only
|
|
||||||
one original note at any time.
|
|
||||||
|
|
||||||
Note-to-box relationships form a graph, and are kept in the SQL database
|
|
||||||
as a collection of *edges* from the note to the box (and naturally
|
|
||||||
vice-versa).
|
|
||||||
|
|
||||||
- Decision: When an original note is deleted, do all references and
|
|
||||||
embeddings also get deleted, or is the oldest one elevated to be a new
|
|
||||||
"original"? Or is that something the user may choose?
|
|
||||||
|
|
||||||
- Decision: Should the merging issue be handled at this layer, or would
|
|
||||||
it make sense to move this to a higher layer, and only provide the
|
|
||||||
hooks for it here?
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
use sqlx;
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
/// All the ways looking up objects can fail
|
/// All the ways looking up objects can fail
|
||||||
|
|
|
@ -1,7 +1,8 @@
|
||||||
mod errors;
|
mod errors;
|
||||||
mod reference_parser;
|
mod row_structs;
|
||||||
mod store;
|
mod store;
|
||||||
mod structs;
|
mod structs;
|
||||||
|
mod reference_parser;
|
||||||
|
|
||||||
pub use crate::errors::NoteStoreError;
|
pub use crate::errors::NoteStoreError;
|
||||||
pub use crate::store::NoteStore;
|
pub use crate::store::NoteStore;
|
||||||
|
|
|
@ -37,7 +37,7 @@ pub(crate) fn find_links(document: &str) -> Vec<String> {
|
||||||
}
|
}
|
||||||
|
|
||||||
match &node.data.borrow().value {
|
match &node.data.borrow().value {
|
||||||
NodeValue::Text(ref text) => Some(
|
&NodeValue::Text(ref text) => Some(
|
||||||
RE_REFERENCES
|
RE_REFERENCES
|
||||||
.captures_iter(text)
|
.captures_iter(text)
|
||||||
.map(|t| String::from_utf8_lossy(&t.get(1).unwrap().as_bytes()).to_string())
|
.map(|t| String::from_utf8_lossy(&t.get(1).unwrap().as_bytes()).to_string())
|
||||||
|
@ -68,23 +68,22 @@ fn recase(title: &str) -> String {
|
||||||
RE_PASS3.replace_all(&pass, " ").trim().to_string()
|
RE_PASS3.replace_all(&pass, " ").trim().to_string()
|
||||||
}
|
}
|
||||||
|
|
||||||
fn build_page_titles(references: &[String]) -> Vec<String> {
|
fn build_page_titles(references: &Vec<String>) -> Vec<String> {
|
||||||
references
|
references
|
||||||
.iter()
|
.iter()
|
||||||
.map(|s| match s.chars().next() {
|
.map(|s| {
|
||||||
Some('#') => recase(s),
|
let c = s.chars().nth(0);
|
||||||
Some('[') => s.strip_prefix("[[").unwrap().strip_suffix("]]").unwrap().to_string(),
|
match c {
|
||||||
Some(_) => s.clone(),
|
Some('#') => recase(s),
|
||||||
_ => "".to_string(),
|
Some('[') => s.strip_prefix("[[").unwrap().strip_suffix("]]").unwrap().to_string(),
|
||||||
|
Some(_) => s.clone(),
|
||||||
|
_ => "".to_string(),
|
||||||
|
}
|
||||||
})
|
})
|
||||||
.filter(|s| s.is_empty())
|
.filter(|s| s.len() > 0)
|
||||||
.collect()
|
.collect()
|
||||||
}
|
}
|
||||||
|
|
||||||
pub(crate) fn build_references(content: &str) -> Vec<String> {
|
|
||||||
build_page_titles(&find_links(content))
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
#[cfg(test)]
|
||||||
mod tests {
|
mod tests {
|
||||||
use super::*;
|
use super::*;
|
||||||
|
@ -123,12 +122,4 @@ Right? [[
|
||||||
];
|
];
|
||||||
assert!(res.iter().eq(expected.iter()), "{:?}", res);
|
assert!(res.iter().eq(expected.iter()), "{:?}", res);
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn doesnt_crash_on_empty() {
|
|
||||||
let sample = "";
|
|
||||||
let res = build_page_titles(&find_links(sample));
|
|
||||||
let expected: Vec<String> = vec![];
|
|
||||||
assert!(res.iter().eq(expected.iter()), "{:?}", res);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,105 @@
|
||||||
|
use chrono::{DateTime, Utc};
|
||||||
|
use derive_builder::Builder;
|
||||||
|
use serde::{Deserialize, Serialize};
|
||||||
|
use sqlx::{self, FromRow};
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
||||||
|
pub struct RawPage {
|
||||||
|
pub id: i64,
|
||||||
|
pub slug: String,
|
||||||
|
pub title: String,
|
||||||
|
pub note_id: i64,
|
||||||
|
pub creation_date: DateTime<Utc>,
|
||||||
|
pub updated_date: DateTime<Utc>,
|
||||||
|
pub lastview_date: DateTime<Utc>,
|
||||||
|
pub deleted_date: Option<DateTime<Utc>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
||||||
|
pub struct RawNote {
|
||||||
|
pub id: i64,
|
||||||
|
pub uuid: String,
|
||||||
|
pub parent_id: i64,
|
||||||
|
pub parent_uuid: String,
|
||||||
|
pub content: String,
|
||||||
|
pub position: i64,
|
||||||
|
pub notetype: String,
|
||||||
|
pub creation_date: DateTime<Utc>,
|
||||||
|
pub updated_date: DateTime<Utc>,
|
||||||
|
pub lastview_date: DateTime<Utc>,
|
||||||
|
pub deleted_date: Option<DateTime<Utc>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize, Debug, Builder)]
|
||||||
|
pub struct NewPage {
|
||||||
|
pub slug: String,
|
||||||
|
pub title: String,
|
||||||
|
pub note_id: i64,
|
||||||
|
#[builder(default = r#"chrono::Utc::now()"#)]
|
||||||
|
pub creation_date: DateTime<Utc>,
|
||||||
|
#[builder(default = r#"chrono::Utc::now()"#)]
|
||||||
|
pub updated_date: DateTime<Utc>,
|
||||||
|
#[builder(default = r#"chrono::Utc::now()"#)]
|
||||||
|
pub lastview_date: DateTime<Utc>,
|
||||||
|
#[builder(default = r#"None"#)]
|
||||||
|
pub deleted_date: Option<DateTime<Utc>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize, Debug, Builder)]
|
||||||
|
pub struct NewNote {
|
||||||
|
#[builder(default = r#""".to_string()"#)]
|
||||||
|
pub uuid: String,
|
||||||
|
pub content: String,
|
||||||
|
#[builder(default = r#""note".to_string()"#)]
|
||||||
|
pub notetype: String,
|
||||||
|
#[builder(default = r#"chrono::Utc::now()"#)]
|
||||||
|
pub creation_date: DateTime<Utc>,
|
||||||
|
#[builder(default = r#"chrono::Utc::now()"#)]
|
||||||
|
pub updated_date: DateTime<Utc>,
|
||||||
|
#[builder(default = r#"chrono::Utc::now()"#)]
|
||||||
|
pub lastview_date: DateTime<Utc>,
|
||||||
|
#[builder(default = r#"None"#)]
|
||||||
|
pub deleted_date: Option<DateTime<Utc>>,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
||||||
|
pub(crate) struct JustSlugs {
|
||||||
|
pub slug: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
||||||
|
pub(crate) struct JustTitles {
|
||||||
|
title: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
||||||
|
pub(crate) struct JustId {
|
||||||
|
pub id: i64,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
||||||
|
pub(crate) struct NoteRelationship {
|
||||||
|
pub parent_id: i64,
|
||||||
|
pub note_id: i64,
|
||||||
|
pub position: i64,
|
||||||
|
pub nature: String,
|
||||||
|
}
|
||||||
|
|
||||||
|
#[cfg(test)]
|
||||||
|
mod tests {
|
||||||
|
use super::*;
|
||||||
|
|
||||||
|
#[test]
|
||||||
|
fn can_build_new_note() {
|
||||||
|
let now = chrono::Utc::now();
|
||||||
|
let newnote = NewNoteBuilder::default()
|
||||||
|
.uuid("foo".to_string())
|
||||||
|
.content("bar".to_string())
|
||||||
|
.build()
|
||||||
|
.unwrap();
|
||||||
|
assert!((newnote.creation_date - now).num_minutes() < 1);
|
||||||
|
assert!((newnote.updated_date - now).num_minutes() < 1);
|
||||||
|
assert!((newnote.lastview_date - now).num_minutes() < 1);
|
||||||
|
assert!(newnote.deleted_date.is_none());
|
||||||
|
}
|
||||||
|
}
|
|
@ -1,8 +1,8 @@
|
||||||
DROP TABLE IF EXISTS notes;
|
DROP TABLE IF EXISTS notes;
|
||||||
DROP TABLE IF EXISTS note_relationships;
|
DROP TABLE IF EXISTS note_relationships;
|
||||||
DROP TABLE IF EXISTS pages;
|
DROP TABLE IF EXISTS pages;
|
||||||
DROP TABLE IF EXISTS page_relationships;
|
|
||||||
DROP TABLE IF EXISTS favorites;
|
DROP TABLE IF EXISTS favorites;
|
||||||
|
DROP TABLE IF EXISTS page_relationships;
|
||||||
|
|
||||||
CREATE TABLE notes (
|
CREATE TABLE notes (
|
||||||
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
id INTEGER PRIMARY KEY AUTOINCREMENT,
|
||||||
|
|
File diff suppressed because it is too large
Load Diff
|
@ -1,10 +1,9 @@
|
||||||
use chrono::{DateTime, Utc};
|
use chrono::{DateTime, Utc};
|
||||||
use derive_builder::Builder;
|
|
||||||
use serde::{Deserialize, Serialize};
|
use serde::{Deserialize, Serialize};
|
||||||
use sqlx::{self, FromRow};
|
use sqlx::{self, FromRow};
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
||||||
pub struct RawPage {
|
pub(crate) struct RawPage {
|
||||||
pub id: i64,
|
pub id: i64,
|
||||||
pub slug: String,
|
pub slug: String,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
|
@ -16,13 +15,10 @@ pub struct RawPage {
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
||||||
pub struct RawNote {
|
pub(crate) struct RawNote {
|
||||||
pub id: i64,
|
pub id: i64,
|
||||||
pub uuid: String,
|
pub uuid: String,
|
||||||
pub parent_id: i64,
|
|
||||||
pub parent_uuid: String,
|
|
||||||
pub content: String,
|
pub content: String,
|
||||||
pub position: i64,
|
|
||||||
pub notetype: String,
|
pub notetype: String,
|
||||||
pub creation_date: DateTime<Utc>,
|
pub creation_date: DateTime<Utc>,
|
||||||
pub updated_date: DateTime<Utc>,
|
pub updated_date: DateTime<Utc>,
|
||||||
|
@ -30,87 +26,58 @@ pub struct RawNote {
|
||||||
pub deleted_date: Option<DateTime<Utc>>,
|
pub deleted_date: Option<DateTime<Utc>>,
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize, Debug, Builder)]
|
// // A Resource is either content or a URL to content that the
|
||||||
pub struct NewPage {
|
// // user embeds in a note. TODO: I have no idea how to do this yet,
|
||||||
|
// // but I'll figure it out.
|
||||||
|
// #[derive(Clone, Serialize, Deserialize, Debug)]
|
||||||
|
// pub struct Resource {
|
||||||
|
// pub id: String,
|
||||||
|
// pub content: String,
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // A Breadcrumb is a component of a reference. Every element should
|
||||||
|
// // be clickable, although in practice what's going to happen is that
|
||||||
|
// // the user will be sent to the *page* with that note, then *scrolled*
|
||||||
|
// // to that note via anchor.
|
||||||
|
// #[derive(Clone, Debug)]
|
||||||
|
// pub struct Breadcrumb {
|
||||||
|
// pub note_id: String,
|
||||||
|
// pub summary: String,
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// // A Note is the heart of our system. It is a single object that has
|
||||||
|
// // a place in our system; it has a parent, but it also has embedded
|
||||||
|
// // references that allow it to navigate through a web of related
|
||||||
|
// // objects. It may have children. *AT THIS LAYER*, though, it is
|
||||||
|
// // returned as an array. It is up to the
|
||||||
|
// #[derive(Clone, Debug)]
|
||||||
|
// pub struct Note {
|
||||||
|
// pub id: String,
|
||||||
|
// pub parent_id: String,
|
||||||
|
// pub content: String,
|
||||||
|
// pub resources: Vec<Resource>,
|
||||||
|
// pub note_type: String, // Describes the relationship to the parent note.
|
||||||
|
// pub created: DateTime<Utc>,
|
||||||
|
// pub updated: DateTime<Utc>,
|
||||||
|
// pub viewed: DateTime<Utc>,
|
||||||
|
// pub deleted: Option<DateTime<Utc>>,
|
||||||
|
// }
|
||||||
|
//
|
||||||
|
// pub struct Reference {
|
||||||
|
// pub page_id: String,
|
||||||
|
// pub page_title: String,
|
||||||
|
// pub reference_summary_titles: Vec<Breadcrumbs>,
|
||||||
|
// pub reference_summary: String,
|
||||||
|
// }
|
||||||
|
|
||||||
|
pub struct Page {
|
||||||
pub slug: String,
|
pub slug: String,
|
||||||
pub title: String,
|
pub title: String,
|
||||||
pub note_id: i64,
|
// pub notes: Vec<Notes>, // The actual notes on this page.
|
||||||
#[builder(default = r#"chrono::Utc::now()"#)]
|
// pub references: Vec<Reference>, // All other notes that reference this page.
|
||||||
pub creation_date: DateTime<Utc>,
|
// pub unlinked_references: Vec<Reference>,
|
||||||
#[builder(default = r#"chrono::Utc::now()"#)]
|
pub created: DateTime<Utc>,
|
||||||
pub updated_date: DateTime<Utc>,
|
pub updated: DateTime<Utc>,
|
||||||
#[builder(default = r#"chrono::Utc::now()"#)]
|
pub viewed: DateTime<Utc>,
|
||||||
pub lastview_date: DateTime<Utc>,
|
pub deleted: Option<DateTime<Utc>>,
|
||||||
#[builder(default = r#"None"#)]
|
|
||||||
pub deleted_date: Option<DateTime<Utc>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize, Debug, Builder)]
|
|
||||||
pub struct NewNote {
|
|
||||||
#[builder(default = r#""".to_string()"#)]
|
|
||||||
pub uuid: String,
|
|
||||||
pub content: String,
|
|
||||||
#[builder(default = r#""note".to_string()"#)]
|
|
||||||
pub notetype: String,
|
|
||||||
#[builder(default = r#"chrono::Utc::now()"#)]
|
|
||||||
pub creation_date: DateTime<Utc>,
|
|
||||||
#[builder(default = r#"chrono::Utc::now()"#)]
|
|
||||||
pub updated_date: DateTime<Utc>,
|
|
||||||
#[builder(default = r#"chrono::Utc::now()"#)]
|
|
||||||
pub lastview_date: DateTime<Utc>,
|
|
||||||
#[builder(default = r#"None"#)]
|
|
||||||
pub deleted_date: Option<DateTime<Utc>>,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
|
||||||
pub(crate) struct JustSlugs {
|
|
||||||
pub slug: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
|
||||||
pub(crate) struct JustTitles {
|
|
||||||
title: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
|
||||||
pub(crate) struct JustId {
|
|
||||||
pub id: i64,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
|
||||||
pub(crate) struct PageTitles {
|
|
||||||
pub id: i64,
|
|
||||||
pub title: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
|
||||||
pub(crate) struct NoteRelationship {
|
|
||||||
pub parent_id: i64,
|
|
||||||
pub note_id: i64,
|
|
||||||
pub position: i64,
|
|
||||||
pub nature: String,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[derive(Clone, Serialize, Deserialize, Debug, FromRow)]
|
|
||||||
pub(crate) struct RowCount {
|
|
||||||
pub count: i64,
|
|
||||||
}
|
|
||||||
|
|
||||||
#[cfg(test)]
|
|
||||||
mod tests {
|
|
||||||
use super::*;
|
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn can_build_new_note() {
|
|
||||||
let now = chrono::Utc::now();
|
|
||||||
let newnote = NewNoteBuilder::default()
|
|
||||||
.uuid("foo".to_string())
|
|
||||||
.content("bar".to_string())
|
|
||||||
.build()
|
|
||||||
.unwrap();
|
|
||||||
assert!((newnote.creation_date - now).num_minutes() < 1);
|
|
||||||
assert!((newnote.updated_date - now).num_minutes() < 1);
|
|
||||||
assert!((newnote.lastview_date - now).num_minutes() < 1);
|
|
||||||
assert!(newnote.deleted_date.is_none());
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue