1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
//! A list that keeps the order of your values and their positions in sync.
pub use Item;
pub use Iter;
use HashMap;
use crate::;
/// A value of a [`LexicalList`] with its position and the ids of its neighbors.
///
/// The list returns it by reference from its operations (`push`, `move_to`, `find`, `iter`...): read the
/// value to save from [`value`](Self::value), and walk the list with [`previous`](Self::previous) and
/// [`next`](Self::next).
///
/// ```
/// # use staquia::{Item, LexicalList, Position, Staquia};
/// # #[derive(Debug)]
/// # struct Task { id: u32, done: bool, position: String }
/// # impl Item for Task {
/// # type Id = u32;
/// # fn id(&self) -> &u32 { &self.id }
/// # fn position(&self) -> &str { &self.position }
/// # fn set_position(&mut self, position: &Position) { self.position = position.to_string(); }
/// # }
/// # let task = |id| Task { id, done: false, position: String::new() };
/// # let stored = |id, position: &str| Task { id, done: false, position: position.to_string() };
/// let mut list = LexicalList::new(Staquia::create('!', 'a', 'z', 5, 15, 0.15)?);
/// list.push(task(1))?;
/// let item = list.push(task(2))?;
///
/// assert_eq!(item.id(), &2);
/// assert_eq!(item.position().to_string(), "mmmmn!");
/// assert_eq!(item.previous(), Some(&1));
/// assert_eq!(item.next(), None);
/// # Ok::<(), staquia::Error<u32>>(())
/// ```
/// The result of the operations that change many positions of a [`LexicalList`] at once
/// ([`set`](LexicalList::set), [`set_untrusted`](LexicalList::set_untrusted)): the affected items (the rows
/// to save), or the error with the values given back in a [`Rejected`].
///
/// ```
/// # use staquia::{Item, LexicalList, Position, Staquia};
/// # #[derive(Debug)]
/// # struct Task { id: u32, done: bool, position: String }
/// # impl Item for Task {
/// # type Id = u32;
/// # fn id(&self) -> &u32 { &self.id }
/// # fn position(&self) -> &str { &self.position }
/// # fn set_position(&mut self, position: &Position) { self.position = position.to_string(); }
/// # }
/// # let task = |id| Task { id, done: false, position: String::new() };
/// # let stored = |id, position: &str| Task { id, done: false, position: position.to_string() };
/// let mut list = LexicalList::new(Staquia::create('!', 'a', 'z', 5, 15, 0.15)?);
/// let rows = vec![stored(1, "mmmmm!"), stored(2, "")];
///
/// let affected = list.set_untrusted(rows)?;
/// assert_eq!(affected.len(), 1); // only task 2 got a new position
/// # Ok::<(), staquia::Error<u32>>(())
/// ```
pub type Replaced<'a, T> = ;
/// A list that keeps the order of your values and their positions in sync, as a doubly linked list of ids.
///
/// Any value that implements [`Item`] can be stored. Every operation writes the new position into the
/// changed values and tells you which items changed: in the usual case only one, so only one row needs to
/// be saved. Nothing panics: the operations return a [`Result`], and when one fails neither the list nor
/// your values are changed.
///
/// # Example
///
/// ```
/// use staquia::{Item, LexicalList, Position, Staquia};
///
/// struct Task {
/// id: u32,
/// position: String,
/// }
///
/// impl Item for Task {
/// type Id = u32;
/// fn id(&self) -> &u32 { &self.id }
/// fn position(&self) -> &str { &self.position }
/// fn set_position(&mut self, position: &Position) { self.position = position.to_string(); }
/// }
///
/// let task = |id| Task { id, position: String::new() };
/// let mut list = LexicalList::new(Staquia::create('!', 'a', 'z', 5, 15, 0.15)?);
///
/// list.push(task(1))?; // "mmmmm!"
/// list.push(task(2))?; // "mmmmn!"
/// list.push(task(3))?; // "mmmmo!"
///
/// // Moves the task 3 between 1 and 2: only its position changes
/// let moved = list.move_to(&3, Some(&1), Some(&2))?;
/// assert_eq!(moved.value().position, "mmmmmn");
///
/// let order: Vec<u32> = list.iter().map(|item| item.value().id).collect();
/// assert_eq!(order, [1, 3, 2]);
/// # Ok::<(), staquia::Error<u32>>(())
/// ```