-
Notifications
You must be signed in to change notification settings - Fork 0
/
efinanceira.rs
855 lines (775 loc) · 25.3 KB
/
efinanceira.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
/**
Generates XML file from Multiple XML schemas (xsd files).
This feature requires Apache XMLBeans.
https://xmlbeans.apache.org/docs/2.0.0/guide/tools.html#xsd2inst
xsd2inst (Schema to Instance Tool)
Prints an XML instance from the specified global element using the specified schema.
Download schema xsd file:
wget http://sped.rfb.gov.br/estatico/59/791D5602EF73CE796AD62E07FA8FF6500CBC99/evtMovOpFin-v1_2_1.xsd
XSD to XML:
/home/claudio/Downloads/XMLBeans/xmlbeans-5.2.0/bin/xsd2inst evtMovOpFin-v1_2_1.xsd -name eFinanceira -dl > evtMovOpFin-v1_2_1.xml
Converter XML file to Rust struct:
read_xml -s evtMovOpFin-v1_2_1.xml > evtMovOpFin-v1_2_1.rs
*/
use std::ops::Deref;
use chrono::NaiveDate;
use claudiofsr_lib::StrExtension;
use rust_xlsxwriter::serialize_chrono_option_naive_to_excel;
use serde::{Deserialize, Serialize};
use struct_iterable::Iterable;
use crate::{
Arguments,
Information,
StructExtension,
excel::InfoExtension,
xml_structs::assinaturas::Signature,
get_naive_date_from_yyyymm,
};
// https://stackoverflow.com/questions/29986057/is-there-a-way-to-get-the-field-names-of-a-struct-in-a-macro
// https://danielkeep.github.io/tlborm/book/README.html (The Little Book of Rust Macros)
#[allow(unused_macros)]
macro_rules! my_macro {
(pub struct $name:ident {
$(pub $field_name:ident: $field_type:ty,)*
}) => {
pub struct $name {
pub $($field_name: $field_type,)*
}
impl $name {
// This is purely an example—not a good one.
fn get_field_names() -> Vec<&'static str> {
vec![$(stringify!($field_name)),*]
}
}
}
}
#[derive(Debug, Default, Serialize, Deserialize, Clone, Iterable)]
pub struct InfoEFinanceira {
#[serde(rename = "Identificação da Entidade Declarante")]
pub id: String,
#[serde(rename = "CNPJ da Entidade Declarante")]
pub cnpj_do_declarante: String,
#[serde(rename = "Identificação do declarado")]
pub ni_do_declarado: String,
#[serde(rename = "Nome do declarado")]
pub nome_declarado: String,
#[serde(rename = "Ano/Mês Caixa Reportado", serialize_with = "serialize_chrono_option_naive_to_excel")]
pub ano_mes_caixa: Option<NaiveDate>,
#[serde(rename = "Nº da Conta")]
pub num_conta: String,
#[serde(rename = "País Reportado")]
pub pais_reportado: String,
#[serde(rename = "Balanço da Conta: Total de Créditos")]
pub tot_creditos: Option<f64>,
#[serde(rename = "Balanço da Conta: Total de Débitos")]
pub tot_debitos: Option<f64>,
}
/// <https://doc.rust-lang.org/book/ch10-02-traits.html#default-implementations>
impl InfoExtension for InfoEFinanceira {}
impl InfoEFinanceira {
pub fn get_lengths(&self) -> Vec<usize> {
vec![
self.id.chars_count(),
self.cnpj_do_declarante.chars_count(),
self.ni_do_declarado.chars_count(),
self.nome_declarado.chars_count(),
self.ano_mes_caixa.map(|date| date.to_string().chars_count()).unwrap_or_default(),
self.num_conta.chars_count(),
self.pais_reportado.chars_count(),
self.tot_creditos.map(|f| f.to_string().chars_count()).unwrap_or_default(),
self.tot_debitos.map(|f| f.to_string().chars_count()).unwrap_or_default(),
]
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct EFinanceira {
#[serde(rename = "@versao")]
pub versao: Option<String>,
#[serde(rename = "@xmlns")]
pub xmlns: Option<String>,
#[serde(rename = "$text")]
pub text: Option<String>,
#[serde(rename = "evtMovOpFin")]
pub evt_mov_op_fin: EvtMovOpFin,
#[serde(rename = "Signature")]
pub signature: Signature,
}
/// <https://doc.rust-lang.org/book/ch10-02-traits.html#default-implementations>
impl StructExtension for EFinanceira {
fn get_information(&self, xml_path: &std::path::Path, arguments: &Arguments) -> Information {
if arguments.verbose {
println!("eFinanceira xml_path: {xml_path:?}");
println!("eFinanceira: {self:#?}\n");
}
Information::EFinanceira(self.get_infos())
}
}
impl EFinanceira {
pub fn get_id(&self) -> String {
self
.evt_mov_op_fin
.id
.clone()
}
pub fn get_cnpj_do_declarante(&self) -> String {
self
.evt_mov_op_fin
.ide_declarante
.cnpj_declarante
.trim()
.format_cnpj()
}
pub fn get_ni_do_declarado(&self) -> String {
self
.evt_mov_op_fin
.ide_declarado
.nideclarado
.trim()
.format_cnpj()
}
pub fn get_nome_declarado(&self) -> String {
self
.evt_mov_op_fin
.ide_declarado
.nome_declarado
.replace_multiple_whitespaces()
}
fn get_contas(&self) -> Vec<&Conta> {
self
.evt_mov_op_fin
.mes_caixa
.mov_op_fin
.conta
.as_ref()
.into_iter()
.flatten()
.collect::<Vec<&Conta>>()
}
pub fn get_ano_mes(&self) -> Option<NaiveDate> {
self
.evt_mov_op_fin
.mes_caixa
.get_date_ano_mes_caixa()
}
/// Try to Convert String to Float64.
///
/// "12345,67" -> "12345.67"
fn to_float64<T>(string: T) -> Option<f64>
where
T: Deref<Target=str>,
{
string
.replace(',', ".")
.parse()
.ok()
}
pub fn get_infos(&self) -> Vec<InfoEFinanceira> {
let mut infos = Vec::new();
let contas: Vec<&Conta> = self.get_contas();
for conta in contas {
let num_conta = conta.info_conta.num_conta.clone();
let pais_reportado = conta.info_conta.reportavel.iter().map(|info| info.pais.clone()).collect::<Vec<String>>().join(", ");
let tot_creditos = Self::to_float64(conta.info_conta.balanco_conta.tot_creditos.as_ref());
let tot_debitos = Self::to_float64(conta.info_conta.balanco_conta.tot_debitos.as_ref());
let info_efinanceira = InfoEFinanceira {
id: self.get_id(),
cnpj_do_declarante: self.get_cnpj_do_declarante(),
ni_do_declarado: self.get_ni_do_declarado(),
nome_declarado: self.get_nome_declarado(),
ano_mes_caixa: self.get_ano_mes(),
num_conta,
pais_reportado,
tot_creditos,
tot_debitos,
};
infos.push(info_efinanceira.clone());
}
infos
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct EvtMovOpFin {
#[serde(rename = "@id")]
pub id: String,
#[serde(rename = "$text")]
pub text: Option<String>,
#[serde(rename = "ideEvento")]
pub ide_evento: IdeEvento,
#[serde(rename = "ideDeclarante")]
pub ide_declarante: IdeDeclarante,
#[serde(rename = "ideDeclarado")]
pub ide_declarado: IdeDeclarado,
#[serde(rename = "mesCaixa")]
pub mes_caixa: MesCaixa,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct IdeEvento {
#[serde(rename = "indRetificacao")]
pub ind_retificacao: String,
#[serde(rename = "nrRecibo")]
pub nr_recibo: Option<String>,
#[serde(rename = "tpAmb")]
pub tp_amb: String,
#[serde(rename = "aplicEmi")]
pub aplic_emi: String,
#[serde(rename = "verAplic")]
pub ver_aplic: String,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct IdeDeclarante {
#[serde(rename = "cnpjDeclarante")]
pub cnpj_declarante: String,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct IdeDeclarado {
#[serde(rename = "tpNI")]
pub tp_ni: String,
#[serde(rename = "tpDeclarado")]
pub tp_declarado: Option<Vec<String>>,
#[serde(rename = "NIDeclarado")]
pub nideclarado: String,
#[serde(rename = "NomeDeclarado")]
pub nome_declarado: String,
#[serde(rename = "tpNomeDeclarado")]
pub tp_nome_declarado: Option<String>,
#[serde(rename = "DataNasc")]
pub data_nasc: Option<String>,
#[serde(rename = "EnderecoLivre")]
pub endereco_livre: Option<String>,
#[serde(rename = "tpEndereco")]
pub tp_endereco: Option<String>,
#[serde(rename = "$text")]
pub text: Option<String>,
#[serde(rename = "NIF")] // Número de Identificação Fiscal
pub nif: Option<Vec<IdeDeclaradoNif>>,
#[serde(rename = "NomeOutros")]
pub nome_outros: Option<NifNomeOutros>,
#[serde(rename = "InfoNascimento")]
pub info_nascimento: Option<NomePjInfoNascimento>,
#[serde(rename = "PaisEndereco")]
pub pais_endereco: PaisNascPaisEndereco,
#[serde(rename = "EnderecoOutros")]
pub endereco_outros: Option<Vec<PaisEnderecoEnderecoOutros>>,
#[serde(rename = "paisResid")]
pub pais_resid: Option<Vec<EnderecoPaisResid>>,
#[serde(rename = "PaisNacionalidade")]
pub pais_nacionalidade: Option<Vec<PaisResidPaisNacionalidade>>,
#[serde(rename = "Proprietarios")]
pub proprietarios: Option<Vec<Proprietarios>>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct IdeDeclaradoNif {
#[serde(rename = "NumeroNIF")]
pub numero_nif: String,
#[serde(rename = "PaisEmissaoNIF")]
pub pais_emissao_nif: String,
#[serde(rename = "tpNIF")]
pub tp_nif: Option<String>,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct NifNomeOutros {
#[serde(rename = "$text")]
pub text: Option<String>,
#[serde(rename = "NomePF")]
pub nome_pf: NifNomeOutrosNomePf,
#[serde(rename = "NomePJ")]
pub nome_pj: NomePj,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct NifNomeOutrosNomePf {
#[serde(rename = "tpNome")]
pub tp_nome: Option<String>,
#[serde(rename = "PrecTitulo")]
pub prec_titulo: String,
#[serde(rename = "Titulo")]
pub titulo: Vec<String>,
#[serde(rename = "IdGeracao")]
pub id_geracao: Vec<String>,
#[serde(rename = "Sufixo")]
pub sufixo: Vec<String>,
#[serde(rename = "GenSufixo")]
pub gen_sufixo: String,
#[serde(rename = "$text")]
pub text: Option<String>,
#[serde(rename = "PrimeiroNome")]
pub primeiro_nome: NifNomeOutrosNomePfPrimeiroNome,
#[serde(rename = "MeioNome")]
pub meio_nome: Vec<NomeOutrosNomePfPrimeiroNomeMeioNome>,
#[serde(rename = "PrefixoNome")]
pub prefixo_nome: NomePfPrimeiroNomeMeioNomePrefixoNome,
#[serde(rename = "UltimoNome")]
pub ultimo_nome: PrimeiroNomeMeioNomePrefixoNomeUltimoNome,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct NifNomeOutrosNomePfPrimeiroNome {
#[serde(rename = "Tipo")]
pub tipo: String,
#[serde(rename = "Nome")]
pub nome: String,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct NomeOutrosNomePfPrimeiroNomeMeioNome {
#[serde(rename = "Tipo")]
pub tipo: String,
#[serde(rename = "Nome")]
pub nome: String,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct NomePfPrimeiroNomeMeioNomePrefixoNome {
#[serde(rename = "Tipo")]
pub tipo: String,
#[serde(rename = "Nome")]
pub nome: String,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PrimeiroNomeMeioNomePrefixoNomeUltimoNome {
#[serde(rename = "Tipo")]
pub tipo: String,
#[serde(rename = "Nome")]
pub nome: String,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct NomePj {
#[serde(rename = "tpNome")]
pub tp_nome: String,
#[serde(rename = "Nome")]
pub nome: String,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct NomePjInfoNascimento {
#[serde(rename = "Municipio")]
pub municipio: String,
#[serde(rename = "Bairro")]
pub bairro: String,
#[serde(rename = "$text")]
pub text: Option<String>,
#[serde(rename = "PaisNasc")]
pub pais_nasc: NomePjInfoNascimentoPaisNasc,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct NomePjInfoNascimentoPaisNasc {
#[serde(rename = "Pais")]
pub pais: String,
#[serde(rename = "AntigoNomePais")]
pub antigo_nome_pais: Option<String>,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PaisNascPaisEndereco {
#[serde(rename = "Pais")]
pub pais: String,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PaisEnderecoEnderecoOutros {
#[serde(rename = "tpEndereco")]
pub tp_endereco: String,
#[serde(rename = "Pais")]
pub pais: String,
#[serde(rename = "EnderecoLivre")]
pub endereco_livre: Option<String>,
#[serde(rename = "$text")]
pub text: Option<String>,
#[serde(rename = "EnderecoEstrutura")]
pub endereco_estrutura: Option<PaisEnderecoEnderecoOutrosEnderecoEstrutura>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PaisEnderecoEnderecoOutrosEnderecoEstrutura {
#[serde(rename = "EnderecoLivre")]
pub endereco_livre: Option<String>,
#[serde(rename = "CEP")]
pub cep: String,
#[serde(rename = "Municipio")]
pub municipio: String,
#[serde(rename = "UF")]
pub uf: String,
#[serde(rename = "$text")]
pub text: Option<String>,
#[serde(rename = "Endereco")]
pub endereco: PaisEnderecoEnderecoOutrosEnderecoEstruturaEndereco,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PaisEnderecoEnderecoOutrosEnderecoEstruturaEndereco {
#[serde(rename = "Logradouro")]
pub logradouro: String,
#[serde(rename = "Numero")]
pub numero: String,
#[serde(rename = "Complemento")]
pub complemento: String,
#[serde(rename = "Andar")]
pub andar: String,
#[serde(rename = "Bairro")]
pub bairro: String,
#[serde(rename = "CaixaPostal")]
pub caixa_postal: String,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct EnderecoPaisResid {
#[serde(rename = "Pais")]
pub pais: String,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PaisResidPaisNacionalidade {
#[serde(rename = "Pais")]
pub pais: String,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Proprietarios {
#[serde(rename = "tpNI")]
pub tp_ni: String,
#[serde(rename = "NIProprietario")]
pub niproprietario: String,
#[serde(rename = "tpProprietario")]
pub tp_proprietario: Option<String>,
#[serde(rename = "Nome")]
pub nome: String,
#[serde(rename = "tpNome")]
pub tp_nome: Option<String>,
#[serde(rename = "EnderecoLivre")]
pub endereco_livre: Option<String>,
#[serde(rename = "tpEndereco")]
pub tp_endereco: Option<String>,
#[serde(rename = "DataNasc")]
pub data_nasc: Option<String>,
#[serde(rename = "$text")]
pub text: Option<String>,
#[serde(rename = "NIF")]
pub nif: Option<ProprietariosNif>,
#[serde(rename = "NomeOutros")]
pub nome_outros: Option<Vec<NifNomeOutros>>,
#[serde(rename = "PaisEndereco")]
pub pais_endereco: UltimoNomePaisEndereco,
#[serde(rename = "EnderecoOutros")]
pub endereco_outros: Option<Vec<PaisEnderecoEnderecoOutros>>,
#[serde(rename = "paisResid")]
pub pais_resid: Option<Vec<EnderecoPaisResid>>,
#[serde(rename = "PaisNacionalidade")]
pub pais_nacionalidade: Option<Vec<PaisResidPaisNacionalidade>>,
#[serde(rename = "InfoNascimento")]
pub info_nascimento: Option<PaisNacionalidadeInfoNascimento>,
#[serde(rename = "Reportavel")]
pub reportavel: PaisNascReportavel,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ProprietariosNif {
#[serde(rename = "NumeroNIF")]
pub numero_nif: String,
#[serde(rename = "PaisEmissaoNIF")]
pub pais_emissao_nif: String,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct UltimoNomePaisEndereco {
#[serde(rename = "Pais")]
pub pais: String,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PaisNacionalidadeInfoNascimento {
#[serde(rename = "Municipio")]
pub municipio: String,
#[serde(rename = "Bairro")]
pub bairro: String,
#[serde(rename = "$text")]
pub text: Option<String>,
#[serde(rename = "PaisNasc")]
pub pais_nasc: PaisNacionalidadeInfoNascimentoPaisNasc,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PaisNacionalidadeInfoNascimentoPaisNasc {
#[serde(rename = "Pais")]
pub pais: Option<String>,
#[serde(rename = "AntigoNomePais")]
pub antigo_nome_pais: Option<String>,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PaisNascReportavel {
#[serde(rename = "Pais")]
pub pais: String,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct MesCaixa {
#[serde(rename = "anoMesCaixa")]
pub ano_mes_caixa: Option<String>,
#[serde(rename = "$text")]
pub text: Option<String>,
#[serde(rename = "movOpFin")]
pub mov_op_fin: MovOpFin,
}
impl MesCaixa {
pub fn get_date_ano_mes_caixa(&self) -> Option<NaiveDate> {
get_naive_date_from_yyyymm(&self.ano_mes_caixa)
}
}
#[derive(Debug, Serialize, Deserialize)]
pub struct MovOpFin {
#[serde(rename = "$text")]
pub text: Option<String>,
#[serde(rename = "Conta")]
pub conta: Option<Vec<Conta>>,
#[serde(rename = "Cambio")]
pub cambio: Option<Cambio>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Conta {
#[serde(rename = "$text")]
pub text: Option<String>,
#[serde(rename = "MedJudic")]
pub med_judic: Option<Vec<ContaMedJudic>>,
#[serde(rename = "infoConta")]
pub info_conta: InfoConta,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct ContaMedJudic {
#[serde(rename = "NumProcJud")]
pub num_proc_jud: String,
#[serde(rename = "Vara")]
pub vara: String,
#[serde(rename = "SecJud")]
pub sec_jud: String,
#[serde(rename = "SubSecJud")]
pub sub_sec_jud: String,
#[serde(rename = "dtConcessao")]
pub dt_concessao: String,
#[serde(rename = "dtCassacao")]
pub dt_cassacao: String,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct InfoConta {
#[serde(rename = "tpConta")]
pub tp_conta: String,
#[serde(rename = "subTpConta")]
pub sub_tp_conta: String,
#[serde(rename = "tpNumConta")]
pub tp_num_conta: String,
#[serde(rename = "numConta")]
pub num_conta: String,
#[serde(rename = "tpRelacaoDeclarado")]
pub tp_relacao_declarado: String,
pub moeda: Option<String>,
#[serde(rename = "NoTitulares")]
pub no_titulares: String,
#[serde(rename = "dtEncerramentoConta")]
pub dt_encerramento_conta: Option<String>,
#[serde(rename = "IndInatividade")]
pub ind_inatividade: Option<String>,
#[serde(rename = "IndNDoc")]
pub ind_ndoc: Option<String>,
#[serde(rename = "$text")]
pub text: Option<String>,
#[serde(rename = "Reportavel")]
pub reportavel: Vec<InfoContaReportavel>,
#[serde(rename = "Intermediario")]
pub intermediario: Option<Intermediario>,
#[serde(rename = "Fundo")]
pub fundo: Option<Fundo>,
#[serde(rename = "BalancoConta")]
pub balanco_conta: BalancoConta,
#[serde(rename = "PgtosAcum")]
pub pgtos_acum: Vec<PgtosAcum>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct InfoContaReportavel {
#[serde(rename = "Pais")]
pub pais: String,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Intermediario {
#[serde(rename = "GIIN")]
pub giin: String,
#[serde(rename = "tpNI")]
pub tp_ni: String,
#[serde(rename = "NIIntermediario")]
pub niintermediario: String,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Fundo {
#[serde(rename = "GIIN")]
pub giin: String,
#[serde(rename = "CNPJ")]
pub cnpj: String,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct BalancoConta {
#[serde(rename = "totCreditos")]
pub tot_creditos: String,
#[serde(rename = "totDebitos")]
pub tot_debitos: String,
#[serde(rename = "totCreditosMesmaTitularidade")]
pub tot_creditos_mesma_titularidade: String,
#[serde(rename = "totDebitosMesmaTitularidade")]
pub tot_debitos_mesma_titularidade: String,
#[serde(rename = "vlrUltDia")]
pub vlr_ult_dia: Option<String>,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct PgtosAcum {
#[serde(rename = "tpPgto")]
pub tp_pgto: Vec<String>,
#[serde(rename = "totPgtosAcum")]
pub tot_pgtos_acum: String,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct Cambio {
#[serde(rename = "totCompras")]
pub tot_compras: String,
#[serde(rename = "totVendas")]
pub tot_vendas: String,
#[serde(rename = "totTransferencias")]
pub tot_transferencias: String,
#[serde(rename = "$text")]
pub text: Option<String>,
#[serde(rename = "MedJudic")]
pub med_judic: Option<Vec<CambioMedJudic>>,
}
#[derive(Debug, Serialize, Deserialize)]
pub struct CambioMedJudic {
#[serde(rename = "NumProcJud")]
pub num_proc_jud: String,
#[serde(rename = "Vara")]
pub vara: String,
#[serde(rename = "SecJud")]
pub sec_jud: String,
#[serde(rename = "SubSecJud")]
pub sub_sec_jud: String,
#[serde(rename = "dtConcessao")]
pub dt_concessao: String,
#[serde(rename = "dtCassacao")]
pub dt_cassacao: String,
#[serde(rename = "$text")]
pub text: Option<String>,
}
#[cfg(test)]
mod lib_functions {
use super::*;
use crate::MyResult;
use chrono::NaiveDate;
// cargo test -- --help
// cargo test -- --show-output
// cargo test -- --show-output multiple_values
#[test]
/// https://stackoverflow.com/questions/37140768/how-to-get-struct-field-names-in-rust
///
/// `cargo test -- --show-output get_struct_field_names`
#[allow(dead_code)]
fn get_struct_field_names() -> MyResult<()> {
my_macro! {
pub struct Test {
pub id: String,
pub cnpj_do_declarante: String,
pub ni_do_declarado: String,
pub nome_declarado: String,
pub ano_mes_caixa: Option<NaiveDate>,
pub num_conta: String,
pub tot_creditos: f64,
pub tot_debitos: f64,
}
}
let field_names = Test::get_field_names();
println!("field_names: {field_names:?}");
assert_eq!(
field_names,
[
"id",
"cnpj_do_declarante",
"ni_do_declarado",
"nome_declarado",
"ano_mes_caixa",
"num_conta",
"tot_creditos",
"tot_debitos"
]
);
Ok(())
}
#[test]
/// https://stackoverflow.com/questions/76429366/rust-unpack-iter-of-structs-to-iter-of-fields
///
/// `cargo test -- --show-output struct_iterator`
#[allow(dead_code)]
fn struct_iterator() -> MyResult<()> {
let info_a = InfoEFinanceira {
id: "identificação 01".to_string(),
cnpj_do_declarante: "cnpj 01".to_string(),
ni_do_declarado: "ni 01".to_string(),
nome_declarado: "nome 01".to_string(),
ano_mes_caixa: NaiveDate::from_ymd_opt(2015, 3, 14),
num_conta: "conta 01".to_string(),
pais_reportado: "país 01".to_string(),
tot_creditos: Some(12.45),
tot_debitos: Some(0.45),
};
let info_b = InfoEFinanceira {
id: "identificação 02".to_string(),
cnpj_do_declarante: "cnpj blabla 02".to_string(),
ni_do_declarado: "ni 02".to_string(),
nome_declarado: "nome 02".to_string(),
ano_mes_caixa: NaiveDate::from_ymd_opt(2015, 3, 15),
num_conta: "conta 02".to_string(),
pais_reportado: "país 02".to_string(),
tot_creditos: Some(5.45),
tot_debitos: None,
};
let info_c = InfoEFinanceira {
id: "id blablá".to_string(),
cnpj_do_declarante: "cnpj 03".to_string(),
ni_do_declarado: "ni foo 03".to_string(),
nome_declarado: "nome 03".to_string(),
ano_mes_caixa: NaiveDate::from_ymd_opt(2015, 3, 16),
num_conta: "conta 03".to_string(),
pais_reportado: "país 03".to_string(),
tot_creditos: None,
tot_debitos: Some(-327.4056),
};
let infos = vec![info_a, info_b, info_c];
infos
.iter()
.enumerate()
.for_each(|(index, info)| {
println!("index: {index} ; info: {info:?}, lengths: {:?}\n", info.get_lengths());
});
Ok(())
}
}