Struct HistoryTree

Source
pub struct HistoryTree {
    pub records: Vec<Record>,
    pub cursor: Option<usize>,
}
Expand description

Stores information about history tree relations.

Fields§

§records: Vec<Record>

Stores records.

§cursor: Option<usize>

History cursor. Points to an index of records where all previous changes are active, and those after are inactive. When set to None, it is assumed to point to the latest version.

Implementations§

Source§

impl HistoryTree

Source

pub fn new() -> HistoryTree

Creates a new history tree.

Examples found in repository?
examples/app.rs (line 41)
39    pub fn new() -> App {
40        App {
41            ht: HistoryTree::new(),
42            // Add dummy root to align indices.
43            text: vec!["root".into()],
44        }
45    }
More examples
Hide additional examples
examples/index.rs (line 10)
9fn main() {
10    let mut ht = HistoryTree::new();
11    let root = ht.root();
12    let _assets = ht.add(root);
13    let notes = ht.add(root);
14    let mut bar = ht.add(notes);
15    let _baz = ht.add(bar);
16    ht.print(root, 0);
17
18    ht.change(&mut bar);
19    ht.print(root, 0);
20
21    let src = ht.add(root);
22    let _file = ht.add(src);
23    let _foo = ht.add(src);
24    ht.print(root, 0);
25
26    ht.delete(bar);
27    ht.print(root, 0);
28
29    println!("--------- undo ----------");
30    for _ in 0..ht.records.len() - 1 {
31        ht.undo();
32        ht.print(root, 0);
33    }
34
35    println!("--------- redo ----------");
36    for _ in 0..ht.records.len() - 1 {
37        ht.redo();
38        ht.print(root, 0);
39    }
40}
Source

pub fn root(&self) -> usize

Gets the root.

Examples found in repository?
examples/app.rs (line 48)
48    pub fn root(&self) -> usize {self.ht.root()}
More examples
Hide additional examples
examples/index.rs (line 11)
9fn main() {
10    let mut ht = HistoryTree::new();
11    let root = ht.root();
12    let _assets = ht.add(root);
13    let notes = ht.add(root);
14    let mut bar = ht.add(notes);
15    let _baz = ht.add(bar);
16    ht.print(root, 0);
17
18    ht.change(&mut bar);
19    ht.print(root, 0);
20
21    let src = ht.add(root);
22    let _file = ht.add(src);
23    let _foo = ht.add(src);
24    ht.print(root, 0);
25
26    ht.delete(bar);
27    ht.print(root, 0);
28
29    println!("--------- undo ----------");
30    for _ in 0..ht.records.len() - 1 {
31        ht.undo();
32        ht.print(root, 0);
33    }
34
35    println!("--------- redo ----------");
36    for _ in 0..ht.records.len() - 1 {
37        ht.redo();
38        ht.print(root, 0);
39    }
40}
Source

pub fn cursor(&self) -> usize

Gets the cursor.

Examples found in repository?
examples/app.rs (line 52)
51    pub fn add(&mut self, text: String, parent: usize) -> usize {
52        let cursor = self.ht.cursor();
53        self.text.truncate(cursor + 1);
54
55        self.text.push(text);
56        self.ht.add(parent)
57    }
58
59    /// Changes a node.
60    pub fn change(&mut self, text: String, node: &mut usize) {
61        let cursor = self.ht.cursor();
62        self.text.truncate(cursor + 1);
63
64        self.text.push(text);
65        self.ht.change(node);
66    }
67
68    /// Deletes a node.
69    pub fn delete(&mut self, node: usize) {
70        let cursor = self.ht.cursor();
71        self.text.truncate(cursor + 1);
72
73        self.ht.delete(node);
74    }
Source

pub fn add(&mut self, parent: usize) -> usize

Add new node.

