Skip to content

Commit 97d3c30

Browse files
committed
Fixed rust generated code and testcases
1 parent 48d4bef commit 97d3c30

File tree

2 files changed

+46
-44
lines changed

2 files changed

+46
-44
lines changed

lib/codegen/rust/curl_rust.dart

Lines changed: 10 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import 'package:jinja/jinja.dart' as jj;
2-
import 'package:apidash/utils/utils.dart' show getValidRequestUri, requestModelToHARJsonRequest;
2+
import 'package:apidash/utils/utils.dart'
3+
show getValidRequestUri, requestModelToHARJsonRequest;
34
import 'package:apidash/models/models.dart' show RequestModel;
45
import 'package:apidash/consts.dart';
56

@@ -31,13 +32,13 @@ fn main() {
3132
3233
""";
3334

34-
String kTemplateRawBody ="""
35+
String kTemplateRawBody = """
3536
easy.post_fields_copy(r#"{{body}}"#.as_bytes()).unwrap();
3637
3738
3839
""";
3940

40-
String kTemplateJsonBody ="""
41+
String kTemplateJsonBody = """
4142
easy.post_fields_copy(json!({{body}}).to_string().as_bytes()).unwrap();
4243
4344
@@ -48,14 +49,14 @@ fn main() {
4849
{% for field in fields %}
4950
form.part("{{field.name}}")
5051
{% if field.type == "file" %}.file("{{field.value}}"){% else %}.contents(b"{{field.value}}"){% endif %}
51-
.add();
52+
.add().unwrap();
5253
{% endfor %}
53-
easy.httppost(form);
54+
easy.httppost(form).unwrap();
5455
""";
5556

5657
String kTemplateHeader = """
5758
{% if headers %}let mut list = List::new();{% for header, value in headers %}
58-
list.append("{{header}}: {{value}}");{% endfor %}
59+
list.append("{{header}}: {{value}}").unwrap();{% endfor %}
5960
easy.http_headers(list).unwrap();
6061
{% endif %}
6162
@@ -86,7 +87,9 @@ fn main() {
8687

8788
result += jj.Template(kTemplateStart).render({
8889
"hasJsonBody": requestModel.hasJsonData,
89-
"hasHeaders": requestModel.enabledRequestHeaders != null || requestModel.hasBody
90+
"hasHeaders": (requestModel.enabledRequestHeaders != null &&
91+
requestModel.enabledRequestHeaders!.isNotEmpty) ||
92+
requestModel.hasBody
9093
});
9194

9295
var rec = getValidRequestUri(

test/codegen/curl_rust_codegen_test.dart

Lines changed: 36 additions & 37 deletions
Original file line numberDiff line numberDiff line change
@@ -135,7 +135,7 @@ fn main() {
135135
easy.get(true).unwrap();
136136
137137
let mut list = List::new();
138-
list.append("User-Agent: Test Agent");
138+
list.append("User-Agent: Test Agent").unwrap();
139139
easy.http_headers(list).unwrap();
140140
141141
{
@@ -169,7 +169,7 @@ fn main() {
169169
easy.get(true).unwrap();
170170
171171
let mut list = List::new();
172-
list.append("User-Agent: Test Agent");
172+
list.append("User-Agent: Test Agent").unwrap();
173173
easy.http_headers(list).unwrap();
174174
175175
{
@@ -232,7 +232,7 @@ fn main() {
232232
easy.get(true).unwrap();
233233
234234
let mut list = List::new();
235-
list.append("User-Agent: Test Agent");
235+
list.append("User-Agent: Test Agent").unwrap();
236236
easy.http_headers(list).unwrap();
237237
238238
{
@@ -295,7 +295,7 @@ fn main() {
295295
easy.get(true).unwrap();
296296
297297
let mut list = List::new();
298-
list.append("User-Agent: Test Agent");
298+
list.append("User-Agent: Test Agent").unwrap();
299299
easy.http_headers(list).unwrap();
300300
301301
{
@@ -329,7 +329,7 @@ fn main() {
329329
easy.get(true).unwrap();
330330
331331
let mut list = List::new();
332-
list.append("User-Agent: Test Agent");
332+
list.append("User-Agent: Test Agent").unwrap();
333333
easy.http_headers(list).unwrap();
334334
335335
{
@@ -354,7 +354,6 @@ fn main() {
354354
test('GET12', () {
355355
const expectedCode = r"""
356356
use curl::easy::Easy;
357-
use curl::easy::List;
358357
359358
fn main() {
360359
let mut easy = Easy::new();
@@ -461,7 +460,7 @@ fn main() {
461460
}"#.as_bytes()).unwrap();
462461
463462
let mut list = List::new();
464-
list.append("Content-Type: text/plain");
463+
list.append("Content-Type: text/plain").unwrap();
465464
easy.http_headers(list).unwrap();
466465
467466
{
@@ -505,7 +504,7 @@ fn main() {
505504
}).to_string().as_bytes()).unwrap();
506505
507506
let mut list = List::new();
508-
list.append("Content-Type: application/json");
507+
list.append("Content-Type: application/json").unwrap();
509508
easy.http_headers(list).unwrap();
510509
511510
{
@@ -544,8 +543,8 @@ fn main() {
544543
}).to_string().as_bytes()).unwrap();
545544
546545
let mut list = List::new();
547-
list.append("User-Agent: Test Agent");
548-
list.append("Content-Type: application/json");
546+
list.append("User-Agent: Test Agent").unwrap();
547+
list.append("Content-Type: application/json").unwrap();
549548
easy.http_headers(list).unwrap();
550549
551550
{
@@ -582,17 +581,17 @@ fn main() {
582581
583582
form.part("text")
584583
.contents(b"API")
585-
.add();
584+
.add().unwrap();
586585
587586
form.part("sep")
588587
.contents(b"|")
589-
.add();
588+
.add().unwrap();
590589
591590
form.part("times")
592591
.contents(b"3")
593-
.add();
592+
.add().unwrap();
594593
595-
easy.httppost(form);
594+
easy.httppost(form).unwrap();
596595
{
597596
let mut transfer = easy.transfer();
598597
transfer.write_function(|new_data| {
@@ -627,19 +626,19 @@ fn main() {
627626
628627
form.part("text")
629628
.contents(b"API")
630-
.add();
629+
.add().unwrap();
631630
632631
form.part("sep")
633632
.contents(b"|")
634-
.add();
633+
.add().unwrap();
635634
636635
form.part("times")
637636
.contents(b"3")
638-
.add();
637+
.add().unwrap();
639638
640-
easy.httppost(form);
639+
easy.httppost(form).unwrap();
641640
let mut list = List::new();
642-
list.append("User-Agent: Test Agent");
641+
list.append("User-Agent: Test Agent").unwrap();
643642
easy.http_headers(list).unwrap();
644643
645644
{
@@ -676,13 +675,13 @@ fn main() {
676675
677676
form.part("token")
678677
.contents(b"xyz")
679-
.add();
678+
.add().unwrap();
680679
681680
form.part("imfile")
682681
.file("/Documents/up/1.png")
683-
.add();
682+
.add().unwrap();
684683
685-
easy.httppost(form);
684+
easy.httppost(form).unwrap();
686685
{
687686
let mut transfer = easy.transfer();
688687
transfer.write_function(|new_data| {
@@ -717,13 +716,13 @@ fn main() {
717716
718717
form.part("token")
719718
.contents(b"xyz")
720-
.add();
719+
.add().unwrap();
721720
722721
form.part("imfile")
723722
.file("/Documents/up/1.png")
724-
.add();
723+
.add().unwrap();
725724
726-
easy.httppost(form);
725+
easy.httppost(form).unwrap();
727726
{
728727
let mut transfer = easy.transfer();
729728
transfer.write_function(|new_data| {
@@ -758,17 +757,17 @@ fn main() {
758757
759758
form.part("text")
760759
.contents(b"API")
761-
.add();
760+
.add().unwrap();
762761
763762
form.part("sep")
764763
.contents(b"|")
765-
.add();
764+
.add().unwrap();
766765
767766
form.part("times")
768767
.contents(b"3")
769-
.add();
768+
.add().unwrap();
770769
771-
easy.httppost(form);
770+
easy.httppost(form).unwrap();
772771
{
773772
let mut transfer = easy.transfer();
774773
transfer.write_function(|new_data| {
@@ -803,16 +802,16 @@ fn main() {
803802
804803
form.part("token")
805804
.contents(b"xyz")
806-
.add();
805+
.add().unwrap();
807806
808807
form.part("imfile")
809808
.file("/Documents/up/1.png")
810-
.add();
809+
.add().unwrap();
811810
812-
easy.httppost(form);
811+
easy.httppost(form).unwrap();
813812
let mut list = List::new();
814-
list.append("User-Agent: Test Agent");
815-
list.append("Keep-Alive: true");
813+
list.append("User-Agent: Test Agent").unwrap();
814+
list.append("Keep-Alive: true").unwrap();
816815
easy.http_headers(list).unwrap();
817816
818817
{
@@ -855,7 +854,7 @@ fn main() {
855854
}).to_string().as_bytes()).unwrap();
856855
857856
let mut list = List::new();
858-
list.append("Content-Type: application/json");
857+
list.append("Content-Type: application/json").unwrap();
859858
easy.http_headers(list).unwrap();
860859
861860
{
@@ -898,7 +897,7 @@ fn main() {
898897
}).to_string().as_bytes()).unwrap();
899898
900899
let mut list = List::new();
901-
list.append("Content-Type: application/json");
900+
list.append("Content-Type: application/json").unwrap();
902901
easy.http_headers(list).unwrap();
903902
904903
{
@@ -970,7 +969,7 @@ fn main() {
970969
}).to_string().as_bytes()).unwrap();
971970
972971
let mut list = List::new();
973-
list.append("Content-Type: application/json");
972+
list.append("Content-Type: application/json").unwrap();
974973
easy.http_headers(list).unwrap();
975974
976975
{

0 commit comments

Comments
 (0)