intentional-core 0.1.3

Core model and release planning for intentional
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
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// ---
// relationships:
//   implements: intent-driven-polyglot-release
// ---

//! Format adapters that replace only a configured version value.

use crate::error::{Error, Result};
use std::collections::BTreeMap;
use std::ops::Range;
use std::path::Path;
use toml_edit::{value, DocumentMut, Item};

/// A format-preserving version-value adapter.
pub trait FormatAdapter {
    /// Read a string value selected by `pointer`.
    fn read(&self, file: &Path, pointer: &str) -> Result<String> {
        let text = std::fs::read_to_string(file).map_err(|error| Error::io(file, error))?;
        self.read_text(&text, pointer)
    }

    /// Produce updated text without touching the filesystem.
    fn edit_text(&self, text: &str, pointer: &str, value: &str) -> Result<String>;

    /// Read a selected string from in-memory text.
    fn read_text(&self, text: &str, pointer: &str) -> Result<String>;

    /// Write a selected value unless `dry_run` is enabled.
    fn write(&self, file: &Path, pointer: &str, new_value: &str, dry_run: bool) -> Result<()> {
        let text = std::fs::read_to_string(file).map_err(|error| Error::io(file, error))?;
        let edited = self.edit_text(&text, pointer, new_value)?;
        if !dry_run && edited != text {
            std::fs::write(file, edited).map_err(|error| Error::io(file, error))?;
        }
        Ok(())
    }
}

/// Targeted JSON string adapter.
#[derive(Debug, Clone, Copy, Default)]
pub struct JsonFormat;

/// Comment-preserving TOML adapter backed by `toml_edit`.
#[derive(Debug, Clone, Copy, Default)]
pub struct TomlFormat;

/// Targeted YAML scalar-line adapter.
#[derive(Debug, Clone, Copy, Default)]
pub struct YamlFormat;

impl FormatAdapter for JsonFormat {
    fn edit_text(&self, text: &str, pointer: &str, new_value: &str) -> Result<String> {
        let node = JsonParser::new(text).parse()?;
        let target = node.find(&json_pointer(pointer)?)?;
        let mut edited = text.to_owned();
        let replacement = serde_json::to_string(new_value)
            .map_err(|error| Error::Validation(format!("JSON value is invalid: {error}")))?;
        edited.replace_range(target.range.clone(), &replacement);
        Ok(edited)
    }

    fn read_text(&self, text: &str, pointer: &str) -> Result<String> {
        let node = JsonParser::new(text).parse()?;
        let target = node.find(&json_pointer(pointer)?)?;
        serde_json::from_str(&text[target.range.clone()]).map_err(|error| {
            Error::Validation(format!(
                "JSON pointer {pointer} does not select a string: {error}"
            ))
        })
    }
}

impl FormatAdapter for TomlFormat {
    fn edit_text(&self, text: &str, pointer: &str, new_value: &str) -> Result<String> {
        let mut document = text
            .parse::<DocumentMut>()
            .map_err(|error| Error::Validation(format!("invalid TOML: {error}")))?;
        let item = toml_item_mut(document.as_item_mut(), &key_pointer(pointer)?, pointer)?;
        let decor = item.as_value().map(|old| old.decor().clone());
        *item = value(new_value);
        if let (Some(decor), Some(new)) = (decor, item.as_value_mut()) {
            *new.decor_mut() = decor;
        }
        Ok(document.to_string())
    }

    fn read_text(&self, text: &str, pointer: &str) -> Result<String> {
        let document = text
            .parse::<DocumentMut>()
            .map_err(|error| Error::Validation(format!("invalid TOML: {error}")))?;
        let item = toml_item(document.as_item(), &key_pointer(pointer)?, pointer)?;
        item.as_str()
            .map(str::to_owned)
            .ok_or_else(|| Error::Validation(format!("TOML pointer {pointer} is not a string")))
    }
}

