style: apply cargo fmt across workspace

This commit is contained in:
Karim Jerbi committed 2026-10-11 03:14:29 +01:00
1 parent 85710e5454
commit 9cf9ff2c5c
531 files changed
+57740 -33906

No files matched your search

+147 -174
View File
@@ -97,7 +97,10 @@ pub fn set_entity_annotative(doc: &mut CadDocument, handle: Handle, want: bool)
/// Derive the per-scale context payload for an entity from its current
/// placement. Returns the concrete class name and the context kind, or `None`
/// for entity types that do not carry a per-object annotation context.
fn dimension_context_for(doc: &CadDocument, dimension: &codec::entities::Dimension) -> Option<DimContext> {
fn dimension_context_for(
doc: &CadDocument,
dimension: &codec::entities::Dimension,
) -> Option<DimContext> {
use codec::entities::Dimension;
let subtype = match dimension {
@@ -238,7 +241,10 @@ fn context_kind_for(
)),
EntityType::Dimension(dimension) => {
let context = dimension_context_for(doc, dimension)?;
Some((context.subtype.class_name(), ObjectContextKind::Dim(context)))
Some((
context.subtype.class_name(),
ObjectContextKind::Dim(context),
))
}
EntityType::MultiLeader(mleader) => Some((
"ACDB_MLEADEROBJECTCONTEXTDATA_CLASS",
@@ -424,7 +430,9 @@ pub fn create_annotation_context(
}
// The first representation created is the default (native) one.
let is_default = as_dict(doc, coll_h).map(|d| d.entries.is_empty()).unwrap_or(true);
let is_default = as_dict(doc, coll_h)
.map(|d| d.entries.is_empty())
.unwrap_or(true);
let n = as_dict(doc, coll_h).map(|d| d.entries.len()).unwrap_or(0) + 1;
let key = format!("*A{n}");
@@ -478,9 +486,9 @@ pub fn annotative_offscale_for(
}
match scale_handle {
Some(handle) => !scales.iter().any(|(_, member)| *member == handle),
None => !scales.iter().any(|(name, _)| {
name.eq_ignore_ascii_case(&doc.header.current_annotation_scale)
}),
None => !scales
.iter()
.any(|(name, _)| name.eq_ignore_ascii_case(&doc.header.current_annotation_scale)),
}
}
pub(crate) fn annotation_scale_handles_for_entity(
@@ -493,20 +501,19 @@ pub(crate) fn annotation_scale_handles_for_entity(
.collect()
}
pub fn scale_handle_by_name(doc: &CadDocument, name: &str) -> Option<Handle> {
doc.objects.iter().find_map(|(handle, object)| match object {
ObjectType::Scale(scale)
if !scale.is_temporary && scale.name.eq_ignore_ascii_case(name) =>
{
Some(*handle)
}
_ => None,
})
doc.objects
.iter()
.find_map(|(handle, object)| match object {
ObjectType::Scale(scale)
if !scale.is_temporary && scale.name.eq_ignore_ascii_case(name) =>
{
Some(*handle)
}
_ => None,
})
}
pub fn ensure_scale_object(
doc: &mut CadDocument,
source: &codec::objects::Scale,
) -> Handle {
pub fn ensure_scale_object(doc: &mut CadDocument, source: &codec::objects::Scale) -> Handle {
if let Some(handle) = scale_handle_by_name(doc, &source.name) {
return handle;
}
@@ -587,7 +594,9 @@ pub fn remove_annotation_context_for_scale(
// If this is the object's only representation, fully de-annotate it (drop the
// whole chain AND the native flag, like the Yes→No toggle) so it stops
// resolving annotative; otherwise drop just this leaf.
let last = as_dict(doc, coll_h).map(|c| c.entries.len() <= 1).unwrap_or(true);
let last = as_dict(doc, coll_h)
.map(|c| c.entries.len() <= 1)
.unwrap_or(true);
if last {
set_entity_annotative(doc, entity, false);
return true;
@@ -602,7 +611,9 @@ pub fn remove_annotation_context_for_scale(
/// Resolve an entity's `ACDB_ANNOTATIONSCALES` collection dictionary handle, if
/// its context chain exists.
fn annotation_scales_dict(doc: &CadDocument, entity: Handle) -> Option<Handle> {
let xd = doc.get_entity(entity).and_then(|e| e.common().xdictionary_handle)?;
let xd = doc
.get_entity(entity)
.and_then(|e| e.common().xdictionary_handle)?;
let mgr = as_dict(doc, xd).and_then(|d| d.get("AcDbContextDataManager"))?;
as_dict(doc, mgr).and_then(|d| d.get("ACDB_ANNOTATIONSCALES"))
}
@@ -747,12 +758,13 @@ pub fn effective_annotation_scale_for(
};
let active = active_object_context_for_scale(doc, entity.common().handle, scale_handle);
let native = coll.entries.iter().find_map(|(_, leaf_h)| {
match doc.objects.get(leaf_h) {
let native = coll
.entries
.iter()
.find_map(|(_, leaf_h)| match doc.objects.get(leaf_h) {
Some(ObjectType::ObjectContextData(leaf)) if leaf.is_default => Some(leaf),
_ => None,
}
});
});
let (Some(active), Some(native)) = (active, native) else {
return fallback;
};
@@ -938,8 +950,7 @@ fn apply_hatch_context(hatch: &mut codec::entities::Hatch, context: &HatchScaleC
.iter()
.enumerate()
.map(|(index, loop_data)| {
let flags =
codec::entities::BoundaryPathFlags::from_bits(loop_data.loop_type as u32);
let flags = codec::entities::BoundaryPathFlags::from_bits(loop_data.loop_type as u32);
let mut path = if loop_data.supports_context {
base_paths.get(index).cloned()
} else {
@@ -976,8 +987,7 @@ pub fn entity_for_annotation_context<'a>(
entity: &'a EntityType,
scale_handle: Option<Handle>,
) -> Cow<'a, EntityType> {
let Some(context) =
active_object_context_for_scale(doc, entity.common().handle, scale_handle)
let Some(context) = active_object_context_for_scale(doc, entity.common().handle, scale_handle)
else {
return Cow::Borrowed(entity);
};
@@ -1023,10 +1033,7 @@ pub fn entity_for_annotation_context<'a>(
(ObjectContextKind::MLeader(value), EntityType::MultiLeader(mleader)) => {
mleader.context.clone_from(value);
}
(
ObjectContextKind::MTextAttribute(value),
EntityType::AttributeEntity(attribute),
) => {
(ObjectContextKind::MTextAttribute(value), EntityType::AttributeEntity(attribute)) => {
apply_attribute_context(
&mut attribute.insertion_point,
&mut attribute.alignment_point,
@@ -1036,10 +1043,7 @@ pub fn entity_for_annotation_context<'a>(
value,
);
}
(
ObjectContextKind::MTextAttribute(value),
EntityType::AttributeDefinition(attribute),
) => {
(ObjectContextKind::MTextAttribute(value), EntityType::AttributeDefinition(attribute)) => {
apply_attribute_context(
&mut attribute.insertion_point,
&mut attribute.alignment_point,
@@ -1083,9 +1087,9 @@ pub fn entity_for_annotation_context<'a>(
fn sync_mtext_context(context: &mut MTextContext, entity: &codec::entities::MText) {
context.attachment = entity.attachment_point as i32;
context.x_axis_dir = entity.dwg_x_direction.unwrap_or_else(|| {
Vector3::new(entity.rotation.cos(), entity.rotation.sin(), 0.0)
});
context.x_axis_dir = entity
.dwg_x_direction
.unwrap_or_else(|| Vector3::new(entity.rotation.cos(), entity.rotation.sin(), 0.0));
context.insertion = entity.insertion_point;
context.rect_width = entity.rectangle_width;
context.rect_height = entity.rectangle_height.unwrap_or(0.0);
@@ -1200,7 +1204,11 @@ pub fn sync_annotation_context_from_entity(
EntityType::Dimension(dimension) => doc
.block_records
.iter()
.find(|record| record.name.eq_ignore_ascii_case(&dimension.base().block_name))
.find(|record| {
record
.name
.eq_ignore_ascii_case(&dimension.base().block_name)
})
.map(|record| record.handle),
_ => None,
};
@@ -1218,8 +1226,7 @@ pub fn sync_annotation_context_from_entity(
) => {
*rotation = insert.rotation;
*insertion = insert.insert_point;
*scale_factor =
Vector3::new(insert.x_scale(), insert.y_scale(), insert.z_scale());
*scale_factor = Vector3::new(insert.x_scale(), insert.y_scale(), insert.z_scale());
}
(
EntityType::Text(text),
@@ -1252,18 +1259,14 @@ pub fn sync_annotation_context_from_entity(
(EntityType::MultiLeader(mleader), ObjectContextKind::MLeader(context)) => {
context.clone_from(&mleader.context);
}
(
EntityType::AttributeEntity(attribute),
ObjectContextKind::MTextAttribute(context),
) => {
(EntityType::AttributeEntity(attribute), ObjectContextKind::MTextAttribute(context)) => {
context.horizontal_mode = attribute.horizontal_alignment.to_value();
context.rotation = attribute.rotation;
context.insertion =
Vector2::new(attribute.insertion_point.x, attribute.insertion_point.y);
context.alignment =
Vector2::new(attribute.alignment_point.x, attribute.alignment_point.y);
if let (Some(embedded), Some(mtext)) =
(&mut context.context, &attribute.embedded_mtext)
if let (Some(embedded), Some(mtext)) = (&mut context.context, &attribute.embedded_mtext)
{
sync_mtext_context(&mut embedded.mtext, mtext);
}
@@ -1278,8 +1281,7 @@ pub fn sync_annotation_context_from_entity(
Vector2::new(attribute.insertion_point.x, attribute.insertion_point.y);
context.alignment =
Vector2::new(attribute.alignment_point.x, attribute.alignment_point.y);
if let (Some(embedded), Some(mtext)) =
(&mut context.context, &attribute.embedded_mtext)
if let (Some(embedded), Some(mtext)) = (&mut context.context, &attribute.embedded_mtext)
{
sync_mtext_context(&mut embedded.mtext, mtext);
}
@@ -1361,9 +1363,7 @@ pub fn transform_annotation_contexts(
.entries
.iter()
.filter_map(|(_, leaf_handle)| match doc.objects.get(leaf_handle) {
Some(ObjectType::ObjectContextData(leaf)) => {
Some((leaf.handle, leaf.scale))
}
Some(ObjectType::ObjectContextData(leaf)) => Some((leaf.handle, leaf.scale)),
_ => None,
})
.collect()
@@ -1380,8 +1380,10 @@ pub fn transform_annotation_contexts(
// The entity translator keeps the compatibility break list in sync,
// while the complete per-segment list is a separate persisted field.
if let (EntityType::MultiLeader(mleader), crate::command::EntityTransform::Translate(delta)) =
(&mut placed, t)
if let (
EntityType::MultiLeader(mleader),
crate::command::EntityTransform::Translate(delta),
) = (&mut placed, t)
{
let offset = Vector3::new(delta.x, delta.y, delta.z);
for root in &mut mleader.context.leader_roots {
@@ -1402,7 +1404,10 @@ pub fn transform_annotation_contexts(
if let (EntityType::Dimension(placed), EntityType::Dimension(base)) =
(&mut placed, &base_entity)
{
placed.base_mut().block_name.clone_from(&base.base().block_name);
placed
.base_mut()
.block_name
.clone_from(&base.base().block_name);
}
if let Some(entity) = doc.get_entity_mut(entity_handle) {
@@ -1439,14 +1444,16 @@ fn get_or_create_child_dict(doc: &mut CadDocument, parent_h: Handle, key: &str)
/// on it. The shared `SCALE` objects in `ACAD_SCALELIST` are document-level and
/// left intact.
pub fn clear_annotation_context(doc: &mut CadDocument, handle: Handle) {
if let Some(xdict_h) = doc.get_entity(handle).and_then(|e| e.common().xdictionary_handle) {
if let Some(xdict_h) = doc
.get_entity(handle)
.and_then(|e| e.common().xdictionary_handle)
{
// Collect the manager subtree (manager dict, its scales dict, the leaves)
// before mutating, then drop them.
let mut remove = Vec::new();
if let Some(mgr_h) = as_dict(doc, xdict_h).and_then(|d| d.get("AcDbContextDataManager")) {
remove.push(mgr_h);
if let Some(scales_h) =
as_dict(doc, mgr_h).and_then(|d| d.get("ACDB_ANNOTATIONSCALES"))
if let Some(scales_h) = as_dict(doc, mgr_h).and_then(|d| d.get("ACDB_ANNOTATIONSCALES"))
{
remove.push(scales_h);
if let Some(scales) = as_dict(doc, scales_h) {
@@ -1498,9 +1505,9 @@ fn mleader_style_annotative(doc: &CadDocument, handle: Option<Handle>) -> bool {
let Some(h) = handle else {
return false;
};
doc.objects.iter().any(|(oh, o)| {
matches!(o, ObjectType::MultiLeaderStyle(s) if *oh == h && s.is_annotative)
})
doc.objects
.iter()
.any(|(oh, o)| matches!(o, ObjectType::MultiLeaderStyle(s) if *oh == h && s.is_annotative))
}
fn table_style_annotative(doc: &CadDocument, handle: Option<Handle>) -> bool {
@@ -1546,12 +1553,8 @@ fn has_context_manager(doc: &CadDocument, common: &EntityCommon) -> bool {
/// Whether a MULTILEADER participates in annotation scaling through its
/// per-object context or entity flag. A later style edit is applied only by an
/// explicit style update, so it cannot retroactively change existing objects.
pub fn mleader_is_annotative(
doc: &CadDocument,
mleader: &codec::entities::MultiLeader,
) -> bool {
has_context_manager(doc, &mleader.common)
|| mleader.enable_annotation_scale
pub fn mleader_is_annotative(doc: &CadDocument, mleader: &codec::entities::MultiLeader) -> bool {
has_context_manager(doc, &mleader.common) || mleader.enable_annotation_scale
}
pub fn annotation_style_is_annotative(doc: &CadDocument, entity: &EntityType) -> bool {
@@ -1571,9 +1574,7 @@ pub fn annotation_style_is_annotative(doc: &CadDocument, entity: &EntityType) ->
EntityType::Tolerance(tolerance) => {
dim_style_is_annotative(doc, &tolerance.dimension_style_name)
}
EntityType::MultiLeader(leader) => {
mleader_style_annotative(doc, leader.style_handle)
}
EntityType::MultiLeader(leader) => mleader_style_annotative(doc, leader.style_handle),
EntityType::Table(table) => table_style_annotative(doc, table.table_style_handle),
_ => false,
}
@@ -1751,9 +1752,7 @@ fn apply_mleader_style_at_display_scale(
.collect::<Vec<_>>();
apply_mleader_style(entity, style);
let scale = if display_scale.is_finite()
&& display_scale > 1.0e-12
{
let scale = if display_scale.is_finite() && display_scale > 1.0e-12 {
display_scale
} else {
1.0
@@ -1771,10 +1770,7 @@ fn apply_mleader_style_at_display_scale(
};
for (index, root) in entity.context.leader_roots.iter_mut().enumerate() {
root.landing_distance = if entity
.property_override_flags
.contains(F::LANDING_DISTANCE)
{
root.landing_distance = if entity.property_override_flags.contains(F::LANDING_DISTANCE) {
landing_distances
.get(index)
.copied()
@@ -1784,8 +1780,7 @@ fn apply_mleader_style_at_display_scale(
entity.dogleg_length * scale
};
root.text_attachment_direction =
entity.text_attachment_direction;
root.text_attachment_direction = entity.text_attachment_direction;
}
}
@@ -1794,9 +1789,7 @@ pub fn apply_mleader_style_to_object(
handle: Handle,
style: &codec::objects::MultiLeaderStyle,
) -> bool {
let Some(EntityType::MultiLeader(original)) =
doc.get_entity(handle).cloned()
else {
let Some(EntityType::MultiLeader(original)) = doc.get_entity(handle).cloned() else {
return false;
};
@@ -1820,52 +1813,32 @@ pub fn apply_mleader_style_to_object(
let mut styled = original.clone();
apply_mleader_style_at_display_scale(
&mut styled,
style,
base_display_scale,
);
apply_mleader_style_at_display_scale(&mut styled, style, base_display_scale);
if let Some(EntityType::MultiLeader(entity)) =
doc.get_entity_mut(handle)
{
if let Some(EntityType::MultiLeader(entity)) = doc.get_entity_mut(handle) {
*entity = styled;
}
let leaf_handles: Vec<_> =
annotation_scales_dict(doc, handle)
.and_then(|collection| as_dict(doc, collection))
.map(|collection| {
collection
.entries
.iter()
.map(|(_, leaf)| *leaf)
.collect()
})
.unwrap_or_default();
let leaf_handles: Vec<_> = annotation_scales_dict(doc, handle)
.and_then(|collection| as_dict(doc, collection))
.map(|collection| collection.entries.iter().map(|(_, leaf)| *leaf).collect())
.unwrap_or_default();
for leaf_handle in leaf_handles {
let context_before =
match doc.objects.get(&leaf_handle) {
Some(
ObjectType::ObjectContextData(leaf)
) => {
let ObjectContextKind::MLeader(context) =
&leaf.kind
else {
continue;
};
let context_before = match doc.objects.get(&leaf_handle) {
Some(ObjectType::ObjectContextData(leaf)) => {
let ObjectContextKind::MLeader(context) = &leaf.kind else {
continue;
};
context.clone()
}
context.clone()
}
_ => continue,
};
_ => continue,
};
let context_scale =
if context_before.scale_factor.is_finite()
&& context_before.scale_factor > 1.0e-12
{
if context_before.scale_factor.is_finite() && context_before.scale_factor > 1.0e-12 {
context_before.scale_factor
} else {
base_display_scale
@@ -1873,25 +1846,13 @@ pub fn apply_mleader_style_to_object(
let mut per_scale = original.clone();
per_scale.context =
context_before;
per_scale.context = context_before;
apply_mleader_style_at_display_scale(
&mut per_scale,
style,
context_scale,
);
apply_mleader_style_at_display_scale(&mut per_scale, style, context_scale);
if let Some(
ObjectType::ObjectContextData(leaf)
) = doc.objects.get_mut(&leaf_handle)
{
if let ObjectContextKind::MLeader(context) =
&mut leaf.kind
{
context.clone_from(
&per_scale.context,
);
if let Some(ObjectType::ObjectContextData(leaf)) = doc.objects.get_mut(&leaf_handle) {
if let ObjectContextKind::MLeader(context) = &mut leaf.kind {
context.clone_from(&per_scale.context);
}
}
}
@@ -1915,25 +1876,29 @@ pub fn update_entity_from_annotation_style(
return false;
};
let update = match entity {
EntityType::Text(text) => doc.text_styles.get(&text.style).map(|style| {
StyleUpdate::Text {
annotative: style.annotative,
height: style.height,
}
}),
EntityType::MText(text) => doc.text_styles.get(&text.style).map(|style| {
StyleUpdate::Text {
annotative: style.annotative,
height: style.height,
}
}),
EntityType::AttributeEntity(attribute) => doc
EntityType::Text(text) => doc
.text_styles
.get(&attribute.text_style)
.get(&text.style)
.map(|style| StyleUpdate::Text {
annotative: style.annotative,
height: style.height,
}),
EntityType::MText(text) => {
doc.text_styles
.get(&text.style)
.map(|style| StyleUpdate::Text {
annotative: style.annotative,
height: style.height,
})
}
EntityType::AttributeEntity(attribute) => {
doc.text_styles
.get(&attribute.text_style)
.map(|style| StyleUpdate::Text {
annotative: style.annotative,
height: style.height,
})
}
EntityType::AttributeDefinition(attribute) => doc
.text_styles
.get(&attribute.text_style)
@@ -1941,32 +1906,36 @@ pub fn update_entity_from_annotation_style(
annotative: style.annotative,
height: style.height,
}),
EntityType::Dimension(dimension) => doc
.dim_styles
.get(&dimension.base().style_name)
.map(|style| StyleUpdate::Dimension {
annotative: style.annotative,
}),
EntityType::Leader(leader) => doc
.dim_styles
.get(&leader.dimension_style)
.map(|style| StyleUpdate::Dimension {
annotative: style.annotative,
}),
EntityType::Dimension(dimension) => {
doc.dim_styles
.get(&dimension.base().style_name)
.map(|style| StyleUpdate::Dimension {
annotative: style.annotative,
})
}
EntityType::Leader(leader) => {
doc.dim_styles
.get(&leader.dimension_style)
.map(|style| StyleUpdate::Dimension {
annotative: style.annotative,
})
}
EntityType::Tolerance(tolerance) => doc
.dim_styles
.get(&tolerance.dimension_style_name)
.map(|style| StyleUpdate::Dimension {
annotative: style.annotative,
}),
EntityType::MultiLeader(leader) => leader.style_handle.and_then(|style_handle| {
match doc.objects.get(&style_handle) {
Some(ObjectType::MultiLeaderStyle(style)) => {
Some(StyleUpdate::MultiLeader(style.clone()))
}
_ => None,
}
}),
EntityType::MultiLeader(leader) => {
leader
.style_handle
.and_then(|style_handle| match doc.objects.get(&style_handle) {
Some(ObjectType::MultiLeaderStyle(style)) => {
Some(StyleUpdate::MultiLeader(style.clone()))
}
_ => None,
})
}
_ if is_annotative(doc, entity) => Some(StyleUpdate::ContextOnly),
_ => None,
};
@@ -2017,10 +1986,14 @@ pub fn is_annotative(doc: &CadDocument, entity: &EntityType) -> bool {
let standard_marker = xd
.get_record("AcadAnnotative")
.and_then(|record| {
record.values.iter().filter_map(|value| match value {
codec::xdata::XDataValue::Integer16(value) => Some(*value),
_ => None,
}).last()
record
.values
.iter()
.filter_map(|value| match value {
codec::xdata::XDataValue::Integer16(value) => Some(*value),
_ => None,
})
.last()
})
.is_some_and(|value| value != 0);
if standard_marker