added IntoOwned trait and derive macro
This commit is contained in:
parent
055900d3e0
commit
9b41a9844f
8 changed files with 268 additions and 107 deletions
|
|
@ -1,5 +1,6 @@
|
|||
use chrono::{serde::ts_seconds, DateTime, Utc};
|
||||
use serde::Deserialize;
|
||||
use torn_api_macros::IntoOwned;
|
||||
|
||||
use crate::de_util;
|
||||
|
||||
|
|
@ -36,7 +37,7 @@ pub enum StateColour {
|
|||
Blue,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[derive(Debug, IntoOwned, Deserialize)]
|
||||
pub struct Status<'a> {
|
||||
pub description: &'a str,
|
||||
#[serde(deserialize_with = "de_util::empty_string_is_none")]
|
||||
|
|
|
|||
|
|
@ -3,7 +3,7 @@ use std::collections::{BTreeMap, HashMap};
|
|||
use chrono::{DateTime, Utc};
|
||||
use serde::Deserialize;
|
||||
|
||||
use torn_api_macros::ApiCategory;
|
||||
use torn_api_macros::{ApiCategory, IntoOwned};
|
||||
|
||||
use crate::de_util::{self, null_is_empty_dict};
|
||||
|
||||
|
|
@ -32,7 +32,7 @@ pub enum FactionSelection {
|
|||
|
||||
pub type Selection = FactionSelection;
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[derive(Debug, IntoOwned, Deserialize)]
|
||||
pub struct Member<'a> {
|
||||
pub name: &'a str,
|
||||
pub level: i16,
|
||||
|
|
@ -42,7 +42,7 @@ pub struct Member<'a> {
|
|||
pub last_action: LastAction,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[derive(Debug, IntoOwned, Deserialize)]
|
||||
pub struct FactionTerritoryWar<'a> {
|
||||
pub territory_war_id: i32,
|
||||
pub territory: &'a str,
|
||||
|
|
@ -58,7 +58,7 @@ pub struct FactionTerritoryWar<'a> {
|
|||
pub end_time: DateTime<Utc>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[derive(Debug, IntoOwned, Deserialize)]
|
||||
pub struct Basic<'a> {
|
||||
#[serde(rename = "ID")]
|
||||
pub id: i32,
|
||||
|
|
|
|||
79
torn-api/src/into_owned.rs
Normal file
79
torn-api/src/into_owned.rs
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
pub use torn_api_macros::IntoOwned;
|
||||
|
||||
pub trait IntoOwned {
|
||||
type Owned;
|
||||
|
||||
fn into_owned(self) -> Self::Owned;
|
||||
}
|
||||
|
||||
impl<T> IntoOwned for Option<T>
|
||||
where
|
||||
T: IntoOwned,
|
||||
{
|
||||
type Owned = Option<T::Owned>;
|
||||
|
||||
fn into_owned(self) -> Self::Owned {
|
||||
self.map(IntoOwned::into_owned)
|
||||
}
|
||||
}
|
||||
|
||||
impl<T> IntoOwned for Vec<T> where T: IntoOwned {
|
||||
type Owned = Vec<<T as IntoOwned>::Owned>;
|
||||
|
||||
fn into_owned(self) -> Self::Owned {
|
||||
let mut owned = Vec::with_capacity(self.len());
|
||||
for elem in self {
|
||||
owned.push(elem.into_owned());
|
||||
}
|
||||
owned
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V> IntoOwned for std::collections::HashMap<K, V> where V: IntoOwned, K: Eq + std::hash::Hash {
|
||||
type Owned = std::collections::HashMap<K, <V as IntoOwned>::Owned>;
|
||||
|
||||
fn into_owned(self) -> Self::Owned {
|
||||
self.into_iter().map(|(k, v)| (k, v.into_owned())).collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<K, V> IntoOwned for std::collections::BTreeMap<K, V> where V: IntoOwned, K: Eq + Ord + std::hash::Hash {
|
||||
type Owned = std::collections::BTreeMap<K, <V as IntoOwned>::Owned>;
|
||||
|
||||
fn into_owned(self) -> Self::Owned {
|
||||
self.into_iter().map(|(k, v)| (k, v.into_owned())).collect()
|
||||
}
|
||||
}
|
||||
|
||||
impl<Z> IntoOwned for chrono::DateTime<Z> where Z: chrono::TimeZone {
|
||||
type Owned = Self;
|
||||
|
||||
fn into_owned(self) -> Self::Owned {
|
||||
self
|
||||
}
|
||||
}
|
||||
|
||||
impl<'a> IntoOwned for &'a str {
|
||||
type Owned = String;
|
||||
|
||||
fn into_owned(self) -> Self::Owned {
|
||||
self.to_owned()
|
||||
}
|
||||
}
|
||||
|
||||
macro_rules! impl_ident {
|
||||
($name:path) => {
|
||||
impl IntoOwned for $name {
|
||||
type Owned = $name;
|
||||
fn into_owned(self) -> Self::Owned {
|
||||
self
|
||||
}
|
||||
}
|
||||
};
|
||||
}
|
||||
|
||||
impl_ident!(i64);
|
||||
impl_ident!(i32);
|
||||
impl_ident!(i16);
|
||||
impl_ident!(i8);
|
||||
impl_ident!(String);
|
||||
|
|
@ -1,5 +1,6 @@
|
|||
#![warn(clippy::all, clippy::perf, clippy::style, clippy::suspicious)]
|
||||
|
||||
pub mod into_owned;
|
||||
pub mod local;
|
||||
pub mod send;
|
||||
|
||||
|
|
@ -32,6 +33,8 @@ use chrono::{DateTime, Utc};
|
|||
use serde::{de::Error as DeError, Deserialize};
|
||||
use thiserror::Error;
|
||||
|
||||
pub use into_owned::IntoOwned;
|
||||
|
||||
pub struct ApiResponse {
|
||||
pub value: serde_json::Value,
|
||||
}
|
||||
|
|
@ -156,7 +159,7 @@ where
|
|||
from: None,
|
||||
to: None,
|
||||
comment: None,
|
||||
phantom: std::marker::PhantomData::default(),
|
||||
phantom: Default::default(),
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
|
|||
|
|
@ -4,7 +4,7 @@ use serde::{
|
|||
};
|
||||
use std::collections::{BTreeMap, HashMap};
|
||||
|
||||
use torn_api_macros::ApiCategory;
|
||||
use torn_api_macros::{ApiCategory, IntoOwned};
|
||||
|
||||
use crate::de_util;
|
||||
|
||||
|
|
@ -41,7 +41,7 @@ pub enum Gender {
|
|||
Enby,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, IntoOwned)]
|
||||
pub struct Faction<'a> {
|
||||
pub faction_id: i32,
|
||||
pub faction_name: &'a str,
|
||||
|
|
@ -133,7 +133,7 @@ where
|
|||
deserializer.deserialize_struct("Faction", FIELDS, FactionVisitor)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[derive(Debug, IntoOwned, Deserialize)]
|
||||
pub struct Basic<'a> {
|
||||
pub player_id: i32,
|
||||
pub name: &'a str,
|
||||
|
|
@ -142,7 +142,8 @@ pub struct Basic<'a> {
|
|||
pub status: Status<'a>,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, PartialEq, Eq, Deserialize)]
|
||||
#[derive(Debug, Clone, IntoOwned, PartialEq, Eq, Deserialize)]
|
||||
#[into_owned(identity)]
|
||||
pub struct Discord {
|
||||
#[serde(rename = "userID")]
|
||||
pub user_id: i32,
|
||||
|
|
@ -188,7 +189,8 @@ pub enum EliminationTeam {
|
|||
CapsLockCrew,
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone)]
|
||||
#[derive(Debug, Clone, IntoOwned)]
|
||||
#[into_owned(identity)]
|
||||
pub enum Competition {
|
||||
Elimination {
|
||||
score: i32,
|
||||
|
|
@ -327,7 +329,7 @@ where
|
|||
deserializer.deserialize_option(CompetitionVisitor)
|
||||
}
|
||||
|
||||
#[derive(Debug, Clone, Deserialize)]
|
||||
#[derive(Debug, IntoOwned, Deserialize)]
|
||||
pub struct Profile<'a> {
|
||||
pub player_id: i32,
|
||||
pub name: &'a str,
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue