existence 0.4.0

CLI for the Existence ontology framework
Documentation
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
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
use crate::config::Config;
use std::path::Path;

/// Create a new ontology node from the SPEC.md template.
///
/// Generates `src/<term>.md` with pre-filled sections.
/// Optionally adds the term to a ring in `existence.toml` and opens `$EDITOR`.
pub fn run(
    ontology_dir: &Path,
    term: &str,
    ring: Option<u32>,
    no_edit: bool,
    description: Option<&str>,
) -> Result<(), String> {
    let src_dir = ontology_dir.join("src");
    if !src_dir.is_dir() {
        std::fs::create_dir_all(&src_dir)
            .map_err(|e| format!("Failed to create src directory: {e}"))?;
    }

    let file_path = src_dir.join(format!("{term}.md"));
    if file_path.exists() {
        return Err(format!(
            "Term '{}' already exists at {}",
            term,
            file_path.display()
        ));
    }

    let title = titlecase(term);
    let ontology_text = description.unwrap_or("*Definition to be documented.*");

    let content = format!(
        "# {title}\n\
         \n\
         ## [Ontology](./ontology.md)\n\
         \n\
         {ontology_text}\n\
         \n\
         ## [Axiology](./axiology.md)\n\
         \n\
         *Value and significance to be documented.*\n\
         \n\
         ## [Epistemology](./epistemology.md)\n\
         \n\
         *How this concept is known and validated to be documented.*\n"
    );

    std::fs::write(&file_path, &content)
        .map_err(|e| format!("Failed to write {}: {e}", file_path.display()))?;

    // Optionally add to ring in existence.toml
    if let Some(ring_level) = ring {
        add_term_to_ring(ontology_dir, term, ring_level)?;
    }

    println!("Created {}", file_path.display());

    // Open in $EDITOR if set and --no-edit not specified
    if !no_edit
        && let Ok(editor) = std::env::var("EDITOR")
    {
        let status = std::process::Command::new(&editor)
            .arg(&file_path)
            .status()
            .map_err(|e| format!("Failed to launch editor '{editor}': {e}"))?;
        if !status.success() {
            return Err(format!("Editor '{editor}' exited with non-zero status"));
        }
    }

    Ok(())
}

/// Capitalize the first letter of each word, replacing hyphens with spaces.
fn titlecase(term: &str) -> String {
    term.split('-')
        .map(|word| {
            let mut chars = word.chars();
            match chars.next() {
                None => String::new(),
                Some(c) => {
                    let upper: String = c.to_uppercase().collect();
                    upper + &chars.as_str().to_lowercase()
                }
            }
        })
        .collect::<Vec<_>>()
        .join(" ")
}

/// Add a term to the specified ring in existence.toml.
///
/// Reads the TOML, modifies the ring's terms array, and writes back.
/// Uses string manipulation to preserve formatting since `toml` crate
/// serialization may reorder keys.
fn add_term_to_ring(ontology_dir: &Path, term: &str, ring_level: u32) -> Result<(), String> {
    let config_path = ontology_dir.join("existence.toml");
    if !config_path.exists() {
        return Err(format!(
            "existence.toml not found at {}. Cannot add term to ring.",
            config_path.display()
        ));
    }

    // Validate the ring exists
    let config = Config::load(&config_path)?;
    if config.get_ring(ring_level).is_none() {
        return Err(format!("Ring {ring_level} not defined in existence.toml"));
    }

    // Check if term is already in the ring
    if let Some(r) = config.get_ring(ring_level)
        && r.terms.contains(&term.to_string())
    {
        // Already present, nothing to do
        return Ok(());
    }

    // Read raw content and insert the term into the ring's terms array
    let raw = std::fs::read_to_string(&config_path)
        .map_err(|e| format!("Failed to read {}: {e}", config_path.display()))?;

    let updated = insert_term_in_toml(&raw, ring_level, term)?;

    std::fs::write(&config_path, &updated)
        .map_err(|e| format!("Failed to write {}: {e}", config_path.display()))?;

    println!("Added '{term}' to ring {ring_level} in existence.toml");
    Ok(())
}

