Preserve CADCraft DWG diagnostics

This commit is contained in:
Dylan M. Taylor
2026-11-14 00:32:49 +00:00
parent 2a9fc8f241
commit 54dfb2ee00
2 changed files with 37 additions and 4 deletions
+16 -3
View File
@@ -61,6 +61,19 @@ pub enum IoError {
pub type Result<T> = std::result::Result<T, IoError>;
/// Restore CADCraft's public diagnostics after the shared byte bridge supplies generic wording.
fn cadcraft_dwg_message(message: &str) -> String {
message
.replace("accepted by this bridge", "CADCraft opens")
.replace("types this bridge can't preserve", "types CADCraft doesn't keep")
.replace("this bridge writes", "CADCraft writes")
.replace("accepted for DWG conversion", "CADCraft converts to DWG")
}
fn dwg_error(message: String) -> IoError {
IoError::Format(cadcraft_dwg_message(&message))
}
/// How a program runs the DWG reader and writer in a child process (`craft_dwg::worker`).
pub mod dwg_worker {
pub use craft_dwg::{WORKER_ARG, serve_if_asked, use_own_worker};
@@ -82,9 +95,9 @@ pub fn read(bytes: &[u8], name: impl AsRef<Path>) -> Result<Drawing> {
/// which the round-trip harness counts.
pub(crate) fn read_source(bytes: &[u8], name: &Path) -> Result<(Drawing, Option<Vec<u8>>)> {
if craft_dwg::is_dwg(bytes) {
let (dxf, notes) = craft_dwg::decode(bytes, craft_dwg::Limits::DEFAULT).map_err(IoError::Format)?;
let (dxf, notes) = craft_dwg::decode(bytes, craft_dwg::Limits::DEFAULT).map_err(dwg_error)?;
let mut d = dxf_read::read(&dxf).map_err(|e| IoError::Format(format!("{e} (in the DXF converted from this DWG)")))?;
d.read_notes = notes;
d.read_notes = notes.into_iter().map(|note| cadcraft_dwg_message(&note)).collect();
return Ok((d, Some(dxf)));
}
let d = match (Format::from_path(name), ext(name).as_str()) {
@@ -146,7 +159,7 @@ pub fn write_noted(d: &Drawing, name: impl AsRef<Path>, opts: &SaveOptions) -> R
/// reads them as that version.
fn write_dwg(d: &Drawing, version: Version) -> Result<(Vec<u8>, craft_dwg::Unwritten)> {
let bridge = dxf_write::dwg_bridge(d, version);
craft_dwg::encode_kept_noted(bridge.as_bytes(), version.code(), d.foreign.version.as_deref()).map_err(IoError::Format)
craft_dwg::encode_kept_noted(bridge.as_bytes(), version.code(), d.foreign.version.as_deref()).map_err(dwg_error)
}
/// Like [`write`], but image formats (PNG, SVG, PDF) show `window` (a model-space rectangle in
+21 -1
View File
@@ -1518,7 +1518,7 @@ fn oversized_dwg_and_dxf_are_rejected_early() {
let limits = craft_dwg::Limits { max_dwg_bytes: 100_000, ..craft_dwg::Limits::DEFAULT };
let e = craft_dwg::dwg_to_dxf_with(&dwg, limits).unwrap_err();
assert!(e.starts_with("DWG: the file is 0.") && e.ends_with("MB, larger than the 0.1 MB CADCraft opens"), "{e}");
assert!(e.starts_with("DWG: the file is 0.") && e.ends_with("MB, larger than the 0.1 MB accepted by this bridge"), "{e}");
// Lines that aren't even group codes: the size is reported before anything is parsed.
let text = "not a group code\nx\n".repeat(60);
@@ -1526,6 +1526,26 @@ fn oversized_dwg_and_dxf_are_rejected_early() {
assert_eq!(e.to_string(), "drawing too large: 60 DXF group codes, more than the 50 CADCraft reads (set CADCRAFT_MAX_DXF_TAGS to read more)");
}
#[test]
fn shared_dwg_diagnostics_keep_cadcraft_vocabulary() {
assert_eq!(
cadcraft_dwg_message("DWG: the file is 0.1 MB, larger than the 0.1 MB accepted by this bridge"),
"DWG: the file is 0.1 MB, larger than the 0.1 MB CADCraft opens"
);
assert_eq!(
cadcraft_dwg_message("DWG: 2 objects of types this bridge can't preserve were left out (FOO × 2)"),
"DWG: 2 objects of types CADCraft doesn't keep were left out (FOO × 2)"
);
assert_eq!(
cadcraft_dwg_message("DWG version `AC1015` can't be written (this bridge writes AC1018+)"),
"DWG version `AC1015` can't be written (CADCraft writes AC1018+)"
);
assert_eq!(
cadcraft_dwg_message("DXF: the input is 1024 MB, larger than the 1024 MB accepted for DWG conversion"),
"DXF: the input is 1024 MB, larger than the 1024 MB CADCraft converts to DWG"
);
}
/// Issue #800: one unreadable object record doesn't fail the whole DWG. The reader skips it, the
/// rest opens, and the drawing's read notes say how many records were left out and of which
/// types, so a partial read never passes for a complete one. A complete read has no notes.