use crate::tool::proposal::ActionProposalError;
use serde::{Deserialize, Serialize};
use std::path::{Component, Path};
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct WorkspacePatchChangeEvidence {
pub(super) relative_path: String,
pub(super) preimage_bytes: usize,
pub(super) replacement_bytes: usize,
pub(super) file_bytes_before: usize,
pub(super) file_bytes_after: usize,
pub(super) file_fingerprint_before: String,
pub(super) file_fingerprint_after: String,
}
impl WorkspacePatchChangeEvidence {
pub fn new(
relative_path: impl Into<String>,
preimage_bytes: usize,
replacement_bytes: usize,
file_bytes_before: usize,
file_bytes_after: usize,
file_fingerprint_before: impl Into<String>,
file_fingerprint_after: impl Into<String>,
) -> Result<Self, ActionProposalError> {
let relative_path = validate_apply_patch_path(relative_path.into())?;
validate_apply_patch_counts(
preimage_bytes,
replacement_bytes,
file_bytes_before,
file_bytes_after,
)?;
let file_fingerprint_before = validate_apply_patch_fingerprint(
"file_fingerprint_before",
file_fingerprint_before.into(),
)?;
let file_fingerprint_after = validate_apply_patch_fingerprint(
"file_fingerprint_after",
file_fingerprint_after.into(),
)?;
Ok(Self {
relative_path,
preimage_bytes,
replacement_bytes,
file_bytes_before,
file_bytes_after,
file_fingerprint_before,
file_fingerprint_after,
})
}
#[must_use]
pub fn relative_path(&self) -> &str {
&self.relative_path
}
#[must_use]
pub fn preimage_bytes(&self) -> usize {
self.preimage_bytes
}
#[must_use]
pub fn replacement_bytes(&self) -> usize {
self.replacement_bytes
}
#[must_use]
pub fn file_bytes_before(&self) -> usize {
self.file_bytes_before
}
#[must_use]
pub fn file_bytes_after(&self) -> usize {
self.file_bytes_after
}
#[must_use]
pub fn file_fingerprint_before(&self) -> &str {
&self.file_fingerprint_before
}
#[must_use]
pub fn file_fingerprint_after(&self) -> &str {
&self.file_fingerprint_after
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct WorkspacePatchExecutionEvidence {
pub(super) changes: Vec<WorkspacePatchChangeEvidence>,
}
impl WorkspacePatchExecutionEvidence {
pub fn new(
relative_path: impl Into<String>,
preimage_bytes: usize,
replacement_bytes: usize,
file_bytes_before: usize,
file_bytes_after: usize,
file_fingerprint_before: impl Into<String>,
file_fingerprint_after: impl Into<String>,
) -> Result<Self, ActionProposalError> {
Self::from_changes(vec![WorkspacePatchChangeEvidence::new(
relative_path,
preimage_bytes,
replacement_bytes,
file_bytes_before,
file_bytes_after,
file_fingerprint_before,
file_fingerprint_after,
)?])
}
pub fn from_changes(
changes: Vec<WorkspacePatchChangeEvidence>,
) -> Result<Self, ActionProposalError> {
validate_apply_patch_changes(&changes)?;
Ok(Self { changes })
}
#[must_use]
pub fn changes(&self) -> &[WorkspacePatchChangeEvidence] {
&self.changes
}
pub(super) fn first_change(&self) -> &WorkspacePatchChangeEvidence {
self.changes
.first()
.expect("workspace patch execution evidence always has at least one change")
}
#[must_use]
pub fn relative_path(&self) -> &str {
self.first_change().relative_path()
}
#[must_use]
pub fn preimage_bytes(&self) -> usize {
self.first_change().preimage_bytes()
}
#[must_use]
pub fn replacement_bytes(&self) -> usize {
self.first_change().replacement_bytes()
}
#[must_use]
pub fn file_bytes_before(&self) -> usize {
self.first_change().file_bytes_before()
}
#[must_use]
pub fn file_bytes_after(&self) -> usize {
self.first_change().file_bytes_after()
}
#[must_use]
pub fn file_fingerprint_before(&self) -> &str {
self.first_change().file_fingerprint_before()
}
#[must_use]
pub fn file_fingerprint_after(&self) -> &str {
self.first_change().file_fingerprint_after()
}
}
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
#[serde(deny_unknown_fields)]
pub struct WorkspacePatchProposal {
pub(super) changes: Vec<WorkspacePatchChangeEvidence>,
}
impl WorkspacePatchProposal {
pub fn new(
relative_path: impl Into<String>,
preimage_bytes: usize,
replacement_bytes: usize,
file_bytes_before: usize,
file_bytes_after: usize,
file_fingerprint_before: impl Into<String>,
file_fingerprint_after: impl Into<String>,
) -> Result<Self, ActionProposalError> {
Self::from_changes(vec![WorkspacePatchChangeEvidence::new(
relative_path,
preimage_bytes,
replacement_bytes,
file_bytes_before,
file_bytes_after,
file_fingerprint_before,
file_fingerprint_after,
)?])
}
pub fn from_changes(
changes: Vec<WorkspacePatchChangeEvidence>,
) -> Result<Self, ActionProposalError> {
validate_apply_patch_changes(&changes)?;
Ok(Self { changes })
}
#[must_use]
pub fn changes(&self) -> &[WorkspacePatchChangeEvidence] {
&self.changes
}
pub(super) fn first_change(&self) -> &WorkspacePatchChangeEvidence {
self.changes
.first()
.expect("workspace patch proposal always has at least one change")
}
#[must_use]
pub fn relative_path(&self) -> &str {
self.first_change().relative_path()
}
#[must_use]
pub fn preimage_bytes(&self) -> usize {
self.first_change().preimage_bytes()
}
#[must_use]
pub fn replacement_bytes(&self) -> usize {
self.first_change().replacement_bytes()
}
#[must_use]
pub fn file_bytes_before(&self) -> usize {
self.first_change().file_bytes_before()
}
#[must_use]
pub fn file_bytes_after(&self) -> usize {
self.first_change().file_bytes_after()
}
#[must_use]
pub fn file_fingerprint_before(&self) -> &str {
self.first_change().file_fingerprint_before()
}
#[must_use]
pub fn file_fingerprint_after(&self) -> &str {
self.first_change().file_fingerprint_after()
}
}
pub(super) fn validate_apply_patch_changes(
changes: &[WorkspacePatchChangeEvidence],
) -> Result<(), ActionProposalError> {
if changes.is_empty() {
return Err(ActionProposalError::InvalidWorkspacePatch {
field: "changes",
reason: "must contain at least one file change",
});
}
Ok(())
}
pub(super) fn validate_apply_patch_counts(
preimage_bytes: usize,
replacement_bytes: usize,
file_bytes_before: usize,
file_bytes_after: usize,
) -> Result<(), ActionProposalError> {
if preimage_bytes == 0 && file_bytes_before != 0 {
return Err(ActionProposalError::InvalidWorkspacePatch {
field: "preimage_bytes",
reason: "must be greater than zero unless the file is new",
});
}
let expected_after = file_bytes_before
.checked_sub(preimage_bytes)
.and_then(|unchanged| unchanged.checked_add(replacement_bytes))
.ok_or(ActionProposalError::InvalidWorkspacePatch {
field: "file_bytes_after",
reason: "must be consistent with before, preimage, and replacement byte counts",
})?;
if expected_after != file_bytes_after {
return Err(ActionProposalError::InvalidWorkspacePatch {
field: "file_bytes_after",
reason: "must equal file_bytes_before - preimage_bytes + replacement_bytes",
});
}
Ok(())
}
pub(super) fn validate_apply_patch_fingerprint(
field: &'static str,
value: String,
) -> Result<String, ActionProposalError> {
if value.trim().is_empty() {
return Err(ActionProposalError::InvalidWorkspacePatch {
field,
reason: "must not be blank",
});
}
if value.len() > 128 {
return Err(ActionProposalError::InvalidWorkspacePatch {
field,
reason: "exceeds the byte limit",
});
}
let Some((algorithm, digest)) = value.split_once(':') else {
return Err(ActionProposalError::InvalidWorkspacePatch {
field,
reason: "must include an algorithm prefix",
});
};
if algorithm != "fnv1a64" {
return Err(ActionProposalError::InvalidWorkspacePatch {
field,
reason: "must use the fnv1a64 fingerprint prefix",
});
}
if digest.len() != 16 || !digest.bytes().all(|byte| byte.is_ascii_hexdigit()) {
return Err(ActionProposalError::InvalidWorkspacePatch {
field,
reason: "must include 16 hexadecimal digest characters",
});
}
Ok(value)
}
pub(super) const MAX_WORKSPACE_PATCH_RELATIVE_PATH_BYTES: usize = 4096;
pub(super) fn validate_apply_patch_path(value: String) -> Result<String, ActionProposalError> {
if value.trim().is_empty() {
return Err(ActionProposalError::InvalidWorkspacePatch {
field: "relative_path",
reason: "must not be blank",
});
}
if value.len() > MAX_WORKSPACE_PATCH_RELATIVE_PATH_BYTES {
return Err(ActionProposalError::InvalidWorkspacePatch {
field: "relative_path",
reason: "exceeds the byte limit",
});
}
if value.chars().any(char::is_control) {
return Err(ActionProposalError::InvalidWorkspacePatch {
field: "relative_path",
reason: "must not contain control characters",
});
}
let path = Path::new(&value);
let segments = value.strip_prefix('/').unwrap_or(&value);
if segments.split('/').any(str::is_empty) {
return Err(ActionProposalError::InvalidWorkspacePatch {
field: "relative_path",
reason: "must not contain empty path segments",
});
}
if !path.is_absolute() && value.starts_with('\\') {
return Err(ActionProposalError::InvalidWorkspacePatch {
field: "relative_path",
reason: "must use `/` separators",
});
}
let mut saw_component = false;
for component in path.components() {
match component {
Component::Normal(value) => {
if value.to_str().is_none() {
return Err(ActionProposalError::InvalidWorkspacePatch {
field: "relative_path",
reason: "components must be UTF-8",
});
}
saw_component = true;
}
Component::CurDir | Component::ParentDir => {
return Err(ActionProposalError::InvalidWorkspacePatch {
field: "relative_path",
reason: "must not contain dot segments",
});
}
Component::RootDir | Component::Prefix(_) => {}
}
}
if !saw_component {
return Err(ActionProposalError::InvalidWorkspacePatch {
field: "relative_path",
reason: "must name a file",
});
}
Ok(value)
}