/// Insert a term into a ring's terms array in raw TOML text.
///
/// Finds the `terms = [...]` line under `[rings.N]` and appends the new term.
fn insert_term_in_toml(raw: &str, ring_level: u32, term: &str) -> Result<String, String> {
    let ring_header = format!("[rings.{}]", ring_level);
    let lines: Vec<&str> = raw.lines().collect();
    let mut result = Vec::new();
    let mut in_target_ring = false;
    let mut term_inserted = false;

    let mut i = 0;
    while i < lines.len() {
        let trimmed = lines[i].trim();

        // Detect ring section headers
        if trimmed.starts_with('[') {
            in_target_ring = trimmed == ring_header;
        }

        if in_target_ring && !term_inserted && trimmed.starts_with("terms") {
            // Handle multi-line terms array: collect until closing ]
            if trimmed.contains(']') {
                // Single-line terms array: terms = ["a", "b"]
                let closing = lines[i].rfind(']').unwrap();
                let before_bracket = &lines[i][..closing];
                let after_bracket = &lines[i][closing..];
                // Check if the array is empty
                if before_bracket.trim().ends_with('[') {
                    result.push(format!("{}\"{}\"{}",
                        before_bracket, term, after_bracket));
                } else {
                    result.push(format!("{}, \"{}\"{}",
                        before_bracket, term, after_bracket));
                }
                term_inserted = true;
            } else {
                // Multi-line: push lines until we find the closing ]
                result.push(lines[i].to_string());
                i += 1;
                while i < lines.len() {
                    let line_trimmed = lines[i].trim();
                    if line_trimmed.contains(']') {
                        // Insert before the closing bracket
                        let indent = "  ";
                        result.push(format!("{indent}\"{term}\","));
                        result.push(lines[i].to_string());
                        term_inserted = true;
                        break;
                    }
                    result.push(lines[i].to_string());
                    i += 1;
                }
            }
        } else {
            result.push(lines[i].to_string());
        }
        i += 1;
    }

    if !term_inserted {
        return Err(format!(
            "Could not find terms array for ring {ring_level} in existence.toml"
        ));
    }

    // Preserve trailing newline if original had one
    let mut output = result.join("\n");
    if raw.ends_with('\n') {
        output.push('\n');
    }
    Ok(output)
}

#[cfg(test)]
mod tests {
    use super::*;
    use std::fs;

    fn setup_test_ontology(tmp: &std::path::Path) {
        let src = tmp.join("src");
        fs::create_dir_all(&src).unwrap();

        fs::write(
            tmp.join("existence.toml"),
            r#"[meta]
name = "test"
description = "test ontology"

[rings.0]
name = "kernel"
description = "core"
terms = ["existence", "entity"]

[rings.1]
name = "software"
description = "bridge"
terms = ["state"]
"#,
        )
        .unwrap();
    }

    #[test]
    fn test_titlecase_simple() {
        assert_eq!(titlecase("existence"), "Existence");
    }

    #[test]
    fn test_titlecase_hyphenated() {
        assert_eq!(titlecase("domain-model"), "Domain Model");
    }

    #[test]
    fn test_titlecase_multiple_hyphens() {
        assert_eq!(titlecase("my-long-term"), "My Long Term");
    }

    #[test]
    fn test_create_new_term() {
        let tmp = tempfile::tempdir().unwrap();
        setup_test_ontology(tmp.path());

        let result = run(tmp.path(), "focus", None, true, None);
        assert!(result.is_ok(), "Expected Ok, got: {result:?}");

        let file = tmp.path().join("src/focus.md");
        assert!(file.exists());

        let content = fs::read_to_string(&file).unwrap();
        assert!(content.starts_with("# Focus\n"));
        assert!(content.contains("## [Ontology](./ontology.md)"));
        assert!(content.contains("## [Axiology](./axiology.md)"));
        assert!(content.contains("## [Epistemology](./epistemology.md)"));
        assert!(content.contains("*Definition to be documented.*"));
    }

