11use serde:: Serialize ;
22use std:: cmp:: Ordering ;
33
4- use super :: types:: { DfSubType , DfType } ;
4+ use super :: types:: { DfSubType , DfType , HasDevilFruit } ;
55use crate :: types:: UrlTyped ;
66
77#[ derive( Debug , Serialize , Default ) ]
@@ -22,6 +22,23 @@ impl std::fmt::Display for DfTypeInfo {
2222 }
2323}
2424
25+ #[ derive( Debug ) ]
26+ pub struct DevilFruitName {
27+ name : String ,
28+ en_name : String ,
29+ description : String ,
30+ }
31+
32+ impl DevilFruitName {
33+ pub fn new ( name : String , en_name : String , description : String ) -> Self {
34+ Self {
35+ name,
36+ en_name,
37+ description,
38+ }
39+ }
40+ }
41+
2542#[ derive( Debug , Serialize ) ]
2643pub struct DevilFruit {
2744 pub df_type : DfType ,
@@ -33,6 +50,48 @@ pub struct DevilFruit {
3350 pub df_url : String ,
3451}
3552
53+ impl DevilFruit {
54+ pub fn zoan (
55+ sub_type : Option < DfSubType > ,
56+ name_detail : DevilFruitName ,
57+ pic_url : String ,
58+ df_url : String ,
59+ ) -> Self {
60+ Self {
61+ df_type : DfType :: Zoan ,
62+ df_sub_type : sub_type,
63+ name : name_detail. name ,
64+ en_name : name_detail. en_name ,
65+ description : name_detail. description ,
66+ pic_url,
67+ df_url,
68+ }
69+ }
70+
71+ pub fn non_zoan (
72+ df_type : DfType ,
73+ name_detail : DevilFruitName ,
74+ pic_url : String ,
75+ df_url : String ,
76+ ) -> Self {
77+ Self {
78+ df_type,
79+ df_sub_type : None ,
80+ name : name_detail. name ,
81+ en_name : name_detail. en_name ,
82+ description : name_detail. description ,
83+ pic_url,
84+ df_url,
85+ }
86+ }
87+ }
88+
89+ impl HasDevilFruit for DevilFruit {
90+ fn df_type ( & self ) -> DfType {
91+ self . df_type
92+ }
93+ }
94+
3695impl std:: fmt:: Display for DevilFruit {
3796 fn fmt ( & self , f : & mut std:: fmt:: Formatter ) -> std:: fmt:: Result {
3897 write ! (
@@ -92,12 +151,12 @@ mod tests {
92151 use crate :: {
93152 df:: {
94153 models:: DfTypeInfo ,
95- types:: { DfSubType , DfType } ,
154+ types:: { DfSubType , DfType , HasDevilFruit } ,
96155 } ,
97156 types:: UrlTyped ,
98157 } ;
99158
100- use super :: { Character , DevilFruit } ;
159+ use super :: { Character , DevilFruit , DevilFruitName } ;
101160
102161 #[ test]
103162 fn df_has_valid_traits ( ) {
@@ -146,6 +205,7 @@ mod tests {
146205 format!( "(df_type: {}, df_sub_type: {:?}, name: {}, english name: {}, pic: {}, url: {}, description: {})" ,
147206 df1. df_type, df1. df_sub_type, df1. name, df1. en_name, df1. pic_url, df1. df_url, df1. description
148207 ) ) ;
208+ assert_eq ! ( DfType :: Zoan , df3. df_type( ) ) ;
149209 }
150210
151211 #[ test]
@@ -157,4 +217,30 @@ mod tests {
157217 } ;
158218 assert_eq ! ( character. get_path( ) , "/wiki/Character:1234" ) ;
159219 }
220+
221+ #[ test]
222+ fn df_constructor_tests ( ) {
223+ let df = DevilFruit :: zoan (
224+ Some ( DfSubType :: MythicalZoan ) ,
225+ DevilFruitName :: new (
226+ "fruito" . to_string ( ) ,
227+ "fruit" . to_string ( ) ,
228+ "lorem" . to_string ( ) ,
229+ ) ,
230+ "" . to_string ( ) ,
231+ "" . to_string ( ) ,
232+ ) ;
233+ assert_eq ! ( "fruit" , df. en_name) ;
234+ let df = DevilFruit :: non_zoan (
235+ DfType :: Logia ,
236+ DevilFruitName :: new (
237+ "ananas" . to_string ( ) ,
238+ "pinaple" . to_string ( ) ,
239+ "lorem" . to_string ( ) ,
240+ ) ,
241+ "" . to_string ( ) ,
242+ "" . to_string ( ) ,
243+ ) ;
244+ assert_eq ! ( "ananas" , df. name) ;
245+ }
160246}
0 commit comments