impl FormatAdapter for YamlFormat {
    fn edit_text(&self, text: &str, pointer: &str, new_value: &str) -> Result<String> {
        let keys = key_pointer(pointer)?;
        let line = find_yaml_line(text, &keys, pointer)?;
        let source = &text[line.clone()];
        let colon = source
            .find(':')
            .ok_or_else(|| Error::Validation(format!("invalid YAML line for {pointer}")))?;
        let after_colon = &source[colon + 1..];
        let value_offset = after_colon.len() - after_colon.trim_start().len();
        let value_start = colon + 1 + value_offset;
        let value_end = yaml_value_end(source, value_start);
        let old = source[value_start..value_end].trim_end();
        let replacement = quote_like_yaml(old, new_value);
        let mut new_line = String::new();
        new_line.push_str(&source[..value_start]);
        new_line.push_str(&replacement);
        new_line.push_str(&source[value_end..]);
        let mut edited = text.to_owned();
        edited.replace_range(line, &new_line);
        Ok(edited)
    }

    fn read_text(&self, text: &str, pointer: &str) -> Result<String> {
        let keys = key_pointer(pointer)?;
        let line = find_yaml_line(text, &keys, pointer)?;
        let source = &text[line];
        let colon = source
            .find(':')
            .ok_or_else(|| Error::Validation(format!("invalid YAML line for {pointer}")))?;
        let start = colon + 1 + source[colon + 1..].len() - source[colon + 1..].trim_start().len();
        let end = yaml_value_end(source, start);
        let raw = source[start..end].trim();
        Ok(unquote_yaml(raw).to_owned())
    }
}

#[derive(Debug)]
struct JsonNode {
    range: Range<usize>,
    kind: JsonKind,
}

#[derive(Debug)]
enum JsonKind {
    Object(BTreeMap<String, JsonNode>),
    Array(Vec<JsonNode>),
    Scalar,
}

impl JsonNode {
    fn find(&self, pointer: &[String]) -> Result<&Self> {
        let mut current = self;
        for segment in pointer {
            current = match &current.kind {
                JsonKind::Object(values) => values.get(segment),
                JsonKind::Array(values) => segment
                    .parse::<usize>()
                    .ok()
                    .and_then(|index| values.get(index)),
                JsonKind::Scalar => None,
            }
            .ok_or_else(|| {
                Error::Validation(format!("JSON pointer segment {segment:?} was not found"))
            })?;
        }
        Ok(current)
    }
}

struct JsonParser<'a> {
    text: &'a str,
    cursor: usize,
}

impl<'a> JsonParser<'a> {
    fn new(text: &'a str) -> Self {
        Self { text, cursor: 0 }
    }

    fn parse(mut self) -> Result<JsonNode> {
        let node = self.value()?;
        self.whitespace();
        if self.cursor != self.text.len() {
            return Err(Error::Validation("trailing content in JSON".to_owned()));
        }
        Ok(node)
    }

    fn value(&mut self) -> Result<JsonNode> {
        self.whitespace();
        let start = self.cursor;
        let byte = self.peek().ok_or_else(|| {
            Error::Validation("unexpected end of JSON while reading value".to_owned())
        })?;
        let kind = match byte {
            b'{' => self.object()?,
            b'[' => self.array()?,
            b'"' => {
                self.string()?;
                JsonKind::Scalar
            }
            _ => {
                while let Some(byte) = self.peek() {
                    if byte.is_ascii_whitespace() || b",]}".contains(&byte) {
                        break;
                    }
                    self.cursor += 1;
                }
                if self.cursor == start {
                    return Err(Error::Validation("invalid JSON scalar".to_owned()));
                }
                JsonKind::Scalar
            }
        };
        Ok(JsonNode {
            range: start..self.cursor,
            kind,
        })
    }

    fn object(&mut self) -> Result<JsonKind> {
        self.expect(b'{')?;
        self.whitespace();
        let mut values = BTreeMap::new();
        if self.take(b'}') {
            return Ok(JsonKind::Object(values));
        }
        loop {
            self.whitespace();
            let key_range = self.string()?;
            let key: String = serde_json::from_str(&self.text[key_range])
                .map_err(|error| Error::Validation(format!("invalid JSON object key: {error}")))?;
            self.whitespace();
            self.expect(b':')?;
            let value = self.value()?;
            values.insert(key, value);
            self.whitespace();
            if self.take(b'}') {
                break;
            }
            self.expect(b',')?;
        }
        Ok(JsonKind::Object(values))
    }

