use crate::error::Result;
use crate::io::dxf::GroupCodeValueType;
use crate::types::{Color, Handle, Vector2, Vector3};
pub trait DxfStreamWriter {
fn write_string(&mut self, code: i32, value: &str) -> Result<()>;
fn write_byte(&mut self, code: i32, value: u8) -> Result<()>;
fn write_i16(&mut self, code: i32, value: i16) -> Result<()>;
fn write_i32(&mut self, code: i32, value: i32) -> Result<()>;
fn write_i64(&mut self, code: i32, value: i64) -> Result<()>;
fn write_double(&mut self, code: i32, value: f64) -> Result<()>;
fn write_bool(&mut self, code: i32, value: bool) -> Result<()>;
fn write_handle(&mut self, code: i32, handle: Handle) -> Result<()>;
fn write_binary(&mut self, code: i32, data: &[u8]) -> Result<()>;
fn flush(&mut self) -> Result<()>;
}
pub trait DxfStreamWriterExt: DxfStreamWriter {
#[inline]
fn write_point2d(&mut self, x_code: i32, point: Vector2) -> Result<()> {
self.write_double(x_code, point.x)?;
self.write_double(x_code + 10, point.y)?;
Ok(())
}
#[inline]
fn write_point3d(&mut self, x_code: i32, point: Vector3) -> Result<()> {
self.write_double(x_code, point.x)?;
self.write_double(x_code + 10, point.y)?;
self.write_double(x_code + 20, point.z)?;
Ok(())
}
#[inline]
fn write_color(&mut self, code: i32, color: Color) -> Result<()> {
match color {
Color::ByLayer => self.write_i16(code, 256),
Color::ByBlock => self.write_i16(code, 0),
Color::Index(index) => self.write_i16(code, index as i16),
Color::Rgb { .. } => self.write_i16(code, color.approximate_index()),
}
}
#[inline]
fn write_entity_type(&mut self, entity_type: &str) -> Result<()> {
self.write_string(0, entity_type)
}
#[inline]
fn write_subclass(&mut self, marker: &str) -> Result<()> {
self.write_string(100, marker)
}
#[inline]
fn write_section_start(&mut self, section_name: &str) -> Result<()> {
self.write_string(0, "SECTION")?;
self.write_string(2, section_name)?;
Ok(())
}
#[inline]
fn write_section_end(&mut self) -> Result<()> {
self.write_string(0, "ENDSEC")
}
#[inline]
fn write_eof(&mut self) -> Result<()> {
self.write_string(0, "EOF")
}
}
impl<T: DxfStreamWriter> DxfStreamWriterExt for T {}
pub fn value_type_for_code(code: i32) -> GroupCodeValueType {
use crate::io::dxf::DxfCode;
let dxf_code = DxfCode::from_i32(code);
GroupCodeValueType::from_code(dxf_code)
}