BlueGit

ict rusty-teams

Files

rusty-teams / crates / app-base / src / demo_library.rs

demo_library.rs

refactor(app): extract app-base crate
metis Daniel Däschle · pushed by metis · 6 weeks ago
16e522c2 History
demo_library.rs 6.1 KiB Download Raw
1use std::collections::HashMap;
2
3use chrono::{Duration, Utc};
4use teams_core::DriveEntry;
5
6const DEMO_SITE: &str = "https://demo.sharepoint.example/sites/demo";
7const AUTHORS: [&str; 4] = [
8 "Mara Lindqvist",
9 "Jonas Ortega",
10 "Priya Nair",
11 "Tobias Klein",
12];
13const ROOT_FILES: [(&str, u64); 5] = [
14 ("Roadmap-Q4.pptx", 3_412_000),
15 ("Release-Checklist-Q4.xlsx", 48_213),
16 ("Rollout-Plan.pdf", 2_306_867),
17 ("Postmortem-Draft.docx", 91_400),
18 ("Screenshots.zip", 12_880_000),
19];
20const SPEC_FILES: [(&str, u64); 2] = [("API-Design.pdf", 812_000), ("Data-model.xlsx", 64_300)];
21const NOTE_FILES: [(&str, u64); 2] = [
22 ("Weekly-sync-notes.docx", 38_900),
23 ("Retro-2026-10.pdf", 205_000),
24];
25const YEAR_FILES: [(&str, u64); 1] = [("Settings-spec-v3.docx", 120_500)];
26
27#[derive(Default)]
28pub struct DemoLibrary {
29 children: HashMap<String, Vec<DriveEntry>>,
30 next_id: u64,
31}
32
33impl DemoLibrary {
34 pub fn root(&mut self, channel_id: &str, channel_name: &str) -> DriveEntry {
35 let root_id = format!("{channel_id}-root");
36 if !self.children.contains_key(&root_id) {
37 self.fill(&root_id);
38 }
39 self.entry(&root_id, channel_name, Some(0), 0)
40 }
41
42 pub fn children(&self, folder_id: &str) -> Vec<DriveEntry> {
43 self.children
44 .get(folder_id)
45 .map(|entries| {
46 entries
47 .iter()
48 .map(|entry| DriveEntry {
49 child_count: entry.child_count.map(|_| {
50 self.children
51 .get(&entry.id)
52 .map_or(0, |inner| inner.len() as u64)
53 }),
54 ..entry.clone()
55 })
56 .collect()
57 })
58 .unwrap_or_default()
59 }
60
61 pub fn create_folder(&mut self, parent_id: &str, name: &str) -> DriveEntry {
62 let mut name = name.to_owned();
63 let taken = |library: &Self, name: &str| {
64 library.children.get(parent_id).is_some_and(|entries| {
65 entries
66 .iter()
67 .any(|entry| entry.name.eq_ignore_ascii_case(name))
68 })
69 };
70 let base = name.clone();
71 let mut number = 1;
72 while taken(self, &name) {
73 number += 1;
74 name = format!("{base} {number}");
75 }
76 let id = self.new_id(parent_id);
77 let entry = self.entry(&id, &name, Some(0), 0);
78 self.children
79 .entry(parent_id.to_owned())
80 .or_default()
81 .push(entry.clone());
82 self.children.insert(id, Vec::new());
83 entry
84 }
85
86 pub fn add_file(&mut self, parent_id: &str, name: &str, size: u64) -> DriveEntry {
87 let id = self.new_id(parent_id);
88 let entry = self.entry(&id, name, None, size);
89 self.children
90 .entry(parent_id.to_owned())
91 .or_default()
92 .push(entry.clone());
93 entry
94 }
95
96 fn new_id(&mut self, parent_id: &str) -> String {
97 self.next_id += 1;
98 format!("{parent_id}-{}", self.next_id)
99 }
100
101 fn entry(&self, id: &str, name: &str, child_count: Option<u64>, size: u64) -> DriveEntry {
102 let age = Duration::hours(3 + (id.len() as i64 * 37) % 400);
103 DriveEntry {
104 drive_id: "demo-drive".to_owned(),
105 id: id.to_owned(),
106 name: name.to_owned(),
107 web_url: format!("{DEMO_SITE}/{id}/{name}"),
108 size,
109 modified_at: Some(Utc::now() - age),
110 modified_by: Some(AUTHORS[id.len() % AUTHORS.len()].to_owned()),
111 child_count,
112 download_url: None,
113 }
114 }
115
116 fn fill(&mut self, root_id: &str) {
117 let specs = format!("{root_id}-specs");
118 let notes = format!("{root_id}-notes");
119 let year = format!("{specs}-2026");
120 let spec_folder = self.entry(&specs, "Specs", Some(0), 0);
121 let note_folder = self.entry(&notes, "Meeting notes", Some(0), 0);
122 let year_folder = self.entry(&year, "2026", Some(0), 0);
123 let files = |library: &Self, parent: &str, list: &[(&str, u64)]| -> Vec<DriveEntry> {
124 list.iter()
125 .map(|(name, size)| library.entry(&format!("{parent}-{name}"), name, None, *size))
126 .collect()
127 };
128 let mut root = vec![spec_folder, note_folder];
129 root.extend(files(self, root_id, &ROOT_FILES));
130 let mut spec_children = vec![year_folder];
131 spec_children.extend(files(self, &specs, &SPEC_FILES));
132 let note_children = files(self, &notes, &NOTE_FILES);
133 let year_children = files(self, &year, &YEAR_FILES);
134 self.children.insert(root_id.to_owned(), root);
135 self.children.insert(specs, spec_children);
136 self.children.insert(notes, note_children);
137 self.children.insert(year, year_children);
138 }
139}
140
141#[cfg(test)]
142mod tests {
143 use super::*;
144
145 #[test]
146 fn the_demo_library_has_folders_files_and_a_nested_folder() {
147 let mut library = DemoLibrary::default();
148 let root = library.root("demo-channel-1-1", "General");
149 let entries = library.children(&root.id);
150 assert_eq!(entries.iter().filter(|entry| entry.is_folder()).count(), 2);
151 assert_eq!(entries.iter().filter(|entry| !entry.is_folder()).count(), 5);
152 let specs = entries.iter().find(|entry| entry.name == "Specs").unwrap();
153 assert_eq!(specs.child_count, Some(3));
154 let inner = library.children(&specs.id);
155 assert!(
156 inner
157 .iter()
158 .any(|entry| entry.is_folder() && entry.child_count == Some(1))
159 );
160 }
161
162 #[test]
163 fn new_folders_get_a_free_name_and_uploads_show_up() {
164 let mut library = DemoLibrary::default();
165 let root = library.root("demo-channel-1-1", "General");
166 let first = library.create_folder(&root.id, "Specs");
167 assert_eq!(first.name, "Specs 2");
168 library.add_file(&first.id, "a.pdf", 10);
169 let listed = library.children(&root.id);
170 let created = listed.iter().find(|entry| entry.id == first.id).unwrap();
171 assert_eq!(created.child_count, Some(1));
172 }
173}