Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
19 changes: 0 additions & 19 deletions backend/src/distribution/file_format.rs
Original file line number Diff line number Diff line change
Expand Up @@ -254,19 +254,9 @@ pub(super) struct DocumentProcessingFile {
#[serde(default)]
pub(super) allowed_operations: Vec<ProcessingOperation>,
#[serde(default)]
pub(super) operation_policies: Vec<ProcessingOperationPolicyFile>,
#[serde(default)]
pub(super) input_profiles: Vec<ProcessingInputProfilesFile>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub(super) struct ProcessingOperationPolicyFile {
pub(super) operation: ProcessingOperation,
#[serde(default)]
pub(super) required_typst_packages: Vec<TypstPackageRequirementFile>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub(super) struct ProcessingInputProfilesFile {
Expand All @@ -284,15 +274,6 @@ pub(super) struct ProcessingInputProfileFile {
pub(super) description: LocalizedTextFile,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub(super) struct TypstPackageRequirementFile {
pub(super) namespace: String,
pub(super) name: String,
#[serde(default)]
pub(super) allowed_versions: Vec<String>,
}

#[derive(Debug, Deserialize)]
#[serde(deny_unknown_fields)]
pub(super) struct TypstFile {
Expand Down
76 changes: 3 additions & 73 deletions backend/src/distribution/loader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,18 @@

use super::ai_assistant::load_ai_assistant;
use super::experience_content::load_experience;
use super::file_format::{
DistributionFile, ProcessingInputProfilesFile, ProcessingOperationPolicyFile,
};
use super::file_format::{DistributionFile, ProcessingInputProfilesFile};
use super::template_catalog::load_builtin_templates;
use super::{
is_hex_color, read_template, resolve_distribution_file, resolve_path, validate_localized_text,
validate_slug, CheckpointBranchPrefix, DistributionConfig,
DocumentProcessingDistributionConfig, FrontendFeature, FrontendFeaturesConfig, GitConfig,
ProcessingOperationInputProfiles, ProcessingOperationPolicy, ProductAsset, ProductConfig,
CONFIG_SCHEMA_VERSION, MAX_PRODUCT_ASSET_BYTES,
ProcessingOperationInputProfiles, ProductAsset, ProductConfig, CONFIG_SCHEMA_VERSION,
MAX_PRODUCT_ASSET_BYTES,
};
use crate::document_processing::{
ProcessingInputProfile, ProcessingInputProfileSelector, ProcessingOperation,
};
use crate::typst_runtime::TypstPackageRequirement;
use crate::workspace::ProjectType;
use std::collections::HashSet;
use std::env;
Expand Down Expand Up @@ -134,10 +131,6 @@ impl DistributionConfig {
file.document_processing.allowed_operations,
&project_types,
)?;
let processing_operation_policies = validate_processing_operation_policies(
file.document_processing.operation_policies,
&processing_operations,
)?;
let processing_input_profiles = validate_processing_input_profiles(
file.document_processing.input_profiles,
&processing_operations,
Expand Down Expand Up @@ -205,7 +198,6 @@ impl DistributionConfig {
ai_assistant,
document_processing: DocumentProcessingDistributionConfig {
allowed_operations: processing_operations,
operation_policies: processing_operation_policies,
input_profiles: processing_input_profiles,
},
experience,
Expand Down Expand Up @@ -304,68 +296,6 @@ fn validate_processing_operations(
Ok(normalized)
}

fn validate_processing_operation_policies(
values: Vec<ProcessingOperationPolicyFile>,
allowed_operations: &[ProcessingOperation],
) -> Result<Vec<ProcessingOperationPolicy>, String> {
let mut policies = Vec::with_capacity(values.len());
let mut operations = HashSet::new();
for value in values {
if !allowed_operations.contains(&value.operation) {
return Err(format!(
"document_processing.operation_policies cannot configure disabled operation {}",
value.operation
));
}
if value.operation.project_type() != Some(ProjectType::Typst) {
return Err(format!(
"document_processing.operation_policies cannot require Typst packages for {}",
value.operation
));
}
if !operations.insert(value.operation) {
return Err(format!(
"document_processing.operation_policies repeats operation {}",
value.operation
));
}
if value.required_typst_packages.is_empty() {
return Err(format!(
"document_processing.operation_policies for {} must contain a requirement",
value.operation
));
}
let mut requirements = Vec::with_capacity(value.required_typst_packages.len());
let mut identities = HashSet::new();
for requirement in value.required_typst_packages {
let identity = format!("@{}/{}", requirement.namespace, requirement.name);
if !identities.insert(identity.clone()) {
return Err(format!(
"document_processing.operation_policies for {} repeats package {identity}",
value.operation
));
}
let requirement = TypstPackageRequirement::parse(
requirement.namespace,
requirement.name,
requirement.allowed_versions,
)
.ok_or_else(|| {
format!(
"document_processing.operation_policies for {} contains invalid package {identity}",
value.operation
)
})?;
requirements.push(requirement);
}
policies.push(ProcessingOperationPolicy {
operation: value.operation,
required_typst_packages: requirements,
});
}
Ok(policies)
}

fn validate_processing_input_profiles(
values: Vec<ProcessingInputProfilesFile>,
allowed_operations: &[ProcessingOperation],
Expand Down
84 changes: 1 addition & 83 deletions backend/src/distribution/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ pub(crate) mod template_catalog;
use crate::document_processing::{ProcessingInputProfileSelector, ProcessingOperation};
use crate::experience::{ExperienceResourceKind, ExperienceVisibility};
use crate::text_enum::text_enum;
use crate::typst_runtime::{TypstPackageRequirement, TypstProjectDependencies};
use crate::workspace::ProjectType;
pub use ai_assistant::{AiAssistantConfig, AiConnectionPolicyKind, ManagedAiCatalogConfig};
use experience_content::{ExperienceConfig, ExperienceResource, LandingConfig, LandingHighlight};
Expand Down Expand Up @@ -138,16 +137,9 @@ pub struct FrontendFeaturesConfig {
#[derive(Clone, Debug)]
pub struct DocumentProcessingDistributionConfig {
pub allowed_operations: Vec<ProcessingOperation>,
pub operation_policies: Vec<ProcessingOperationPolicy>,
pub input_profiles: Vec<ProcessingOperationInputProfiles>,
}

#[derive(Clone, Debug)]
pub struct ProcessingOperationPolicy {
pub operation: ProcessingOperation,
pub required_typst_packages: Vec<TypstPackageRequirement>,
}

#[derive(Clone, Debug)]
pub struct ProcessingOperationInputProfiles {
pub operation: ProcessingOperation,
Expand Down Expand Up @@ -188,7 +180,6 @@ impl Default for DistributionConfig {
ai_assistant: None,
document_processing: DocumentProcessingDistributionConfig {
allowed_operations: vec![ProcessingOperation::LatexCompilePdfV1],
operation_policies: Vec::new(),
input_profiles: Vec::new(),
},
experience: ExperienceConfig {
Expand Down Expand Up @@ -276,30 +267,6 @@ impl DistributionConfig {
.contains(&operation)
}

pub(crate) fn processing_operation_applicable(
&self,
operation: ProcessingOperation,
dependencies: Option<&TypstProjectDependencies>,
) -> bool {
let Some(policy) = self
.document_processing
.operation_policies
.iter()
.find(|policy| policy.operation == operation)
else {
return true;
};
let Some(dependencies) = dependencies else {
return false;
};
policy.required_typst_packages.iter().all(|requirement| {
dependencies
.packages
.iter()
.any(|package| requirement.matches(package))
})
}

pub(crate) fn processing_input_profile_selector(
&self,
operation: ProcessingOperation,
Expand Down Expand Up @@ -369,13 +336,11 @@ fn is_hex_color(value: &str) -> bool {
mod tests {
use super::{
AiConnectionPolicyKind, CheckpointBranchPrefix, DistributionConfig,
InvalidCheckpointBranchPrefix, ProcessingOperationPolicy,
InvalidCheckpointBranchPrefix,
};
use crate::document_processing::ProcessingOperation;
use crate::experience::ExperienceVisibility;
use crate::typst_runtime::{PackageSpec, TypstPackageRequirement, TypstProjectDependencies};
use crate::workspace::ProjectType;
use std::collections::HashSet;
use std::error::Error;
use std::path::Path;
use uuid::Uuid;
Expand Down Expand Up @@ -464,51 +429,4 @@ mod tests {
);
}
}

#[test]
fn processing_policy_matches_an_exact_reachable_typst_package(
) -> Result<(), Box<dyn std::error::Error>> {
let mut config = DistributionConfig::default();
let requirement = TypstPackageRequirement::parse(
"local".to_string(),
"slides".to_string(),
vec!["0.7.0".to_string()],
)
.ok_or("test package requirement is invalid")?;
config.document_processing.operation_policies = vec![ProcessingOperationPolicy {
operation: ProcessingOperation::TypstExportPptxV1,
required_typst_packages: vec![requirement],
}];
let matching = TypstProjectDependencies {
packages: HashSet::from([PackageSpec::parse(
"local".to_string(),
"slides".to_string(),
"0.7.0".to_string(),
)
.ok_or("matching test package spec is invalid")?]),
has_dynamic_imports: false,
};
let wrong_version = TypstProjectDependencies {
packages: HashSet::from([PackageSpec::parse(
"local".to_string(),
"slides".to_string(),
"0.6.0".to_string(),
)
.ok_or("wrong-version test package spec is invalid")?]),
has_dynamic_imports: false,
};

assert!(config.processing_operation_applicable(
ProcessingOperation::TypstExportPptxV1,
Some(&matching)
));
assert!(!config.processing_operation_applicable(
ProcessingOperation::TypstExportPptxV1,
Some(&wrong_version)
));
assert!(
config.processing_operation_applicable(ProcessingOperation::LatexCompilePdfV1, None)
);
Ok(())
}
}
12 changes: 2 additions & 10 deletions backend/src/document_processing/project_input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,6 @@
use super::persistence::{pin_project_assets, release_project_asset_pins};
use super::ProcessingOperation;
use crate::collaboration::{CollaborationContext, FlushProjectCollaborationError};
use crate::distribution::DistributionConfig;
use crate::object_storage::ObjectStorage;
use crate::typst_runtime::{
analyze_project_dependencies, resolve_processing_package_closure,
Expand Down Expand Up @@ -45,7 +44,6 @@ pub(super) struct TypstProjectBundleCapture<'a> {
pub db: &'a PgPool,
pub storage: Option<&'a ObjectStorage>,
pub collaboration: &'a CollaborationContext,
pub distribution: &'a DistributionConfig,
pub builtin_dir: &'a Path,
pub operation: ProcessingOperation,
pub job_id: Uuid,
Expand All @@ -65,8 +63,6 @@ pub(super) enum CaptureProjectBundleError {
TooManyFiles,
#[error("project input exceeds the configured processing limit")]
TooLarge,
#[error("project does not satisfy the processing operation policy")]
OperationInapplicable,
#[error(transparent)]
Package(#[from] ResolveProcessingPackagesError),
#[error("collaboration state could not be captured")]
Expand Down Expand Up @@ -173,7 +169,6 @@ pub(super) async fn capture_typst_project_bundle(
db,
storage,
collaboration,
distribution,
builtin_dir,
operation,
job_id,
Expand All @@ -182,9 +177,7 @@ pub(super) async fn capture_typst_project_bundle(
} = capture;
let snapshot = capture_project_snapshot(db, collaboration, job_id, project_id).await?;
let dependencies = analyze_project_dependencies(&snapshot.entry_file_path, &snapshot.documents);
let result = if !distribution.processing_operation_applicable(operation, Some(&dependencies)) {
Err(CaptureProjectBundleError::OperationInapplicable)
} else {
let result =
match resolve_processing_package_closure(builtin_dir, &dependencies, max_input_bytes).await
{
Ok(packages) => {
Expand All @@ -200,8 +193,7 @@ pub(super) async fn capture_typst_project_bundle(
.await
}
Err(error) => Err(error.into()),
}
};
};
release_pins(db, job_id).await;
result
}
Expand Down
20 changes: 1 addition & 19 deletions backend/src/document_processing/public_http.rs
Original file line number Diff line number Diff line change
Expand Up @@ -127,7 +127,6 @@ pub(crate) async fn create_typst_pptx_export(
db: &state.db,
storage: state.storage.as_ref(),
collaboration: &state.collaboration,
distribution: &state.distribution,
builtin_dir: &state.typst_builtin_dir,
operation,
job_id: reserved.job_id,
Expand Down Expand Up @@ -608,14 +607,6 @@ pub(crate) async fn project_processing_capabilities(
ProjectProcessingCapabilityState::Inapplicable,
Some("dynamic_typst_dependency".to_string()),
)
} else if !state
.distribution
.processing_operation_applicable(operation, dependencies.as_ref())
{
(
ProjectProcessingCapabilityState::Inapplicable,
Some("required_typst_package_missing".to_string()),
)
} else {
let stats = capability_stats(&state.db, operation)
.await
Expand Down Expand Up @@ -876,10 +867,6 @@ fn capture_failure(error: &CaptureProjectBundleError) -> (&'static str, &'static
"input_too_large",
"Project exceeds the processing input limit",
),
CaptureProjectBundleError::OperationInapplicable => (
"operation_inapplicable",
"Project does not satisfy the processing operation policy",
),
CaptureProjectBundleError::Package(
ResolveProcessingPackagesError::TooManyPackages
| ResolveProcessingPackagesError::TooLarge,
Expand All @@ -889,7 +876,7 @@ fn capture_failure(error: &CaptureProjectBundleError) -> (&'static str, &'static
),
CaptureProjectBundleError::Package(ResolveProcessingPackagesError::DynamicDependency) => (
"dynamic_typst_dependency",
"Project contains a dynamic Typst package dependency",
"Project contains a dynamic Typst dependency",
),
CaptureProjectBundleError::Package(_) => (
"package_capture_failed",
Expand Down Expand Up @@ -927,11 +914,6 @@ fn capture_error(error: CaptureProjectBundleError) -> ApiError {
"Project exceeds the processing input limit",
)
}
CaptureProjectBundleError::OperationInapplicable => ApiError::new(
StatusCode::UNPROCESSABLE_ENTITY,
ApiErrorCode::ProcessingOperationInvalid,
"Project does not satisfy the processing operation policy",
),
CaptureProjectBundleError::Package(
ResolveProcessingPackagesError::TooManyPackages
| ResolveProcessingPackagesError::TooLarge,
Expand Down
2 changes: 1 addition & 1 deletion backend/src/typst_runtime/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@ mod universe;

pub(crate) use dependencies::{analyze_project_dependencies, TypstProjectDependencies};
pub(crate) use http::{typst_builtin_asset, typst_package_proxy};
pub(crate) use package::{PackageSpec, TypstPackageRequirement};
pub(crate) use package::PackageSpec;
pub(crate) use processing_packages::{
resolve_processing_package_closure, ResolveProcessingPackagesError, ResolvedTypstPackage,
};
Expand Down
Loading