Examples found in repository?
examples/app.rs (line 56)
51    pub fn add(&mut self, text: String, parent: usize) -> usize {
52        let cursor = self.ht.cursor();
53        self.text.truncate(cursor + 1);
54
55        self.text.push(text);
56        self.ht.add(parent)
57    }
More examples
Hide additional examples
examples/index.rs (line 12)
9fn main() {
10    let mut ht = HistoryTree::new();
11    let root = ht.root();
12    let _assets = ht.add(root);
13    let notes = ht.add(root);
14    let mut bar = ht.add(notes);
15    let _baz = ht.add(bar);
16    ht.print(root, 0);
17
18    ht.change(&mut bar);
19    ht.print(root, 0);
20
21    let src = ht.add(root);
22    let _file = ht.add(src);
23    let _foo = ht.add(src);
24    ht.print(root, 0);
25
26    ht.delete(bar);
27    ht.print(root, 0);
28
29    println!("--------- undo ----------");
30    for _ in 0..ht.records.len() - 1 {
31        ht.undo();
32        ht.print(root, 0);
33    }
34
35    println!("--------- redo ----------");
36    for _ in 0..ht.records.len() - 1 {
37        ht.redo();
38        ht.print(root, 0);
39    }
40}
Source

pub fn change(&mut self, node: &mut usize)

Change node.

Examples found in repository?
examples/app.rs (line 65)
60    pub fn change(&mut self, text: String, node: &mut usize) {
61        let cursor = self.ht.cursor();
62        self.text.truncate(cursor + 1);
63
64        self.text.push(text);
65        self.ht.change(node);
66    }
More examples
Hide additional examples
examples/index.rs (line 18)
9fn main() {
10    let mut ht = HistoryTree::new();
11    let root = ht.root();
12    let _assets = ht.add(root);
13    let notes = ht.add(root);
14    let mut bar = ht.add(notes);
15    let _baz = ht.add(bar);
16    ht.print(root, 0);
17
18    ht.change(&mut bar);
19    ht.print(root, 0);
20
21    let src = ht.add(root);
22    let _file = ht.add(src);
23    let _foo = ht.add(src);
24    ht.print(root, 0);
25
26    ht.delete(bar);
27    ht.print(root, 0);
28
29    println!("--------- undo ----------");
30    for _ in 0..ht.records.len() - 1 {
31        ht.undo();
32        ht.print(root, 0);
33    }
34
35    println!("--------- redo ----------");
36    for _ in 0..ht.records.len() - 1 {
37        ht.redo();
38        ht.print(root, 0);
39    }
40}
Source

pub fn delete(&mut self, node: usize)

Delete node.

Examples found in repository?
examples/app.rs (line 73)
69    pub fn delete(&mut self, node: usize) {
70        let cursor = self.ht.cursor();
71        self.text.truncate(cursor + 1);
72
73        self.ht.delete(node);
74    }
More examples
Hide additional examples
examples/index.rs (line 26)
9fn main() {
10    let mut ht = HistoryTree::new();
11    let root = ht.root();
12    let _assets = ht.add(root);
13    let notes = ht.add(root);
14    let mut bar = ht.add(notes);
15    let _baz = ht.add(bar);
16    ht.print(root, 0);
17
18    ht.change(&mut bar);
19    ht.print(root, 0);
20
21    let src = ht.add(root);
22    let _file = ht.add(src);
23    let _foo = ht.add(src);
24    ht.print(root, 0);
25
26    ht.delete(bar);
27    ht.print(root, 0);
28
29    println!("--------- undo ----------");
30    for _ in 0..ht.records.len() - 1 {
31        ht.undo();
32        ht.print(root, 0);
33    }
34
35    println!("--------- redo ----------");
36    for _ in 0..ht.records.len() - 1 {
37        ht.redo();
38        ht.print(root, 0);
39    }
40}
Source

pub fn children(&self, parent: usize) -> Vec<usize>

Gets the names of children.

Examples found in repository?
examples/app.rs (line 83)
77    pub fn print(&self, parent: usize, tabs: u32) {
78        if tabs > 0 {
79            for _ in 0..tabs - 1 {print!("  ")}
80            print!("|-");
81        }
82        println!("{}", self.text[parent]);
83        for &ch in &self.ht.children(parent) {
84            self.print(ch, tabs + 1);
85        }
86    }
87
88    /// Goes one step back in history.
89    pub fn undo(&mut self) {self.ht.undo()}
90
91    /// Goes one step forward in history.
92    pub fn redo(&mut self) {self.ht.redo()}
93
94    /// Gets children.
95    pub fn children(&self, parent: usize) -> Vec<usize> {
96        self.ht.children(parent)
97    }
Source

pub fn undo(&mut self)

Goes back one step in history.

Examples found in repository?
examples/app.rs (line 89)
89    pub fn undo(&mut self) {self.ht.undo()}
More examples
Hide additional examples
examples/index.rs (line 31)
9fn main() {
10    let mut ht = HistoryTree::new();
11    let root = ht.root();
12    let _assets = ht.add(root);
13    let notes = ht.add(root);
14    let mut bar = ht.add(notes);
15    let _baz = ht.add(bar);
16    ht.print(root, 0);
17
18    ht.change(&mut bar);
19    ht.print(root, 0);
20
21    let src = ht.add(root);
22    let _file = ht.add(src);
23    let _foo = ht.add(src);
24    ht.print(root, 0);
25
26    ht.delete(bar);
27    ht.print(root, 0);
28
29    println!("--------- undo ----------");
30    for _ in 0..ht.records.len() - 1 {
31        ht.undo();
32        ht.print(root, 0);
33    }
34
35    println!("--------- redo ----------");
36    for _ in 0..ht.records.len() - 1 {
37        ht.redo();
38        ht.print(root, 0);
39    }
40}
Source

pub fn redo(&mut self)

Goes forward one step in history.

Examples found in repository?
examples/app.rs (line 92)
92    pub fn redo(&mut self) {self.ht.redo()}
More examples
Hide additional examples
examples/index.rs (line 37)
9fn main() {
10    let mut ht = HistoryTree::new();
11    let root = ht.root();
12    let _assets = ht.add(root);
13    let notes = ht.add(root);
14    let mut bar = ht.add(notes);
15    let _baz = ht.add(bar);
16    ht.print(root, 0);
17
18    ht.change(&mut bar);
19    ht.print(root, 0);
20
21    let src = ht.add(root);
22    let _file = ht.add(src);
23    let _foo = ht.add(src);
24    ht.print(root, 0);
25
26    ht.delete(bar);
27    ht.print(root, 0);
28
29    println!("--------- undo ----------");
30    for _ in 0..ht.records.len() - 1 {
31        ht.undo();
32        ht.print(root, 0);
33    }
34
35    println!("--------- redo ----------");
36    for _ in 0..ht.records.len() - 1 {
37        ht.redo();
38        ht.print(root, 0);
39    }
40}
Source

pub fn print(&self, parent: usize, tabs: u32)

Prints relations to standard output. This is used for debugging.

Examples found in repository?
examples/index.rs (line 16)
9fn main() {
10    let mut ht = HistoryTree::new();
11    let root = ht.root();
12    let _assets = ht.add(root);
13    let notes = ht.add(root);
14    let mut bar = ht.add(notes);
15    let _baz = ht.add(bar);
16    ht.print(root, 0);
17
18    ht.change(&mut bar);
19    ht.print(root, 0);
20
21    let src = ht.add(root);
22    let _file = ht.add(src);
23    let _foo = ht.add(src);
24    ht.print(root, 0);
25
26    ht.delete(bar);
27    ht.print(root, 0);
28
29    println!("--------- undo ----------");
30    for _ in 0..ht.records.len() - 1 {
31        ht.undo();
32        ht.print(root, 0);
33    }
34
35    println!("--------- redo ----------");
36    for _ in 0..ht.records.len() - 1 {
37        ht.redo();
38        ht.print(root, 0);
39    }
40}

Trait Implementations§

Source§

impl Clone for HistoryTree

Source§

fn clone(&self) -> HistoryTree

Returns a copy of the value. Read more
1.0.0 · Source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
Source§

impl Debug for HistoryTree

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
Source§

impl PartialEq for HistoryTree

Source§

fn eq(&self, other: &HistoryTree) -> bool

Tests for self and other values to be equal, and is used by ==.
1.0.0 · Source§

fn ne(&self, other: &Rhs) -> bool

Tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
Source§

impl Eq for HistoryTree

Source§

impl StructuralPartialEq for HistoryTree

Auto Trait Implementations§

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> CloneToUninit for T
where T: Clone,

Source§

unsafe fn clone_to_uninit(&self, dest: *mut u8)

🔬This is a nightly-only experimental API. (clone_to_uninit)
Performs copy-assignment from self to dest. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToOwned for T
where T: Clone,

Source§

type Owned = T

The resulting type after obtaining ownership.
Source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
Source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = Infallible

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.