notesmachine/server/nm-trees/src/make_tree.rs

46 lines
1.6 KiB
Rust

use crate::structs::{Note, Page};
use nm_store::{RawNote, RawPage};
fn make_note_tree(rawnotes: &[RawNote], root: i64) -> Note {
let the_note = rawnotes.iter().find(|note| note.id == root).unwrap().clone();
// The special case of the root node must be filtered out here to
// prevent the first pass from smashing the stack in an infinite
// loop. The root node is identified by the type 'root' and
// having its `id` and `parent_id` be equal. Numeric comparisons
// are faster.
let mut children = rawnotes
.iter()
.filter(|note| note.parent_id == root && note.id != root)
.map(|note| make_note_tree(rawnotes, note.id))
.collect::<Vec<Note>>();
children.sort_unstable_by(|a, b| a.position.cmp(&b.position));
Note {
uuid: the_note.uuid,
parent_uuid: the_note.parent_uuid,
content: the_note.content,
notetype: the_note.notetype,
position: the_note.position,
creation_date: the_note.creation_date,
updated_date: the_note.updated_date,
lastview_date: the_note.updated_date,
deleted_date: the_note.deleted_date,
children: vec![],
}
}
pub(crate) fn make_tree(rawpage: &RawPage, rawnotes: &[RawNote]) -> Page {
let the_page = rawpage.clone();
Page {
slug: the_page.slug,
title: the_page.title,
creation_date: the_page.creation_date,
updated_date: the_page.updated_date,
lastview_date: the_page.updated_date,
deleted_date: the_page.deleted_date,
root_note: make_note_tree(rawnotes, rawpage.note_id),
}
}