use crate::traits::{LayoutBase, LayoutEdit};
use iron_shapes::CoordinateType;
use std::borrow::Borrow;
pub fn copy_shapes<LS, LT, C>(
target_layout: &mut LT, target_cell: <::CellId, target_layer: <::LayerId,
source_layout: &LS, source_cell: &LS::CellId, source_layer: &LS::LayerId,
)
where C: CoordinateType,
LS: LayoutBase<Coord=C>,
LT: LayoutEdit<Coord=C>,
{
source_layout.for_each_shape(source_cell, source_layer, |_, g| {
let g2 = g.clone();
target_layout.insert_shape(target_cell, target_layer, g2);
})
}
pub fn copy_shapes_all_layers<LS, LT, C>(
target_layout: &mut LT, target_cell: <::CellId,
source_layout: &LS, source_cell: &LS::CellId,
)
where C: CoordinateType,
LS: LayoutBase<Coord=C>,
LT: LayoutEdit<Coord=C>,
{
for source_layer in source_layout.each_layer() {
let layer_info = source_layout.layer_info(&source_layer);
let target_layer = target_layout.find_or_create_layer(layer_info.index, layer_info.datatype);
copy_shapes(target_layout, target_cell, &target_layer,
source_layout, source_cell, &source_layer)
}
}
pub fn copy_all_layers<LS, LT>(
target_layout: &mut LT,
source_layout: &LS,
)
where LS: LayoutBase,
LT: LayoutEdit,
{
for l in source_layout.each_layer() {
copy_layer(target_layout, source_layout, &l);
}
}
pub fn copy_layer<LS, LT>(
target_layout: &mut LT,
source_layout: &LS, source_layer: &LS::LayerId,
) -> LT::LayerId
where LS: LayoutBase,
LT: LayoutEdit,
{
let layer_info = source_layout.layer_info(source_layer);
let layer_id = target_layout.create_layer(layer_info.index, layer_info.datatype);
let layer_name = layer_info.name.as_ref()
.map(|n| {
let s: &str = n.borrow();
s.to_string().into()
});
target_layout.set_layer_name(&layer_id, layer_name);
layer_id
}
pub trait LayoutEditUtil: LayoutEdit {
fn find_or_create_layer(&mut self, index: u32, datatype: u32) -> Self::LayerId {
self.find_layer(index, datatype)
.unwrap_or_else(|| self.create_layer(index, datatype))
}
}
impl<L: LayoutEdit> LayoutEditUtil for L {}