Skip to main content

griz_core/
problem.rs

1//! Why an operation could not be planned.
2
3use schemars::JsonSchema;
4use serde::{Deserialize, Serialize};
5use std::path::PathBuf;
6
7/// A line-numbered excerpt of real file text.
8#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
9pub struct Window {
10    /// 1-based first line.
11    pub line: usize,
12    /// The excerpt.
13    pub text: String,
14}
15
16/// Why an operation could not be planned.
17#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
18#[serde(tag = "kind", rename_all = "snake_case")]
19pub enum ProblemKind {
20    /// The anchor matched nothing; `nearest` is the closest real text.
21    Missing {
22        /// Closest window, when the file has any text.
23        nearest: Option<Window>,
24    },
25    /// The anchor matched more than once.
26    Ambiguous {
27        /// 1-based line of every candidate.
28        lines: Vec<usize>,
29    },
30    /// The file no longer has the fingerprint the caller expected.
31    Stale {
32        /// Fingerprint the caller expected.
33        expected: String,
34        /// Fingerprint found, absent when the file is gone.
35        actual: Option<String>,
36    },
37    /// A create or move target already exists.
38    Exists,
39    /// The file does not exist.
40    NotFound,
41    /// A byte range does not fit the text or overlaps an earlier edit.
42    BadRange,
43    /// The operation is malformed.
44    Invalid {
45        /// What is wrong.
46        message: String,
47    },
48}
49
50/// One operation that could not be planned.
51#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, JsonSchema)]
52pub struct Problem {
53    /// Index of the operation.
54    pub op: usize,
55    /// File it targeted.
56    pub path: PathBuf,
57    /// What went wrong.
58    #[serde(flatten)]
59    pub kind: ProblemKind,
60}