Skip to content

Commit

Permalink
Update serde derives. (#56)
Browse files Browse the repository at this point in the history
Since the crate is using Rust edition 2018, we can use the macros
directly without a `macro_use`.

The recommended way to use `serde_derive` now is to use the `derive`
feature on the `serde` crate.
  • Loading branch information
waywardmonkeys authored Mar 6, 2024
1 parent 83a03de commit b9613ed
Show file tree
Hide file tree
Showing 26 changed files with 152 additions and 143 deletions.
5 changes: 2 additions & 3 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ edition = "2018"
targets = []

[features]
serialize = ["serde", "serde_derive"]
serialize = ["serde"]

[dependencies]
byteorder = "1.3.4"
Expand All @@ -25,8 +25,7 @@ enum_primitive = "0.1.1"
image = "0.24"
itertools = "0.11"
num = "0.4"
serde = { version = "1.*.*", optional = true }
serde_derive = { version = "1.*.*", optional = true }
serde = { version = "1.0", features = ["derive"], optional = true }
uuid = { version = "1.3.3", features = ["serde", "v4"] }

[build-dependencies]
Expand Down
14 changes: 10 additions & 4 deletions build/entity_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,9 @@ fn generate_base_entity(fun: &mut String, element: &Element) {
panic!("Expected first entity to be 'Entity'.");
}
fun.push_str("#[derive(Debug, Clone)]\n");
fun.push_str("#[cfg_attr(feature = \"serialize\", derive(Serialize, Deserialize))]\n");
fun.push_str(
"#[cfg_attr(feature = \"serialize\", derive(serde::Serialize, serde::Deserialize))]\n",
);
fun.push_str("pub struct EntityCommon {\n");
for c in &entity.children {
let t = if allow_multiples(c) {
Expand Down Expand Up @@ -107,7 +109,9 @@ fn generate_base_entity(fun: &mut String, element: &Element) {
fun.push('\n');

fun.push_str("#[derive(Debug, Clone)]\n");
fun.push_str("#[cfg_attr(feature = \"serialize\", derive(Serialize, Deserialize))]\n");
fun.push_str(
"#[cfg_attr(feature = \"serialize\", derive(serde::Serialize, serde::Deserialize))]\n",
);
fun.push_str("pub struct Entity {\n");
fun.push_str(" pub common: EntityCommon,\n");
fun.push_str(" pub specific: EntityType,\n");
Expand Down Expand Up @@ -209,7 +213,9 @@ fn generate_base_entity(fun: &mut String, element: &Element) {

fn generate_entity_types(fun: &mut String, element: &Element) {
fun.push_str("#[derive(Clone, Debug, PartialEq)]\n");
fun.push_str("#[cfg_attr(feature = \"serialize\", derive(Serialize, Deserialize))]\n");
fun.push_str(
"#[cfg_attr(feature = \"serialize\", derive(serde::Serialize, serde::Deserialize))]\n",
);
fun.push_str("pub enum EntityType {\n");
for c in &element.children {
if c.name != "Entity" {
Expand All @@ -231,7 +237,7 @@ fn generate_entity_types(fun: &mut String, element: &Element) {
if name(c) != "Entity" {
// definition
fun.push_str("#[derive(Clone, Debug, PartialEq)]\n");
fun.push_str("#[cfg_attr(feature = \"serialize\", derive(Serialize, Deserialize))]\n");
fun.push_str("#[cfg_attr(feature = \"serialize\", derive(serde::Serialize, serde::Deserialize))]\n");
fun.push_str(&format!("pub struct {typ} {{\n", typ = name(c)));
if base_class(c) == "DimensionBase" {
fun.push_str(" pub dimension_base: DimensionBase,\n");
Expand Down
4 changes: 3 additions & 1 deletion build/header_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,7 +60,9 @@ use self::uuid::Uuid;
fn generate_struct(fun: &mut String, element: &Element) {
let mut seen_fields = HashSet::new();
fun.push_str("/// Contains common properties for the DXF file.\n");
fun.push_str("#[cfg_attr(feature = \"serialize\", derive(Serialize, Deserialize))]\n");
fun.push_str(
"#[cfg_attr(feature = \"serialize\", derive(serde::Serialize, serde::Deserialize))]\n",
);
fun.push_str("pub struct Header {\n");
for v in &element.children {
let field_name = field(v);
Expand Down
14 changes: 10 additions & 4 deletions build/object_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,9 @@ fn generate_base_object(fun: &mut String, element: &Element) {
panic!("Expected first object to be 'Object'.");
}
fun.push_str("#[derive(Clone, Debug)]\n");
fun.push_str("#[cfg_attr(feature = \"serialize\", derive(Serialize, Deserialize))]\n");
fun.push_str(
"#[cfg_attr(feature = \"serialize\", derive(serde::Serialize, serde::Deserialize))]\n",
);
fun.push_str("pub struct ObjectCommon {\n");
for c in &object.children {
let t = if allow_multiples(c) {
Expand Down Expand Up @@ -114,7 +116,9 @@ fn generate_base_object(fun: &mut String, element: &Element) {
fun.push('\n');

fun.push_str("#[derive(Clone, Debug)]\n");
fun.push_str("#[cfg_attr(feature = \"serialize\", derive(Serialize, Deserialize))]\n");
fun.push_str(
"#[cfg_attr(feature = \"serialize\", derive(serde::Serialize, serde::Deserialize))]\n",
);
fun.push_str("pub struct Object {\n");
fun.push_str(" pub common: ObjectCommon,\n");
fun.push_str(" pub specific: ObjectType,\n");
Expand Down Expand Up @@ -219,7 +223,9 @@ fn generate_base_object(fun: &mut String, element: &Element) {
fn generate_object_types(fun: &mut String, element: &Element) {
fun.push_str("#[allow(clippy::large_enum_variant)]\n");
fun.push_str("#[derive(Clone, Debug, PartialEq)]\n");
fun.push_str("#[cfg_attr(feature = \"serialize\", derive(Serialize, Deserialize))]\n");
fun.push_str(
"#[cfg_attr(feature = \"serialize\", derive(serde::Serialize, serde::Deserialize))]\n",
);
fun.push_str("pub enum ObjectType {\n");
for c in &element.children {
if c.name != "Object" {
Expand All @@ -241,7 +247,7 @@ fn generate_object_types(fun: &mut String, element: &Element) {
if name(c) != "Object" {
// definition
fun.push_str("#[derive(Clone, Debug, PartialEq)]\n");
fun.push_str("#[cfg_attr(feature = \"serialize\", derive(Serialize, Deserialize))]\n");
fun.push_str("#[cfg_attr(feature = \"serialize\", derive(serde::Serialize, serde::Deserialize))]\n");
fun.push_str(&format!("pub struct {typ} {{\n", typ = name(c)));
for f in &c.children {
let t = if allow_multiples(f) {
Expand Down
4 changes: 3 additions & 1 deletion build/table_generator.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,7 +57,9 @@ fn generate_table_items(fun: &mut String, element: &Element) {
let mut seen_fields = HashSet::new();
let table_item = &table.children[0];
fun.push_str("#[derive(Debug)]\n");
fun.push_str("#[cfg_attr(feature = \"serialize\", derive(Serialize, Deserialize))]\n");
fun.push_str(
"#[cfg_attr(feature = \"serialize\", derive(serde::Serialize, serde::Deserialize))]\n",
);
fun.push_str(&format!("pub struct {name} {{\n", name = name(table_item)));
fun.push_str(" pub name: String,\n");
fun.push_str(" pub handle: Handle,\n");
Expand Down
2 changes: 1 addition & 1 deletion src/block.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::x_data;

/// A block is a collection of entities.
#[derive(Debug, Clone)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Block {
/// The block's handle.
pub handle: Handle,
Expand Down
2 changes: 1 addition & 1 deletion src/class.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ use crate::helper_functions::*;

/// Represents an application-defined class whose instances are `Block`s, `Entity`s, and `Object`s.
#[derive(Clone)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Class {
/// Class DXF record name.
pub record_name: String,
Expand Down
2 changes: 1 addition & 1 deletion src/code_pair.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ use crate::helper_functions::parse_hex_string;
/// The basic primitive of a DXF file; a code indicating the type of the data contained, and the
/// data itself.
#[derive(Clone)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct CodePair {
pub code: i32,
pub value: CodePairValue,
Expand Down
2 changes: 1 addition & 1 deletion src/code_pair_value.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::fmt::{Debug, Display, Formatter};

/// Contains the data portion of a `CodePair`.
#[derive(PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub enum CodePairValue {
Boolean(i16),
Integer(i32),
Expand Down
2 changes: 1 addition & 1 deletion src/color.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use crate::tables::Layer;

/// Represents an indexed color.
#[derive(Clone, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Color {
raw_value: i16,
}
Expand Down
2 changes: 1 addition & 1 deletion src/data_table_value.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{Handle, Point};

#[derive(Clone, Debug, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub enum DataTableValue {
Boolean(bool),
Integer(i32),
Expand Down
2 changes: 1 addition & 1 deletion src/drawing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ use std::path::Path;
pub(crate) const AUTO_REPLACE_HANDLE: Handle = Handle(0xFFFF_FFFF_FFFF_FFFF);

/// Represents a DXF drawing.
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct Drawing {
/// The drawing's header. Contains various drawing-specific values and settings.
pub header: Header,
Expand Down
2 changes: 1 addition & 1 deletion src/entity.rs
Original file line number Diff line number Diff line change
Expand Up @@ -118,7 +118,7 @@ impl Line {
//------------------------------------------------------------------------------
/// Represents a single vertex of a `LwPolyline`.
#[derive(Clone, Copy, Debug, Default, PartialEq)]
#[cfg_attr(feature = "serialize", derive(Serialize, Deserialize))]
#[cfg_attr(feature = "serialize", derive(serde::Serialize, serde::Deserialize))]
pub struct LwPolylineVertex {
pub x: f64,
pub y: f64,
Expand Down
Loading

0 comments on commit b9613ed

Please sign in to comment.