mirror of
https://github.com/storytold/photocraft.git
synced 2026-12-02 06:20:48 +01:00
Import Notes: a source without notes says "No notes were imported." (#13915)
A readable file without notes, and one the import cannot read as a notes source (not an image, damaged, truncated), now fail with Photoshop's "No notes were imported." instead of "<file> has no notes" or the decoder's text. No history step, document unmodified. A file that cannot be read at all keeps its "<file>: <why>" error, and a memory refusal keeps its own message.
This commit is contained in:
1 parent
5bdbcc1181
commit
4da228b43d
2 files changed
+106
-2
No files matched your search
@@ -190,11 +190,21 @@ fn import(s: &mut Session, p: &Value) -> Result<Value> {
|
||||
import_notes_from(s, &name, &bytes)
|
||||
}
|
||||
|
||||
/// Photoshop's alert when a source gives no notes: one without any, or one it cannot read as a
|
||||
/// notes source (#13915).
|
||||
pub const NO_NOTES_IMPORTED: &str = "No notes were imported.";
|
||||
|
||||
/// File › Import › Notes… from file bytes (what a file picker returns).
|
||||
pub fn import_notes_from(s: &mut Session, name: &str, bytes: &[u8]) -> Result<Value> {
|
||||
let src = crate::file_cmds::import(name, bytes)?;
|
||||
let src = match crate::file_cmds::import(name, bytes) {
|
||||
Ok(src) => src,
|
||||
// A memory refusal says what the user can do about it; keep it.
|
||||
Err(e @ (EngineError::PsdApprovalRequired { .. } | EngineError::PsdHardBudget { .. })) => return Err(e),
|
||||
// Not an image, damaged or truncated: no notes came out of it (#13915).
|
||||
Err(_) => return Err(EngineError::Other(NO_NOTES_IMPORTED.into())),
|
||||
};
|
||||
if src.notes.is_empty() {
|
||||
return Err(EngineError::Other(format!("{name} has no notes")));
|
||||
return Err(EngineError::Other(NO_NOTES_IMPORTED.into()));
|
||||
}
|
||||
let d = s.active().ok_or(EngineError::NoDocument)?.doc.clone();
|
||||
let (w, h) = (f64::from(d.size.width), f64::from(d.size.height));
|
||||
@@ -270,6 +280,10 @@ pub fn specs() -> Vec<CommandSpec> {
|
||||
]
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
#[path = "notes_import_message_tests.rs"]
|
||||
mod notes_import_message_tests;
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
|
||||
@@ -0,0 +1,90 @@
|
||||
//! #13915: File › Import › Notes… from a source that gives no notes fails as Photoshop does, with
|
||||
//! "No notes were imported.", no history step and the document unmodified. A file that cannot be
|
||||
//! read at all keeps its "<file>: <why>" error.
|
||||
|
||||
use photocraft_color::{ColorMode, SampleType};
|
||||
use photocraft_doc::{Document, Note, Size};
|
||||
use serde_json::json;
|
||||
|
||||
use super::{NO_NOTES_IMPORTED, import_notes_from};
|
||||
use crate::Session;
|
||||
|
||||
fn session() -> Session {
|
||||
let mut s = Session::new();
|
||||
s.execute("file.new", json!({"width": 100, "height": 80})).unwrap();
|
||||
s
|
||||
}
|
||||
|
||||
fn source(notes: usize) -> Document {
|
||||
let mut d = Document::new("src", Size::new(60, 40), ColorMode::Rgb, SampleType::U8);
|
||||
for i in 0..notes {
|
||||
d.notes.push(Note { text: format!("note {i}"), position: [10.0, 10.0], ..Default::default() });
|
||||
}
|
||||
d
|
||||
}
|
||||
|
||||
fn pcraft(notes: usize) -> Vec<u8> {
|
||||
photocraft_format::save_to_bytes(&source(notes), &Default::default()).unwrap()
|
||||
}
|
||||
|
||||
fn psd(notes: usize) -> Vec<u8> {
|
||||
photocraft_io::export(&source(notes), "x.psd", &photocraft_io::ExportOptions::default()).unwrap().bytes
|
||||
}
|
||||
|
||||
/// Imports `bytes` as `name` and checks the refusal leaves the document as it was.
|
||||
fn refused(name: &str, bytes: &[u8]) -> String {
|
||||
let mut s = session();
|
||||
let past = s.active().unwrap().history.past_len();
|
||||
let dirty = s.active().unwrap().is_dirty();
|
||||
let e = import_notes_from(&mut s, name, bytes).expect_err(name).to_string();
|
||||
let a = s.active().unwrap();
|
||||
assert_eq!(a.history.past_len(), past, "{name}: no history step");
|
||||
assert_eq!(a.is_dirty(), dirty, "{name}: the document stays unmodified");
|
||||
assert!(a.doc.notes.is_empty(), "{name}");
|
||||
e
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_file_without_notes_says_no_notes_were_imported() {
|
||||
assert_eq!(refused("none.pcraft", &pcraft(0)), NO_NOTES_IMPORTED);
|
||||
assert_eq!(refused("none.psd", &psd(0)), NO_NOTES_IMPORTED);
|
||||
assert_eq!(NO_NOTES_IMPORTED, "No notes were imported.");
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_file_that_is_no_notes_source_says_the_same() {
|
||||
// Not an image.
|
||||
assert_eq!(refused("x.txt", b"just some text\n"), NO_NOTES_IMPORTED);
|
||||
assert_eq!(refused("empty.psd", &[]), NO_NOTES_IMPORTED);
|
||||
// Damaged: a PSD signature with nothing valid behind it.
|
||||
assert_eq!(refused("bad.psd", b"8BPS\0\x01\xff\xff\xff\xff\xff\xff"), NO_NOTES_IMPORTED);
|
||||
// Truncated files that do have notes, cut at several lengths.
|
||||
for (name, bytes) in [("cut.psd", psd(2)), ("cut.pcraft", pcraft(2))] {
|
||||
for len in [4, 26, bytes.len() / 2, bytes.len() - 1] {
|
||||
assert_eq!(refused(name, &bytes[..len]), NO_NOTES_IMPORTED, "{name} cut to {len} bytes");
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
#[test]
|
||||
fn a_file_with_notes_still_imports() {
|
||||
for (name, bytes) in [("a.pcraft", pcraft(2)), ("a.psd", psd(2))] {
|
||||
let mut s = session();
|
||||
let past = s.active().unwrap().history.past_len();
|
||||
let r = import_notes_from(&mut s, name, &bytes).unwrap();
|
||||
assert_eq!(r["imported"], 2, "{name}");
|
||||
assert_eq!(s.active().unwrap().doc.notes.len(), 2, "{name}");
|
||||
assert_eq!(s.active().unwrap().history.past_len(), past + 1, "{name}");
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(not(target_arch = "wasm32"))]
|
||||
#[test]
|
||||
fn a_file_that_cannot_be_read_keeps_its_own_error() {
|
||||
let mut s = session();
|
||||
let path = std::env::temp_dir().join(format!("pc-notes-missing-{}", std::process::id())).join("gone.psd");
|
||||
let p = path.to_string_lossy().to_string();
|
||||
let e = s.execute("file.import.notes", json!({"path": p})).unwrap_err().to_string();
|
||||
assert!(e.starts_with(&format!("{p}: ")), "{e}");
|
||||
assert_ne!(e, NO_NOTES_IMPORTED);
|
||||
}
|
||||
Reference in new issue
Block a user