ElyPDF: an OS permission error shows as an error, never a missing text key (P-01); component library f834f55

This commit is contained in:
ZacharyZhang-NY committed 2026-10-09 10:58:47 -04:00
1 parent ab85673eea
commit 943411fb1a
17 files changed
+90 -25

No files matched your search

+23 -3
View File
@@ -293,10 +293,30 @@ impl Drop for Worker {
}
}
/// A permission refusal is `PermissionDenied`, named by what it needs.
/// A request the document's permissions refused, by what it needs.
#[derive(Debug)]
pub struct Refused(pub &'static str);
impl std::fmt::Display for Refused {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
write!(f, "{}{}", crate::proto::may::REFUSED, self.0)
}
}
impl std::error::Error for Refused {}
/// What a permission refusal needs; none for other errors.
pub fn refused(error: &io::Error) -> Option<&'static str> {
error.get_ref()?.downcast_ref::<Refused>().map(|refused| refused.0)
}
/// A worker error; a refusal names a permission it knows.
fn refusal(text: String) -> io::Error {
match text.strip_prefix(crate::proto::may::REFUSED) {
Some(needed) => io::Error::new(io::ErrorKind::PermissionDenied, needed.to_string()),
let named = text
.strip_prefix(crate::proto::may::REFUSED)
.and_then(|needed| crate::proto::may::NAMES.iter().find(|(_, name)| *name == needed));
match named {
Some((_, name)) => io::Error::other(Refused(name)),
None => io::Error::other(text),
}
}
+1 -1
View File
@@ -14,7 +14,7 @@ pub mod proto;
#[cfg(any(test, feature = "test-support"))]
pub mod sample;
pub use client::{Doc, Opened, Tile, Worker, is_stale};
pub use client::{Doc, Opened, Refused, Tile, Worker, is_stale, refused};
pub use content::{Drawn, TextBlock, Writing, Wrote};
pub use ocr::{OcrLine, tessdata};
pub use output::Optimizing;
+3 -2
View File
@@ -1,6 +1,6 @@
//! Images, text, optimized copies, printing leave, repaired files (C11.8).
use std::{io::ErrorKind, path::Path};
use std::path::Path;
use ely_pdf_engine::{
Doc, Lock, Optimizing, Worker,
@@ -89,7 +89,8 @@ fn printing_and_copying_need_leave() {
worker.protect(doc, &limited, Some(&Lock { user: String::new(), owner: "owner".into(), allow: may::EDIT })).expect("protects");
let (mut other, read) = opened(&limited);
for refused in [other.print_copy(read, &dir.path().join("p.pdf")), other.export_text(read, &dir.path().join("t.txt"))] {
assert_eq!(refused.expect_err("refused").kind(), ErrorKind::PermissionDenied);
let refused = refused.expect_err("refused");
assert!(ely_pdf_engine::refused(&refused).is_some(), "refused: {refused}");
}
worker.print_copy(doc, &dir.path().join("print.pdf")).expect("an unprotected file prints");
}
+4 -4
View File
@@ -1,6 +1,6 @@
//! Protected copies and what they allow (PDF-11).
use std::{io::ErrorKind, path::Path};
use std::path::Path;
use ely_pdf_engine::{
Doc, Lock, Worker,
@@ -43,7 +43,7 @@ fn a_protected_copy_asks_and_holds_its_limits() {
let allowed = worker.permissions(opened.doc).expect("reads");
assert_eq!(allowed & (may::PRINT | may::COPY | may::EDIT | may::OWNER), may::PRINT);
let refused = worker.rotate_pages(opened.doc, &[0], 90).expect_err("refused");
assert_eq!((refused.kind(), refused.to_string()), (ErrorKind::PermissionDenied, "assemble".to_string()));
assert_eq!(ely_pdf_engine::refused(&refused), Some("assemble"));
let text: String = worker.text(opened.doc, 0).expect("still reads").iter().map(|c| c.c).collect();
assert!(text.contains("Hello"));
}
@@ -72,7 +72,7 @@ fn changing_protection_needs_the_owner() {
worker.unlock(doc, "user").expect("unlocks");
let bare = dir.path().join("bare.pdf");
let refused = worker.protect(doc, &bare, None).expect_err("refused");
assert_eq!(refused.kind(), ErrorKind::PermissionDenied);
assert!(ely_pdf_engine::refused(&refused).is_some(), "refused: {refused}");
assert!(!bare.exists());
worker.owner(doc, "owner").expect("tries");
worker.protect(doc, &bare, None).expect("unprotects");
@@ -103,7 +103,7 @@ fn a_file_with_no_open_password_still_holds_limits() {
let opened = other.open(&out).expect("opens");
assert!(!opened.locked);
let refused = other.annotate(opened.doc, &[]).expect_err("refused");
assert_eq!((refused.kind(), refused.to_string()), (ErrorKind::PermissionDenied, "annotate".to_string()));
assert_eq!(ely_pdf_engine::refused(&refused), Some("annotate"));
}
#[test]
+2 -2
View File
@@ -1,6 +1,6 @@
//! Signing by update and checking what was signed (PDF-12, QA-15).
use std::{io::ErrorKind, path::{Path, PathBuf}};
use std::path::{Path, PathBuf};
use ely_pdf_engine::{
Digest, Doc, Lock, Signing, Worker,
@@ -128,7 +128,7 @@ fn a_new_field_needs_leave_to_change_the_page() {
worker.protect(doc, &limited, Some(&lock)).expect("protects");
let (mut other, read) = opened(&limited);
let refused = other.sign(read, &dir.path().join("signed.pdf"), &signing(None, "S", "ely")).expect_err("refused");
assert_eq!(refused.kind(), ErrorKind::PermissionDenied);
assert!(ely_pdf_engine::refused(&refused).is_some(), "refused: {refused}");
}
#[test]
+5
View File
@@ -154,6 +154,11 @@ impl Root {
});
}
/// The toasts on show: title and severity, oldest first.
pub fn toasts(&self, cx: &gpui::App) -> Vec<(SharedString, Option<Severity>)> {
self.toaster.read(cx).shown()
}
/// A toast whose button opens the output `path` (§16).
pub fn toast_output(
&mut self,