    #[test]
    fn test_create_with_description() {
        let tmp = tempfile::tempdir().unwrap();
        setup_test_ontology(tmp.path());

        let result = run(
            tmp.path(),
            "pattern",
            None,
            true,
            Some("Elements that repeat in a predictable manner."),
        );
        assert!(result.is_ok());

        let content = fs::read_to_string(tmp.path().join("src/pattern.md")).unwrap();
        assert!(content.contains("Elements that repeat in a predictable manner."));
        assert!(!content.contains("*Definition to be documented.*"));
    }

    #[test]
    fn test_create_hyphenated_term() {
        let tmp = tempfile::tempdir().unwrap();
        setup_test_ontology(tmp.path());

        let result = run(tmp.path(), "domain-model", None, true, None);
        assert!(result.is_ok());

        let content = fs::read_to_string(tmp.path().join("src/domain-model.md")).unwrap();
        assert!(content.starts_with("# Domain Model\n"));
    }

    #[test]
    fn test_error_if_exists() {
        let tmp = tempfile::tempdir().unwrap();
        setup_test_ontology(tmp.path());

        // Create the term first
        run(tmp.path(), "focus", None, true, None).unwrap();

        // Second attempt should fail
        let result = run(tmp.path(), "focus", None, true, None);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(err.contains("already exists"), "Unexpected error: {err}");
    }

    #[test]
    fn test_create_with_ring() {
        let tmp = tempfile::tempdir().unwrap();
        setup_test_ontology(tmp.path());

        let result = run(tmp.path(), "focus", Some(0), true, None);
        assert!(result.is_ok(), "Expected Ok, got: {result:?}");

        // Verify the term was added to existence.toml
        let config = Config::load(&tmp.path().join("existence.toml")).unwrap();
        let ring0 = config.get_ring(0).unwrap();
        assert!(
            ring0.terms.contains(&"focus".to_string()),
            "Expected 'focus' in ring 0 terms: {:?}",
            ring0.terms
        );
    }

    #[test]
    fn test_create_with_invalid_ring() {
        let tmp = tempfile::tempdir().unwrap();
        setup_test_ontology(tmp.path());

        let result = run(tmp.path(), "focus", Some(5), true, None);
        assert!(result.is_err());
        let err = result.unwrap_err();
        assert!(
            err.contains("Ring 5 not defined"),
            "Unexpected error: {err}"
        );
    }

    #[test]
    fn test_insert_term_in_toml_single_line() {
        let toml = r#"[meta]
name = "test"
description = "test"

[rings.0]
name = "kernel"
description = "core"
terms = ["existence", "entity"]
"#;
        let updated = insert_term_in_toml(toml, 0, "focus").unwrap();
        assert!(
            updated.contains(r#"terms = ["existence", "entity", "focus"]"#),
            "Got: {updated}"
        );
    }

    #[test]
    fn test_insert_term_in_toml_multi_line() {
        let toml = r#"[meta]
name = "test"
description = "test"

[rings.0]
name = "kernel"
description = "core"
terms = [
  "existence",
  "entity",
]
"#;
        let updated = insert_term_in_toml(toml, 0, "focus").unwrap();
        assert!(updated.contains("\"focus\","), "Got: {updated}");
        // Verify it parses correctly
        let config: Config = toml::from_str(&updated).unwrap();
        let ring0 = config.get_ring(0).unwrap();
        assert!(ring0.terms.contains(&"focus".to_string()));
    }

    #[test]
    fn test_insert_term_in_toml_empty_array() {
        let toml = r#"[meta]
name = "test"
description = "test"

[rings.0]
name = "kernel"
description = "core"
terms = []
"#;
        let updated = insert_term_in_toml(toml, 0, "focus").unwrap();
        assert!(
            updated.contains(r#"terms = ["focus"]"#),
            "Got: {updated}"
        );
    }

    #[test]
    fn test_creates_src_dir_if_missing() {
        let tmp = tempfile::tempdir().unwrap();
        // Only create existence.toml, no src dir
        fs::write(
            tmp.path().join("existence.toml"),
            "[meta]\nname = \"t\"\ndescription = \"t\"\n\n[rings.0]\nname = \"k\"\ndescription = \"c\"\nterms = []\n",
        )
        .unwrap();

        let result = run(tmp.path(), "focus", None, true, None);
        assert!(result.is_ok());
        assert!(tmp.path().join("src/focus.md").exists());
    }
}