    fn array(&mut self) -> Result<JsonKind> {
        self.expect(b'[')?;
        self.whitespace();
        let mut values = Vec::new();
        if self.take(b']') {
            return Ok(JsonKind::Array(values));
        }
        loop {
            values.push(self.value()?);
            self.whitespace();
            if self.take(b']') {
                break;
            }
            self.expect(b',')?;
        }
        Ok(JsonKind::Array(values))
    }

    fn string(&mut self) -> Result<Range<usize>> {
        let start = self.cursor;
        self.expect(b'"')?;
        let mut escaped = false;
        while let Some(byte) = self.peek() {
            self.cursor += 1;
            if escaped {
                escaped = false;
            } else if byte == b'\\' {
                escaped = true;
            } else if byte == b'"' {
                return Ok(start..self.cursor);
            }
        }
        Err(Error::Validation("unterminated JSON string".to_owned()))
    }

    fn whitespace(&mut self) {
        while self.peek().is_some_and(|byte| byte.is_ascii_whitespace()) {
            self.cursor += 1;
        }
    }

    fn peek(&self) -> Option<u8> {
        self.text.as_bytes().get(self.cursor).copied()
    }

    fn take(&mut self, byte: u8) -> bool {
        if self.peek() == Some(byte) {
            self.cursor += 1;
            true
        } else {
            false
        }
    }

    fn expect(&mut self, byte: u8) -> Result<()> {
        if self.take(byte) {
            Ok(())
        } else {
            Err(Error::Validation(format!(
                "expected JSON byte {:?} at offset {}",
                byte as char, self.cursor
            )))
        }
    }
}

fn json_pointer(pointer: &str) -> Result<Vec<String>> {
    if pointer.is_empty() {
        return Ok(Vec::new());
    }
    let rest = pointer
        .strip_prefix('/')
        .ok_or_else(|| Error::Validation(format!("JSON pointer must start with '/': {pointer}")))?;
    Ok(rest
        .split('/')
        .map(|part| part.replace("~1", "/").replace("~0", "~"))
        .collect())
}

fn key_pointer(pointer: &str) -> Result<Vec<String>> {
    let parts = if let Some(rest) = pointer.strip_prefix('/') {
        rest.split('/').collect::<Vec<_>>()
    } else {
        pointer.split('.').collect::<Vec<_>>()
    };
    if parts.is_empty() || parts.iter().any(|part| part.is_empty()) {
        return Err(Error::Validation(format!("invalid key pointer {pointer}")));
    }
    Ok(parts.into_iter().map(str::to_owned).collect())
}

fn toml_item_mut<'a>(
    mut item: &'a mut Item,
    keys: &[String],
    pointer: &str,
) -> Result<&'a mut Item> {
    for key in keys {
        item = item
            .get_mut(key)
            .ok_or_else(|| Error::Validation(format!("TOML pointer {pointer} was not found")))?;
    }
    Ok(item)
}

fn toml_item<'a>(mut item: &'a Item, keys: &[String], pointer: &str) -> Result<&'a Item> {
    for key in keys {
        item = item
            .get(key)
            .ok_or_else(|| Error::Validation(format!("TOML pointer {pointer} was not found")))?;
    }
    Ok(item)
}

fn find_yaml_line(text: &str, keys: &[String], pointer: &str) -> Result<Range<usize>> {
    let mut stack: Vec<(usize, String)> = Vec::new();
    let mut offset = 0;
    for line_with_newline in text.split_inclusive('\n') {
        let line = line_with_newline
            .strip_suffix('\n')
            .unwrap_or(line_with_newline);
        let trimmed = line.trim_start();
        if trimmed.is_empty() || trimmed.starts_with('#') || trimmed.starts_with('-') {
            offset += line_with_newline.len();
            continue;
        }
        let indent = line.len() - trimmed.len();
        let Some(colon) = trimmed.find(':') else {
            offset += line_with_newline.len();
            continue;
        };
        let key = trimmed[..colon].trim().trim_matches(['\'', '"']).to_owned();
        while stack.last().is_some_and(|(prior, _)| *prior >= indent) {
            stack.pop();
        }
        let mut path = stack
            .iter()
            .map(|(_, key)| key.as_str())
            .collect::<Vec<_>>();
        path.push(&key);
        if path == keys.iter().map(String::as_str).collect::<Vec<_>>() {
            return Ok(offset..offset + line.len());
        }
        if trimmed[colon + 1..].trim().is_empty() {
            stack.push((indent, key));
        }
        offset += line_with_newline.len();
    }
    Err(Error::Validation(format!(
        "YAML pointer {pointer} was not found"
    )))
}

fn yaml_value_end(line: &str, start: usize) -> usize {
    let bytes = line.as_bytes();
    let quote = bytes
        .get(start)
        .copied()
        .filter(|byte| matches!(byte, b'\'' | b'"'));
    let mut index = start;
    let mut escaped = false;
    while index < bytes.len() {
        let byte = bytes[index];
        if let Some(quote) = quote {
            if index > start && byte == quote && (!escaped || quote == b'\'') {
                index += 1;
                break;
            }
            escaped = byte == b'\\' && !escaped;
            if byte != b'\\' {
                escaped = false;
            }
        } else if byte == b'#' && (index == start || bytes[index - 1].is_ascii_whitespace()) {
            break;
        }
        index += 1;
    }
    while index > start && bytes[index - 1].is_ascii_whitespace() {
        index -= 1;
    }
    index
}

fn quote_like_yaml(old: &str, value: &str) -> String {
    if old.starts_with('\'') && old.ends_with('\'') {
        format!("'{}'", value.replace('\'', "''"))
    } else if old.starts_with('"') && old.ends_with('"') {
        serde_json::to_string(value).expect("string serialization cannot fail")
    } else {
        value.to_owned()
    }
}

fn unquote_yaml(raw: &str) -> &str {
    if raw.len() >= 2
        && ((raw.starts_with('\'') && raw.ends_with('\''))
            || (raw.starts_with('"') && raw.ends_with('"')))
    {
        &raw[1..raw.len() - 1]
    } else {
        raw
    }
}

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

    #[test]
    fn json_targeted_edit_preserves_odd_indentation() {
        let input = include_str!("../../tests/fixtures/formats/odd.json");
        let output = JsonFormat
            .edit_text(input, "/metadata/version", "2.0.0")
            .expect("JSON edit");
        assert_eq!(output, input.replacen("\"1.2.3\"", "\"2.0.0\"", 1));
        assert_eq!(
            JsonFormat
                .read_text(&output, "/metadata/version")
                .expect("JSON read"),
            "2.0.0"
        );
    }

    #[test]
    fn toml_edit_preserves_comments_and_spacing() {
        let input = include_str!("../../tests/fixtures/formats/comments.toml");
        let output = TomlFormat
            .edit_text(input, "/package/version", "2.0.0")
            .expect("TOML edit");
        assert!(output.contains("# Package comment"));
        assert!(output.contains("version = \"2.0.0\" # keep this comment"));
        assert!(output.contains("version = \"9.9.9\""));
        assert_eq!(
            TomlFormat
                .read_text(&output, "package.version")
                .expect("TOML read"),
            "2.0.0"
        );
    }

    #[test]
    fn yaml_edit_preserves_comments_quotes_and_other_keys() {
        let input = include_str!("../../tests/fixtures/formats/comments.yaml");
        let output = YamlFormat
            .edit_text(input, "/package/version", "2.0.0")
            .expect("YAML edit");
        assert!(output.contains("version: '2.0.0' # keep this comment"));
        assert!(output.contains("version: \"9.9.9\""));
        assert_eq!(
            YamlFormat
                .read_text(&output, "/package/version")
                .expect("YAML read"),
            "2.0.0"
        );
    }
}