1mod binary;
15mod card;
16mod compressed;
17mod dialect;
18mod directory;
19mod entities;
20mod error;
21mod global;
22mod graph;
23mod ids;
24mod layout;
26mod loss;
27mod native;
28mod parameter;
29mod profile;
30mod reader;
31mod representation;
32mod version;
33mod writer;
34
35#[doc(hidden)]
36pub mod fuzz;
37
38use cadmpeg_core::decode::{DecodeContext, DecodeDepth, View};
39use cadmpeg_core::target::TargetDescriptor;
40use cadmpeg_core::CodecError;
41use cadmpeg_ir::codec::write::{
42 target::{Catalog, ResolvedWrite},
43 DepthNeed, EncodeInput, EncoderBackend, ExportBody,
44};
45use cadmpeg_ir::codec::{CodecBackend, Confidence, Decoded, FormatId};
46use cadmpeg_ir::hash::document_local_sha256;
47use cadmpeg_ir::CadIr;
48use cadmpeg_ir::ContainerSummary;
49
50const SOURCE_IMAGE_ID: &str = "iges:file:source-image#0";
51
52#[derive(Debug, Clone, Copy, PartialEq, Eq)]
54pub enum IgesVersion {
55 V5_3,
57 V5_2,
59 V5_1,
61 V5_0,
63 V4_0,
65}
66
67macro_rules! writer_vocabulary {
68 ($(#[$all_meta:meta])* $count:literal; $($variant:ident),+ $(,)?) => {
69 $(#[$all_meta])*
70 const ALL: [Self; $count] = [$(Self::$variant),+];
71 const TARGETS: &'static [TargetDescriptor] = &[
73 $(Self::$variant.descriptor()),+
74 ];
75 };
76}
77
78impl IgesVersion {
79 writer_vocabulary!(
80 5;
87 V4_0,
88 V5_0,
89 V5_1,
90 V5_2,
91 V5_3
92 );
93
94 #[must_use]
96 pub const fn descriptor(self) -> TargetDescriptor {
97 let aliases = match self {
98 Self::V4_0 => &["4.0"].as_slice(),
99 Self::V5_0 => &["5.0"].as_slice(),
100 Self::V5_1 => &["5.1"].as_slice(),
101 Self::V5_2 => &["5.2"].as_slice(),
102 Self::V5_3 => &["5.3"].as_slice(),
103 };
104 TargetDescriptor {
105 id: dialect::fixed_ascii_id(self),
106 aliases,
107 }
108 }
109
110 const fn name(self) -> &'static str {
111 version::VersionFlag::from_write_version(self).name()
112 }
113
114 const fn global_flag(self) -> u8 {
115 match self {
116 Self::V4_0 => 6,
117 Self::V5_0 => 8,
118 Self::V5_1 => 9,
119 Self::V5_2 => 10,
120 Self::V5_3 => 11,
121 }
122 }
123}
124
125#[derive(Debug, Default, Clone, Copy)]
127pub struct IgesCodec;
128
129fn document_digest(ir: &CadIr) -> Result<String, cadmpeg_core::CodecError> {
130 let arena = cadmpeg_core::decode::DecodeArena::new();
131 let policy = cadmpeg_core::decode::DecodePolicy::default();
132 let (ctx, _) = DecodeContext::from_root_bytes(&[], &arena, &policy)?;
133 let digest = document_local_sha256(&ctx, ir, ir.source.as_ref(), "iges", SOURCE_IMAGE_ID)?;
134 ctx.finish_session()?;
135 Ok(digest)
136}
137
138impl CodecBackend for IgesCodec {
139 const FORMAT: FormatId = FormatId::new(dialect::FORMAT);
140
141 fn detect_impl(
142 &self,
143 _ctx: &cadmpeg_core::decode::DecodeContext<'_>,
144 prefix: cadmpeg_core::decode::View<'_>,
145 ) -> Result<Confidence, cadmpeg_core::CodecError> {
146 let prefix = prefix.window();
147
148 Ok(representation::confidence(prefix))
149 }
150
151 fn detect_whole_impl(
152 &self,
153 _ctx: &cadmpeg_core::decode::DecodeContext<'_>,
154 input: cadmpeg_core::decode::WholeInput<'_>,
155 ) -> Result<Confidence, cadmpeg_core::CodecError> {
156 let bytes = input.view().window();
157 let prefix = representation::confidence(bytes);
158 if prefix > Confidence::No {
159 return Ok(prefix);
160 }
161 Ok(if card::whole_file_fixed_ascii(bytes) {
162 Confidence::Medium
163 } else {
164 Confidence::No
165 })
166 }
167
168 fn inspect_impl(
169 &self,
170 ctx: &DecodeContext<'_>,
171 root: View<'_>,
172 ) -> Result<ContainerSummary, CodecError> {
173 let representation = representation::classify_whole(root.window())?
174 .ok_or_else(|| CodecError::WrongFormat("unrecognized IGES representation".into()))?;
175 match representation {
176 representation::Representation::FixedAscii => {
177 reader::inspect(ctx, root.window(), representation, root.window().len())
178 }
179 representation::Representation::CompressedAscii => {
180 let normalized = compressed::normalize(root.window())?;
181 reader::inspect(ctx, &normalized, representation, root.window().len())
182 }
183 representation::Representation::Binary => {
184 let normalized = binary::normalize(root.window())?;
185 reader::inspect(ctx, &normalized, representation, root.window().len())
186 }
187 }
188 }
189
190 fn decode_impl(&self, ctx: &DecodeContext<'_>, root: View<'_>) -> Result<Decoded, CodecError> {
191 let representation = representation::classify_whole(root.window())?
192 .ok_or_else(|| CodecError::WrongFormat("unrecognized IGES representation".into()))?;
193 match representation {
194 representation::Representation::FixedAscii => {
195 reader::decode(root.window(), root.window(), representation, ctx)
196 }
197 representation::Representation::CompressedAscii => {
198 let normalized = compressed::normalize(root.window())?;
199 reader::decode(&normalized, root.window(), representation, ctx)
200 }
201 representation::Representation::Binary => {
202 let normalized = binary::normalize(root.window())?;
203 reader::decode(&normalized, root.window(), representation, ctx)
204 }
205 }
206 }
207}
208
209impl EncoderBackend for IgesCodec {
210 const FORMAT: FormatId = <Self as CodecBackend>::FORMAT;
211 const NEEDS: DepthNeed = DepthNeed::AtLeast(DecodeDepth::Geometry);
214 type Target = Catalog;
215 const TARGET: Catalog = Catalog::new(IgesVersion::TARGETS, Some(4));
216
217 fn plan_resolved(
218 &self,
219 input: EncodeInput<'_>,
220 target: ResolvedWrite<'_>,
221 ) -> Result<ExportBody, CodecError> {
222 writer::target::plan(input, &target)
223 }
224}
225
226#[cfg(test)]
227mod golden_tests;
228#[cfg(test)]
229mod integration_tests;
230#[cfg(test)]
231mod test_support;