mirror of
https://github.com/penpot/penpot.git
synced 2025-05-30 19:16:10 +02:00
♻️ Remove unused deserialization code
This commit is contained in:
parent
ec73bd640c
commit
8609db2182
5 changed files with 15 additions and 145 deletions
|
@ -17,11 +17,6 @@
|
|||
;; FIXME: get it from the wasm module
|
||||
(def FILL-BYTE-SIZE (+ 4 (max GRADIENT-BYTE-SIZE IMAGE-BYTE-SIZE SOLID-BYTE-SIZE)))
|
||||
|
||||
;; (defn write-fill! [offset heap-u32 fill]
|
||||
;; (let [dview (js/DataView. (.-buffer heap-u32))]
|
||||
;; offset))
|
||||
|
||||
|
||||
(defn write-solid-fill!
|
||||
[offset heap-u32 argb]
|
||||
(let [dview (js/DataView. (.-buffer heap-u32))]
|
||||
|
|
|
@ -7,10 +7,13 @@ use crate::shapes;
|
|||
use crate::with_current_shape;
|
||||
use crate::STATE;
|
||||
|
||||
const RAW_FILL_DATA_SIZE: usize = std::mem::size_of::<RawFillData>();
|
||||
|
||||
#[repr(C)]
|
||||
#[repr(align(4))]
|
||||
#[repr(u8)]
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
#[allow(dead_code)]
|
||||
pub enum RawFillData {
|
||||
Solid(solid::RawSolidData) = 0x00,
|
||||
Linear(gradient::RawGradientData) = 0x01,
|
||||
|
@ -33,29 +36,20 @@ impl From<RawFillData> for shapes::Fill {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<[u8; RAW_FILL_DATA_SIZE]> for RawFillData {
|
||||
fn from(bytes: [u8; RAW_FILL_DATA_SIZE]) -> Self {
|
||||
unsafe { std::mem::transmute(bytes) }
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&[u8]> for RawFillData {
|
||||
type Error = String;
|
||||
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
|
||||
if bytes.len() < std::mem::size_of::<RawFillData>() {
|
||||
return Err("Invalid fill data".to_string());
|
||||
}
|
||||
|
||||
let fill_type = bytes[0];
|
||||
match fill_type {
|
||||
0x00 => Ok(RawFillData::Solid(solid::RawSolidData::try_from(
|
||||
&bytes[4..],
|
||||
)?)),
|
||||
0x01 => Ok(RawFillData::Linear(gradient::RawGradientData::try_from(
|
||||
&bytes[4..],
|
||||
)?)),
|
||||
0x02 => Ok(RawFillData::Radial(gradient::RawGradientData::try_from(
|
||||
&bytes[4..],
|
||||
)?)),
|
||||
0x03 => Ok(RawFillData::Image(image::RawImageFillData::try_from(
|
||||
&bytes[4..],
|
||||
)?)),
|
||||
_ => Err("Invalid fill type".to_string()),
|
||||
}
|
||||
let data: [u8; RAW_FILL_DATA_SIZE] = bytes
|
||||
.get(0..RAW_FILL_DATA_SIZE)
|
||||
.and_then(|slice| slice.try_into().ok())
|
||||
.ok_or("Invalid fill data".to_string())?;
|
||||
Ok(RawFillData::from(data))
|
||||
}
|
||||
}
|
||||
|
||||
|
@ -92,7 +86,7 @@ mod tests {
|
|||
fn test_raw_fill_data_from_bytes_to_solid_fill() {
|
||||
let mut bytes = vec![0x00; std::mem::size_of::<RawFillData>()];
|
||||
bytes[0] = 0x00;
|
||||
bytes[1..=4].copy_from_slice(&0xfffabada_u32.to_le_bytes());
|
||||
bytes[4..8].copy_from_slice(&0xfffabada_u32.to_le_bytes());
|
||||
|
||||
let raw_fill = RawFillData::try_from(&bytes[..]);
|
||||
|
||||
|
|
|
@ -1,9 +1,6 @@
|
|||
use crate::shapes::{Color, Gradient};
|
||||
|
||||
const MAX_GRADIENT_STOPS: usize = 16;
|
||||
const BASE_GRADIENT_DATA_SIZE: usize = 28;
|
||||
const RAW_GRADIENT_DATA_SIZE: usize =
|
||||
BASE_GRADIENT_DATA_SIZE + RAW_STOP_DATA_SIZE * MAX_GRADIENT_STOPS;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
#[repr(C)]
|
||||
|
@ -19,24 +16,6 @@ pub struct RawGradientData {
|
|||
stops: [RawStopData; MAX_GRADIENT_STOPS],
|
||||
}
|
||||
|
||||
impl From<[u8; RAW_GRADIENT_DATA_SIZE]> for RawGradientData {
|
||||
fn from(bytes: [u8; RAW_GRADIENT_DATA_SIZE]) -> Self {
|
||||
unsafe { std::mem::transmute(bytes) }
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&[u8]> for RawGradientData {
|
||||
type Error = String;
|
||||
|
||||
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
|
||||
let data: [u8; RAW_GRADIENT_DATA_SIZE] = bytes
|
||||
.get(0..RAW_GRADIENT_DATA_SIZE)
|
||||
.and_then(|slice| slice.try_into().ok())
|
||||
.ok_or("Invalid gradient fill data".to_string())?;
|
||||
Ok(RawGradientData::from(data))
|
||||
}
|
||||
}
|
||||
|
||||
impl RawGradientData {
|
||||
pub fn start(&self) -> (f32, f32) {
|
||||
(self.start_x, self.start_y)
|
||||
|
@ -47,8 +26,6 @@ impl RawGradientData {
|
|||
}
|
||||
}
|
||||
|
||||
pub const RAW_STOP_DATA_SIZE: usize = 8;
|
||||
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
#[repr(C)]
|
||||
struct RawStopData {
|
||||
|
@ -66,27 +43,6 @@ impl RawStopData {
|
|||
}
|
||||
}
|
||||
|
||||
impl From<[u8; RAW_STOP_DATA_SIZE]> for RawStopData {
|
||||
fn from(bytes: [u8; RAW_STOP_DATA_SIZE]) -> Self {
|
||||
Self {
|
||||
color: u32::from_le_bytes([bytes[0], bytes[1], bytes[2], bytes[3]]),
|
||||
offset: f32::from_le_bytes([bytes[4], bytes[5], bytes[6], bytes[7]]),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// FIXME: We won't need this once we use `array_chunks`. See comment above.
|
||||
impl TryFrom<&[u8]> for RawStopData {
|
||||
type Error = String;
|
||||
|
||||
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
|
||||
let data: [u8; RAW_STOP_DATA_SIZE] = bytes
|
||||
.try_into()
|
||||
.map_err(|_| "Invalid stop data".to_string())?;
|
||||
Ok(RawStopData::from(data))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RawGradientData> for Gradient {
|
||||
fn from(raw_gradient: RawGradientData) -> Self {
|
||||
let stops = raw_gradient
|
||||
|
@ -105,16 +61,3 @@ impl From<RawGradientData> for Gradient {
|
|||
)
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&[u8]> for Gradient {
|
||||
type Error = String;
|
||||
|
||||
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
|
||||
let raw_gradient_bytes: [u8; RAW_GRADIENT_DATA_SIZE] = bytes[0..RAW_GRADIENT_DATA_SIZE]
|
||||
.try_into()
|
||||
.map_err(|_| "Invalid gradient data".to_string())?;
|
||||
let gradient = RawGradientData::from(raw_gradient_bytes).into();
|
||||
|
||||
Ok(gradient)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
use crate::{shapes::ImageFill, utils::uuid_from_u32_quartet};
|
||||
|
||||
const RAW_IMAGE_DATA_SIZE: usize = 28;
|
||||
|
||||
#[derive(Debug, Clone, Copy, PartialEq)]
|
||||
#[repr(C)]
|
||||
#[repr(align(4))]
|
||||
|
@ -23,30 +21,3 @@ impl From<RawImageFillData> for ImageFill {
|
|||
Self::new(id, opacity, value.width, value.height)
|
||||
}
|
||||
}
|
||||
|
||||
impl From<[u8; RAW_IMAGE_DATA_SIZE]> for RawImageFillData {
|
||||
fn from(value: [u8; RAW_IMAGE_DATA_SIZE]) -> Self {
|
||||
unsafe { std::mem::transmute(value) }
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&[u8]> for RawImageFillData {
|
||||
type Error = String;
|
||||
|
||||
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
|
||||
let data: [u8; RAW_IMAGE_DATA_SIZE] = value
|
||||
.get(0..RAW_IMAGE_DATA_SIZE)
|
||||
.and_then(|slice| slice.try_into().ok())
|
||||
.ok_or("Invalid image fill data".to_string())?;
|
||||
Ok(Self::from(data))
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&[u8]> for ImageFill {
|
||||
type Error = String;
|
||||
|
||||
fn try_from(value: &[u8]) -> Result<Self, Self::Error> {
|
||||
let raw_image_data = RawImageFillData::try_from(value)?;
|
||||
Ok(raw_image_data.into())
|
||||
}
|
||||
}
|
||||
|
|
|
@ -1,7 +1,5 @@
|
|||
use crate::shapes::{Color, SolidColor};
|
||||
|
||||
const RAW_SOLID_DATA_SIZE: usize = 4;
|
||||
|
||||
#[repr(C)]
|
||||
#[repr(align(4))]
|
||||
#[derive(Debug, PartialEq, Clone, Copy)]
|
||||
|
@ -9,39 +7,8 @@ pub struct RawSolidData {
|
|||
pub color: u32,
|
||||
}
|
||||
|
||||
impl From<[u8; 4]> for RawSolidData {
|
||||
fn from(value: [u8; RAW_SOLID_DATA_SIZE]) -> Self {
|
||||
unsafe { std::mem::transmute(value) }
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&[u8]> for RawSolidData {
|
||||
type Error = String;
|
||||
|
||||
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
|
||||
let data: [u8; RAW_SOLID_DATA_SIZE] = bytes
|
||||
.get(0..RAW_SOLID_DATA_SIZE)
|
||||
.and_then(|slice| slice.try_into().ok())
|
||||
.ok_or("Invalid solid fill data".to_string())?;
|
||||
Ok(RawSolidData::from(data))
|
||||
}
|
||||
}
|
||||
|
||||
impl From<RawSolidData> for SolidColor {
|
||||
fn from(value: RawSolidData) -> Self {
|
||||
Self(Color::new(value.color))
|
||||
}
|
||||
}
|
||||
|
||||
impl TryFrom<&[u8]> for SolidColor {
|
||||
type Error = String;
|
||||
|
||||
fn try_from(bytes: &[u8]) -> Result<Self, Self::Error> {
|
||||
let raw_solid_bytes: [u8; 4] = bytes[0..4]
|
||||
.try_into()
|
||||
.map_err(|_| "Invalid solid fill data".to_string())?;
|
||||
let color = RawSolidData::from(raw_solid_bytes).into();
|
||||
|
||||
Ok(color)
|
||||
}
|
||||
}
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue