Skip to content

Commit

Permalink
Add From<> for compatible types for PrimaryKeys
Browse files Browse the repository at this point in the history
  • Loading branch information
Gtker committed Oct 16, 2022
1 parent b53d605 commit 49e935a
Show file tree
Hide file tree
Showing 540 changed files with 12,989 additions and 18 deletions.
45 changes: 30 additions & 15 deletions rxml/src/rust_printer/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -139,11 +139,9 @@ fn create_row(s: &mut Writer, d: &DbcDescription, o: &Objects) {
}

fn create_primary_keys(s: &mut Writer, d: &DbcDescription) {
for key in d.primary_keys() {
let native_ty = match key.ty() {
Type::PrimaryKey { ty, .. } => ty.rust_str(),
_ => unreachable!(),
};
for (key, ty) in d.primary_keys() {
let native_ty = ty.rust_str();

if not_pascal_case_name(d.name()) {
s.wln("#[allow(non_camel_case_types)]");
}
Expand All @@ -161,17 +159,34 @@ fn create_primary_keys(s: &mut Writer, d: &DbcDescription) {
);
});

s.bodyn(
format!(
"impl From<{native_ty}> for {primary_key}",
primary_key = key.ty().rust_str()
),
|s| {
s.bodyn(format!("fn from(v: {native_ty}) -> Self"), |s| {
create_primary_key_froms(s, key, ty);
}
}

fn create_primary_key_froms(s: &mut Writer, key: &Field, ty: &Type) {
let primary_key = key.ty().rust_str();

let from_tys = match ty {
Type::I8 => [Type::I8].as_slice(),
Type::I16 => [Type::I8, Type::I16, Type::U8].as_slice(),
Type::I32 => [Type::I8, Type::I16, Type::I32, Type::U8, Type::U16].as_slice(),
Type::U8 => [Type::U8].as_slice(),
Type::U16 => [Type::U8, Type::U16].as_slice(),
Type::U32 => [Type::U8, Type::U16, Type::U32].as_slice(),
_ => unreachable!("invalid primary key"),
};

for t in from_tys {
let t = t.rust_str();
s.bodyn(format!("impl From<{t}> for {primary_key}",), |s| {
s.bodyn(format!("fn from(v: {t}) -> Self"), |s| {
if t == ty.rust_str() {
s.wln("Self::new(v)");
});
},
);
} else {
s.wln("Self::new(v.into())");
}
});
});
}
}

Expand Down
10 changes: 8 additions & 2 deletions rxml/src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,16 @@ impl DbcDescription {
&self.flags
}

pub fn primary_keys(&self) -> Vec<&Field> {
pub fn primary_keys(&self) -> Vec<(&Field, &Type)> {
self.fields
.iter()
.filter(|a| matches!(a.ty(), Type::PrimaryKey { .. }))
.filter_map(|a| {
if let Type::PrimaryKey { ty, .. } = a.ty() {
Some((a, ty.as_ref()))
} else {
None
}
})
.collect()
}

Expand Down
2 changes: 1 addition & 1 deletion wow_dbc/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Added

* `Hash`, `PartialOrd`, `Ord`, `Default` traits for types that support it.
* `From<{native_ty}` for primary keys.
* `From<{native_ty}` and all integer types with a `From<{native_ty}>` for primary keys.

### Changed

Expand Down
28 changes: 28 additions & 0 deletions wow_dbc/src/tbc_tables/animation_data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -185,13 +185,41 @@ impl AnimationDataKey {

}

impl From<i8> for AnimationDataKey {
fn from(v: i8) -> Self {
Self::new(v.into())
}

}

impl From<i16> for AnimationDataKey {
fn from(v: i16) -> Self {
Self::new(v.into())
}

}

impl From<i32> for AnimationDataKey {
fn from(v: i32) -> Self {
Self::new(v)
}

}

impl From<u8> for AnimationDataKey {
fn from(v: u8) -> Self {
Self::new(v.into())
}

}

impl From<u16> for AnimationDataKey {
fn from(v: u16) -> Self {
Self::new(v.into())
}

}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AnimationDataRow {
pub id: AnimationDataKey,
Expand Down
28 changes: 28 additions & 0 deletions wow_dbc/src/tbc_tables/area_poi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -213,13 +213,41 @@ impl AreaPOIKey {

}

impl From<i8> for AreaPOIKey {
fn from(v: i8) -> Self {
Self::new(v.into())
}

}

impl From<i16> for AreaPOIKey {
fn from(v: i16) -> Self {
Self::new(v.into())
}

}

impl From<i32> for AreaPOIKey {
fn from(v: i32) -> Self {
Self::new(v)
}

}

impl From<u8> for AreaPOIKey {
fn from(v: u8) -> Self {
Self::new(v.into())
}

}

impl From<u16> for AreaPOIKey {
fn from(v: u16) -> Self {
Self::new(v.into())
}

}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct AreaPOIRow {
pub id: AreaPOIKey,
Expand Down
28 changes: 28 additions & 0 deletions wow_dbc/src/tbc_tables/area_table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,13 +248,41 @@ impl AreaTableKey {

}

impl From<i8> for AreaTableKey {
fn from(v: i8) -> Self {
Self::new(v.into())
}

}

impl From<i16> for AreaTableKey {
fn from(v: i16) -> Self {
Self::new(v.into())
}

}

impl From<i32> for AreaTableKey {
fn from(v: i32) -> Self {
Self::new(v)
}

}

impl From<u8> for AreaTableKey {
fn from(v: u8) -> Self {
Self::new(v.into())
}

}

impl From<u16> for AreaTableKey {
fn from(v: u16) -> Self {
Self::new(v.into())
}

}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct AreaTableRow {
pub id: AreaTableKey,
Expand Down
28 changes: 28 additions & 0 deletions wow_dbc/src/tbc_tables/area_trigger.rs
Original file line number Diff line number Diff line change
Expand Up @@ -162,13 +162,41 @@ impl AreaTriggerKey {

}

impl From<i8> for AreaTriggerKey {
fn from(v: i8) -> Self {
Self::new(v.into())
}

}

impl From<i16> for AreaTriggerKey {
fn from(v: i16) -> Self {
Self::new(v.into())
}

}

impl From<i32> for AreaTriggerKey {
fn from(v: i32) -> Self {
Self::new(v)
}

}

impl From<u8> for AreaTriggerKey {
fn from(v: u8) -> Self {
Self::new(v.into())
}

}

impl From<u16> for AreaTriggerKey {
fn from(v: u16) -> Self {
Self::new(v.into())
}

}

#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub struct AreaTriggerRow {
pub id: AreaTriggerKey,
Expand Down
28 changes: 28 additions & 0 deletions wow_dbc/src/tbc_tables/attack_anim_kits.rs
Original file line number Diff line number Diff line change
Expand Up @@ -137,13 +137,41 @@ impl AttackAnimKitsKey {

}

impl From<i8> for AttackAnimKitsKey {
fn from(v: i8) -> Self {
Self::new(v.into())
}

}

impl From<i16> for AttackAnimKitsKey {
fn from(v: i16) -> Self {
Self::new(v.into())
}

}

impl From<i32> for AttackAnimKitsKey {
fn from(v: i32) -> Self {
Self::new(v)
}

}

impl From<u8> for AttackAnimKitsKey {
fn from(v: u8) -> Self {
Self::new(v.into())
}

}

impl From<u16> for AttackAnimKitsKey {
fn from(v: u16) -> Self {
Self::new(v.into())
}

}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AttackAnimKitsRow {
pub id: AttackAnimKitsKey,
Expand Down
28 changes: 28 additions & 0 deletions wow_dbc/src/tbc_tables/auction_house.rs
Original file line number Diff line number Diff line change
Expand Up @@ -164,13 +164,41 @@ impl AuctionHouseKey {

}

impl From<i8> for AuctionHouseKey {
fn from(v: i8) -> Self {
Self::new(v.into())
}

}

impl From<i16> for AuctionHouseKey {
fn from(v: i16) -> Self {
Self::new(v.into())
}

}

impl From<i32> for AuctionHouseKey {
fn from(v: i32) -> Self {
Self::new(v)
}

}

impl From<u8> for AuctionHouseKey {
fn from(v: u8) -> Self {
Self::new(v.into())
}

}

impl From<u16> for AuctionHouseKey {
fn from(v: u16) -> Self {
Self::new(v.into())
}

}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct AuctionHouseRow {
pub id: AuctionHouseKey,
Expand Down
28 changes: 28 additions & 0 deletions wow_dbc/src/tbc_tables/bank_bag_slot_prices.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,13 +116,41 @@ impl BankBagSlotPricesKey {

}

impl From<i8> for BankBagSlotPricesKey {
fn from(v: i8) -> Self {
Self::new(v.into())
}

}

impl From<i16> for BankBagSlotPricesKey {
fn from(v: i16) -> Self {
Self::new(v.into())
}

}

impl From<i32> for BankBagSlotPricesKey {
fn from(v: i32) -> Self {
Self::new(v)
}

}

impl From<u8> for BankBagSlotPricesKey {
fn from(v: u8) -> Self {
Self::new(v.into())
}

}

impl From<u16> for BankBagSlotPricesKey {
fn from(v: u16) -> Self {
Self::new(v.into())
}

}

#[derive(Debug, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)]
pub struct BankBagSlotPricesRow {
pub id: BankBagSlotPricesKey,
Expand Down
Loading

0 comments on commit 49e935a

Please sign in to comment.