mdvault_core/domain/
context.rs1use std::collections::HashMap;
6use std::path::PathBuf;
7use std::sync::Arc;
8
9use chrono::NaiveDate;
10
11use crate::config::types::ResolvedConfig;
12use crate::templates::repository::LoadedTemplate;
13use crate::types::{TypeDefinition, TypeRegistry};
14
15#[derive(Debug, Clone, Default)]
18pub struct CoreMetadata {
19 pub note_type: Option<String>,
20 pub title: Option<String>,
21 pub project_id: Option<String>,
22 pub task_id: Option<String>,
23 pub meeting_id: Option<String>,
24 pub task_counter: Option<u32>,
25 pub project: Option<String>, pub date: Option<String>, pub week: Option<String>, }
29
30impl CoreMetadata {
31 pub fn to_yaml_map(&self) -> HashMap<String, serde_yaml::Value> {
33 let mut map = HashMap::new();
34 if let Some(ref t) = self.note_type {
35 map.insert("type".into(), serde_yaml::Value::String(t.clone()));
36 }
37 if let Some(ref t) = self.title {
38 map.insert("title".into(), serde_yaml::Value::String(t.clone()));
39 }
40 if let Some(ref id) = self.project_id {
41 map.insert("project-id".into(), serde_yaml::Value::String(id.clone()));
42 }
43 if let Some(ref id) = self.task_id {
44 map.insert("task-id".into(), serde_yaml::Value::String(id.clone()));
45 }
46 if let Some(ref id) = self.meeting_id {
47 map.insert("meeting-id".into(), serde_yaml::Value::String(id.clone()));
48 }
49 if let Some(counter) = self.task_counter {
50 map.insert("task_counter".into(), serde_yaml::Value::Number(counter.into()));
51 }
52 if let Some(ref p) = self.project {
53 map.insert("project".into(), serde_yaml::Value::String(p.clone()));
54 }
55 if let Some(ref d) = self.date {
56 map.insert("date".into(), serde_yaml::Value::String(d.clone()));
57 }
58 if let Some(ref w) = self.week {
59 map.insert("week".into(), serde_yaml::Value::String(w.clone()));
60 }
61 map
62 }
63}
64
65pub struct CreationContext<'a> {
67 pub title: String,
69 pub type_name: String,
70
71 pub config: &'a ResolvedConfig,
73 pub typedef: Option<Arc<TypeDefinition>>,
74 pub registry: &'a TypeRegistry,
75
76 pub vars: HashMap<String, String>,
78 pub core_metadata: CoreMetadata,
79
80 pub output_path: Option<PathBuf>,
82
83 pub template: Option<LoadedTemplate>,
85
86 pub batch_mode: bool,
88
89 pub reference_date: Option<NaiveDate>,
91}
92
93impl<'a> CreationContext<'a> {
94 pub fn new(
96 type_name: &str,
97 title: &str,
98 config: &'a ResolvedConfig,
99 registry: &'a TypeRegistry,
100 ) -> Self {
101 let typedef = registry.get(type_name);
102
103 let core_metadata = CoreMetadata {
104 note_type: Some(type_name.to_string()),
105 title: Some(title.to_string()),
106 ..Default::default()
107 };
108
109 let vars = HashMap::from([
110 ("title".to_string(), title.to_string()),
111 ("type".to_string(), type_name.to_string()),
112 ]);
113
114 Self {
115 title: title.to_string(),
116 type_name: type_name.to_string(),
117 config,
118 typedef,
119 registry,
120 vars,
121 core_metadata,
122 output_path: None,
123 template: None,
124 batch_mode: false,
125 reference_date: None,
126 }
127 }
128
129 pub fn with_vars(mut self, cli_vars: HashMap<String, String>) -> Self {
131 self.vars.extend(cli_vars);
132 self
133 }
134
135 pub fn with_template(mut self, template: LoadedTemplate) -> Self {
137 self.template = Some(template);
138 self
139 }
140
141 pub fn with_batch_mode(mut self, batch: bool) -> Self {
143 self.batch_mode = batch;
144 self
145 }
146
147 pub fn get_var(&self, key: &str) -> Option<&str> {
149 self.vars.get(key).map(|s| s.as_str())
150 }
151
152 pub fn set_var(&mut self, key: impl Into<String>, value: impl Into<String>) {
154 self.vars.insert(key.into(), value.into());
155 }
156
157 pub fn to_prompt_context(&self) -> PromptContext<'_> {
159 PromptContext {
160 config: self.config,
161 type_name: &self.type_name,
162 title: &self.title,
163 provided_vars: &self.vars,
164 batch_mode: self.batch_mode,
165 }
166 }
167}
168
169pub struct PromptContext<'a> {
171 pub config: &'a ResolvedConfig,
172 pub type_name: &'a str,
173 pub title: &'a str,
174 pub provided_vars: &'a HashMap<String, String>,
175 pub batch_mode: bool,
176}
177
178#[derive(Debug, Clone)]
180pub struct FieldPrompt {
181 pub field_name: String,
182 pub prompt_text: String,
183 pub prompt_type: PromptType,
184 pub required: bool,
185 pub default_value: Option<String>,
186}
187
188#[derive(Debug, Clone)]
190pub enum PromptType {
191 Text,
193 Multiline,
195 Select(Vec<String>),
197 ProjectSelector,
199}