aws-sdk-rust 0.1.0

AWS SDK for Rust. WIP and under heavy development - warning.
Documentation
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
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
1001
1002
1003
1004
1005
1006
1007
1008
1009
1010
1011
1012
1013
1014
1015
1016
1017
1018
1019
1020
1021
1022
1023
1024
1025
1026
1027
1028
1029
1030
1031
1032
1033
1034
1035
1036
1037
1038
1039
1040
1041
1042
1043
1044
1045
1046
1047
1048
1049
1050
1051
1052
1053
1054
1055
1056
1057
1058
1059
1060
1061
1062
1063
1064
1065
1066
1067
1068
1069
1070
1071
1072
1073
1074
1075
1076
1077
1078
1079
1080
1081
1082
1083
1084
1085
1086
1087
1088
1089
1090
1091
1092
1093
1094
1095
1096
1097
1098
1099
1100
1101
1102
1103
1104
1105
1106
1107
1108
1109
1110
1111
1112
1113
1114
1115
1116
1117
1118
1119
1120
1121
1122
1123
1124
1125
1126
1127
1128
1129
1130
1131
1132
1133
1134
1135
1136
1137
1138
1139
1140
1141
1142
1143
1144
1145
1146
1147
1148
1149
1150
1151
1152
1153
1154
1155
1156
1157
1158
1159
1160
1161
1162
1163
1164
1165
1166
1167
1168
1169
1170
/*
 Copyright 2016 LambdaStack All rights reserved.

 Licensed under the Apache License, Version 2.0 (the "License");
 you may not use this file except in compliance with the License.
 You may obtain a copy of the License at

 http://www.apache.org/licenses/LICENSE-2.0

 Unless required by applicable law or agreed to in writing, software
 distributed under the License is distributed on an "AS IS" BASIS,
 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 See the License for the specific language governing permissions and
 limitations under the License.
*/

#![allow(unused_variables, unused_mut)]
use std::fmt;
use std::ascii::AsciiExt;
use std::collections::HashMap;
use std::error::Error;
use std::io::BufReader;
use std::io::Read;
use std::num::ParseIntError;
use std::str::{FromStr, ParseBoolError};
use std::str;

use hyper::client::{Client, RedirectPolicy};
use openssl::crypto::hash::Type::MD5;
use openssl::crypto::hash::hash;
use rustc_serialize::base64::{ToBase64, STANDARD};
use xml::*;

use aws::common::credentials::{AwsCredentialsProvider, AwsCredentials};
use aws::common::region::Region;
use aws::common::xmlutil::*;
use aws::common::params::{Params, ServiceParams};
use aws::common::signature::SignedRequest;
use aws::common::request::{DispatchSignedRequest, HttpResponse};
use aws::errors::s3_error::S3Error;
use aws::errors::credentials_error::CredentialsError;

pub type Location = String;
/// Parse `Location` from XML
struct LocationParser;
impl LocationParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<Location, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `Location` contents to a `SignedRequest`
struct LocationWriter;
impl LocationWriter {
    fn write_params(params: &mut Params, name: &str, obj: &Location) {
        params.put(name, obj);
    }
}

#[derive(Debug, Default)]
pub struct CreateBucketOutput {
    pub location: Location,
}

/// Parse `CreateBucketOutput` from XML
struct CreateBucketOutputParser;
impl CreateBucketOutputParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<CreateBucketOutput, XmlParseError> {
        try!(start_element(tag_name, stack));
        let mut obj = CreateBucketOutput::default();
        loop {
            let current_name = try!(peek_at_name(stack));
            if current_name == "Location" {
                obj.location = try!(LocationParser::parse_xml("Location", stack));
                continue;
            }
            break;
        }
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `CreateBucketOutput` contents to a `SignedRequest`
struct CreateBucketOutputWriter;
impl CreateBucketOutputWriter {
    fn write_params(params: &mut Params, name: &str, obj: &CreateBucketOutput) {
        let mut prefix = name.to_string();
        if prefix != "" { prefix.push_str("."); }
        LocationWriter::write_params(params, &(prefix.to_string() + "Location"), &obj.location);
    }
}

pub type S3ClientMessage = String;
/// Parse `S`3ClientMessage from XML
pub struct S3ClientMessageParser;

impl S3ClientMessageParser {
    pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<S3ClientMessage, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `S3ClientMessage` contents to a `SignedRequest`
pub struct S3ClientMessageWriter;

impl S3ClientMessageWriter {
    pub fn write_params(params: &mut Params, name: &str, obj: &S3ClientMessage) {
        params.put(name, obj);
    }
}

#[derive(Debug, Default)]
pub struct ListBucketsOutput {
    pub owner: Owner,
    pub buckets: Buckets,
}

/// Parse `ListBucketsOutput` from XML
struct ListBucketsOutputParser;
impl ListBucketsOutputParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<ListBucketsOutput, XmlParseError> {
        try!(start_element(tag_name, stack));
        let mut obj = ListBucketsOutput::default();
        loop {
            let current_name = try!(peek_at_name(stack));
            match current_name.as_ref() {
                "Owner" => {
                    obj.owner = try!(OwnerParser::parse_xml("Owner", stack));
                    continue;
                },
                "Buckets" => {
                    stack.next(); // skip Buckets start and go to contents
                    // this will parse all buckets:
                    obj.buckets = try!(BucketsParser::parse_xml("Bucket", stack));
                },
                _ => break,
            }
        }
        stack.next(); // skip Buckets end
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `ListBucketsOutput` contents to a `SignedRequest`
struct ListBucketsOutputWriter;
impl ListBucketsOutputWriter {
    fn write_params(params: &mut Params, name: &str, obj: &ListBucketsOutput) {
        let mut prefix = name.to_string();
        if prefix != "" { prefix.push_str("."); }
        OwnerWriter::write_params(params, &(prefix.to_string() + "Owner"), &obj.owner);
        BucketsWriter::write_params(params, &(prefix.to_string() + "Bucket"), &obj.buckets);
    }
}

#[derive(Debug)]
pub struct S3Client<P, D> where P: AwsCredentialsProvider, D: DispatchSignedRequest {
            credentials_provider: P,
            region: Region,
            dispatcher: D,
        }

impl<P> S3Client<P, Client> where P: AwsCredentialsProvider {
    pub fn new(credentials_provider: P, region: Region) -> Self {
        let mut client = Client::new();
        client.set_redirect_policy(RedirectPolicy::FollowNone);
        S3Client::with_request_dispatcher(client, credentials_provider, region)
    }
}

impl<P, D> S3Client<P, D> where P: AwsCredentialsProvider, D: DispatchSignedRequest {
    pub fn with_request_dispatcher(request_dispatcher: D, credentials_provider: P, region: Region) -> Self {
        S3Client {
            credentials_provider: credentials_provider,
            region: region,
            dispatcher: request_dispatcher
        }
    }

    /// Creates a new bucket.
    /// All requests go to the us-east-1/us-standard endpoint, but can create buckets anywhere.
    pub fn create_bucket(&self, input: &CreateBucketRequest) -> Result<CreateBucketOutput, S3Error> {
        let region = Region::UsEast1;
        let mut create_config : Vec<u8>;
        let mut request = SignedRequest::new("PUT", "s3", region, "");
        let hostname = self.hostname(Some(&input.bucket));
        request.set_hostname(Some(hostname));

        if needs_create_bucket_config(self.region) {
            create_config = create_bucket_config_xml(self.region);
            request.set_payload(Some(&create_config));
        }

        match input.acl {
            None => (),
            Some(ref canned_acl) => request.add_header("x-amz-acl", &canned_acl_in_aws_format(canned_acl)),
        }

        let result = sign_and_execute(&self.dispatcher, &mut request, try!(self.credentials_provider.credentials()));
        let status = result.status;

        match status {
            200 => {
                match result.headers.get("Location") {
                    Some(ref value) => Ok(CreateBucketOutput{ location: value.to_string() }),
                    None => Err(S3Error::new("Something went wrong when creating a bucket."))
                }
            }
            _ => {
                Err(S3Error::new("error in create_bucket"))
            }
        }
    }

    /// Returns a list of all buckets owned by the authenticated sender of the
    /// request.
    pub fn list_buckets(&self) -> Result<ListBucketsOutput, S3Error> {
        let mut request = SignedRequest::new("GET", "s3", self.region, "/");
        let mut params = Params::new();
        params.put("Action", "ListBuckets");
        request.set_params(params);
        let result = sign_and_execute(&self.dispatcher, &mut request, try!(self.credentials_provider.credentials()));
        let status = result.status;
        let mut reader = EventReader::from_str(&result.body);
        let mut stack = XmlResponse::new(reader.events().peekable());

        stack.next(); // xml start tag

        match status {
            200 => {
                // was "ListBucketsOutput"
                Ok(try!(ListBucketsOutputParser::parse_xml("ListAllMyBucketsResult", &mut stack)))
            }
            _ => { Err(S3Error::new("error in list_buckets")) }
        }
    }

    fn hostname(&self, bucket: Option<&BucketName>) -> String {
        let host = match self.region {
                    Region::UsEast1 => "s3.amazonaws.com".to_string(),
                    Region::CnNorth1 => format!("s3.{}.amazonaws.com.cn", self.region),
                    _ => format!("s3-{}.amazonaws.com", self.region),
                };

        match bucket {
            Some(b) => format!("{}.s3.amazonaws.com", b),
            None => host,
        }
    }
}

/// Helper function to determine if a create config is needed.
pub fn needs_create_bucket_config(region: Region) -> bool {
    match region {
        Region::UsEast1 => false,
        _ => true,
    }
}

// This is a bit hacky to get functionality until we figure out an XML writing util.
/// Manually writes out bucket configuration (location constraint) in XML.
pub fn create_bucket_config_xml(region: Region) -> Vec<u8> {
    match region {
        Region::UsEast1 => {
            Vec::new() // shouldn't actually execute this: panic! or unreachable! this?
        }
        _ => {
            let xml = format!("<CreateBucketConfiguration xmlns=\"http://s3.amazonaws.com/doc/2006-03-01/\">
        <LocationConstraint>{}</LocationConstraint>
        </CreateBucketConfiguration >", region);
            xml.into_bytes()
        }
    }
}

/// Maps canned acl to AWS format.  EG public-read.
pub fn canned_acl_in_aws_format(canned_acl: &CannedAcl) -> String {
    match *canned_acl {
        CannedAcl::Private => "private".to_string(),
        CannedAcl::PublicRead => "public-read".to_string(),
        CannedAcl::PublicReadWrite => "public-read-write".to_string(),
        CannedAcl::AuthenticatedRead => "authenticated-read".to_string(),
        CannedAcl::BucketOwnerRead => "bucket-owner-read".to_string(),
        CannedAcl::BucketOwnerFullControl => "bucket-owner-full-control".to_string(),
    }
}

/// `extract_s3_redirect_location` takes a Hyper `Response` and attempts to pull out the temporary endpoint.
fn extract_s3_redirect_location(response: HttpResponse) -> Result<String, S3Error> {

    let mut reader = EventReader::from_str(&response.body);
    let mut stack = XmlResponse::new(reader.events().peekable());
    stack.next(); // xml start tag

    // extract and return temporary endpoint location
    extract_s3_temporary_endpoint_from_xml(&mut stack)
}

fn field_in_s3_redirect(name: &str) -> bool {
    if name == "Code" || name == "Message" || name == "Bucket" || name == "RequestId" || name == "HostId" {
        return true;
    }
    false
}


/// `extract_s3_temporary_endpoint_from_xml` takes in XML and tries to find the value of the Endpoint node.
fn extract_s3_temporary_endpoint_from_xml<T: Peek + Next>(stack: &mut T) -> Result<String, S3Error> {
    try!(start_element(&"Error".to_string(), stack));

    // now find Endpoint contents
    // This may infinite loop if there's no endpoint in the response: how can we prevent that?
    loop {
        let current_name = try!(peek_at_name(stack));
        if current_name == "Endpoint" {
            let obj = try!(string_field("Endpoint", stack));
            return Ok(obj);
        }
        if field_in_s3_redirect(&current_name){
            // <foo>bar</foo>:
            stack.next(); // skip the start tag <foo>
            stack.next(); // skip contents bar
            stack.next(); // skip close tag </foo>
            continue;
        }
        break;
    }
    Err(S3Error::new("Couldn't find redirect location for S3 bucket"))
}

fn sign_and_execute<D>(dispatcher: &D, request: &mut SignedRequest, creds: AwsCredentials) -> HttpResponse where D: DispatchSignedRequest{
    request.sign(&creds);
    let response = dispatcher.dispatch(request).expect("Error dispatching request");
    debug!("Sent request to AWS");

    if response.status == 307 {
        debug!("Got a redirect response, resending request.");
        // extract location from response, modify request and re-sign and resend.
        let new_hostname = extract_s3_redirect_location(response).unwrap();
        request.set_hostname(Some(new_hostname.to_string()));

        // This does a lot of appending and not clearing/creation, so we'll have to do that ourselves:
        request.sign(&creds);
        return dispatcher.dispatch(request).unwrap();
    }

    response
}

pub type ObjectCannedACL = String;
/// Parse `ObjectCannedACL` from XML
struct ObjectCannedACLParser;
impl ObjectCannedACLParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<ObjectCannedACL, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `ObjectCannedACL` contents to a `SignedRequest`
struct ObjectCannedACLWriter;
impl ObjectCannedACLWriter {
    fn write_params(params: &mut Params, name: &str, obj: &ObjectCannedACL) {
        params.put(name, obj);
    }
}

/// Canned ACL for S3
#[derive(Debug)]
pub enum CannedAcl {
    Private,
    PublicRead,
    PublicReadWrite,
    AuthenticatedRead,
    BucketOwnerRead,
    BucketOwnerFullControl,
}

#[derive(Debug, Default)]
pub struct CreateBucketRequest {
    /// Allows grantee the read, write, read ACP, and write ACP permissions on the
    /// bucket.
    pub grant_full_control: Option<GrantFullControl>,
    pub create_bucket_configuration: Option<CreateBucketConfiguration>,
    /// Allows grantee to write the ACL for the applicable bucket.
    pub grant_write_acp: Option<GrantWriteACP>,
    pub bucket: BucketName,
    /// The canned ACL to apply to the bucket.
    pub acl: Option<CannedAcl>,
    /// Allows grantee to create, overwrite, and delete any object in the bucket.
    pub grant_write: Option<GrantWrite>,
    /// Allows grantee to list the objects in the bucket.
    pub grant_read: Option<GrantRead>,
    /// Allows grantee to read the bucket ACL.
    pub grant_read_acp: Option<GrantReadACP>,
}

pub type BucketName = String;
/// Parse `BucketName` from XML
struct BucketNameParser;
impl BucketNameParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<BucketName, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}

#[derive(Debug, Default)]
pub struct Bucket {
    /// Date the bucket was created.
    pub creation_date: CreationDate,
    /// The name of the bucket.
    pub name: BucketName,
}

/// Parse `Bucket` from XML
struct BucketParser;
impl BucketParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<Bucket, XmlParseError> {
        try!(start_element(tag_name, stack));
        let mut obj = Bucket::default();
        loop {
            let current_name = try!(peek_at_name(stack));
            if current_name == "CreationDate" {
                obj.creation_date = try!(CreationDateParser::parse_xml("CreationDate", stack));
                continue;
            }
            if current_name == "Name" {
                obj.name = try!(BucketNameParser::parse_xml("Name", stack));
                continue;
            }
            break;
        }
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}

/// Write `BucketName` contents to a `SignedRequest`
struct BucketNameWriter;
impl BucketNameWriter {
    fn write_params(params: &mut Params, name: &str, obj: &BucketName) {
        params.put(name, obj);
    }
}

/// Write `Bucket` contents to a `SignedRequest`
struct BucketWriter;
impl BucketWriter {
    fn write_params(params: &mut Params, name: &str, obj: &Bucket) {
        let mut prefix = name.to_string();
        if prefix != "" { prefix.push_str("."); }
        CreationDateWriter::write_params(params, &(prefix.to_string() + "CreationDate"), &obj.creation_date);
        BucketNameWriter::write_params(params, &(prefix.to_string() + "Name"), &obj.name);
    }
}

pub type GrantReadACP = String;
/// Parse `GrantReadACP` from XML
struct GrantReadACPParser;
impl GrantReadACPParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<GrantReadACP, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `GrantReadACP` contents to a `SignedRequest`
struct GrantReadACPWriter;
impl GrantReadACPWriter {
    fn write_params(params: &mut Params, name: &str, obj: &GrantReadACP) {
        params.put(name, obj);
    }
}
#[derive(Debug, Default)]
pub struct Grant {
    pub grantee: Grantee,
    /// Specifies the permission given to the grantee.
    pub permission: Permission,
}

/// Parse `Grant` from XML
struct GrantParser;
impl GrantParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<Grant, XmlParseError> {
        try!(start_element(tag_name, stack));
        let mut obj = Grant::default();
        loop {
            let current_name = try!(peek_at_name(stack));
            if current_name == "Grantee" {
                obj.grantee = try!(GranteeParser::parse_xml("Grantee", stack));
                continue;
            }
            if current_name == "Permission" {
                obj.permission = try!(PermissionParser::parse_xml("Permission", stack));
                continue;
            }
            break;
        }
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `Grant` contents to a `SignedRequest`
struct GrantWriter;
impl GrantWriter {
    fn write_params(params: &mut Params, name: &str, obj: &Grant) {
        let mut prefix = name.to_string();
        if prefix != "" { prefix.push_str("."); }
        GranteeWriter::write_params(params, &(prefix.to_string() + "Grantee"), &obj.grantee);
        PermissionWriter::write_params(params, &(prefix.to_string() + "Permission"), &obj.permission);
    }
}

/// Write `Grantee` contents to a `SignedRequest`
struct GranteeWriter;
impl GranteeWriter {
    fn write_params(params: &mut Params, name: &str, obj: &Grantee) {
        let mut prefix = name.to_string();
        if prefix != "" { prefix.push_str("."); }
        if let Some(ref obj) = obj.email_address {
            EmailAddressWriter::write_params(params, &(prefix.to_string() + "EmailAddress"), obj);
        }
        TypeWriter::write_params(params, &(prefix.to_string() + "xsi:type"), &obj.foo_type);
        if let Some(ref obj) = obj.display_name {
            DisplayNameWriter::write_params(params, &(prefix.to_string() + "DisplayName"), obj);
        }
        if let Some(ref obj) = obj.id {
            IDWriter::write_params(params, &(prefix.to_string() + "ID"), obj);
        }
        if let Some(ref obj) = obj.uri {
            URIWriter::write_params(params, &(prefix.to_string() + "URI"), obj);
        }
    }
}

pub type Permission = String;
/// Parse `Permission` from XML
struct PermissionParser;
impl PermissionParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<Permission, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `Permission` contents to a `SignedRequest`
struct PermissionWriter;
impl PermissionWriter {
    fn write_params(params: &mut Params, name: &str, obj: &Permission) {
        params.put(name, obj);
    }
}

pub type URI = String;
/// Parse `URI` from XML
struct URIParser;
impl URIParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<URI, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `URI` contents to a `SignedRequest`
struct URIWriter;
impl URIWriter {
    fn write_params(params: &mut Params, name: &str, obj: &URI) {
        params.put(name, obj);
    }
}

pub type ID = String;
/// Parse `ID` from XML
struct IDParser;
impl IDParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<ID, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `ID` contents to a `SignedRequest`
struct IDWriter;
impl IDWriter {
    fn write_params(params: &mut Params, name: &str, obj: &ID) {
        params.put(name, obj);
    }
}

pub type ContentType = String;
/// Parse `ContentType` from XML
struct ContentTypeParser;
impl ContentTypeParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<ContentType, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `ContentType` contents to a `SignedRequest`
struct ContentTypeWriter;
impl ContentTypeWriter {
    fn write_params(params: &mut Params, name: &str, obj: &ContentType) {
        params.put(name, obj);
    }
}

pub type Type = String;
/// Parse `Type` from XML
struct TypeParser;
impl TypeParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<Type, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `Type` contents to a `SignedRequest`
struct TypeWriter;
impl TypeWriter {
    fn write_params(params: &mut Params, name: &str, obj: &Type) {
        params.put(name, obj);
    }
}
pub type Buckets = Vec<Bucket>;
/// Parse `Buckets` from XML
struct BucketsParser;
impl BucketsParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<Buckets, XmlParseError> {
        let mut obj = Vec::new();
        while try!(peek_at_name(stack)) == "Bucket" {
            obj.push(try!(BucketParser::parse_xml("Bucket", stack)));
        }
        Ok(obj)
    }
}
/// Write `Buckets` contents to a `SignedRequest`
struct BucketsWriter;
impl BucketsWriter {
    fn write_params(params: &mut Params, name: &str, obj: &Buckets) {
        let mut index = 1;
        for element in obj.iter() {
            let key = &format!("{}.{}", name, index);
            BucketWriter::write_params(params, key, element);
            index += 1;
        }
    }
}

pub type EmailAddress = String;
/// Parse `EmailAddress` from XML
struct EmailAddressParser;
impl EmailAddressParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<EmailAddress, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `EmailAddress` contents to a `SignedRequest`
struct EmailAddressWriter;
impl EmailAddressWriter {
    fn write_params(params: &mut Params, name: &str, obj: &EmailAddress) {
        params.put(name, obj);
    }
}

#[derive(Debug, Default)]
pub struct Grantee {
    /// Email address of the grantee.
    pub email_address: Option<EmailAddress>,
    /// Type of grantee
    pub foo_type: Type,
    /// Screen name of the grantee.
    pub display_name: Option<DisplayName>,
    /// The canonical user ID of the grantee.
    pub id: Option<ID>,
    /// URI of the grantee group.
    pub uri: Option<URI>,
}

/// Parse `Grantee` from XML
struct GranteeParser;
impl GranteeParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<Grantee, XmlParseError> {
        try!(start_element(tag_name, stack));
        let mut obj = Grantee::default();
        loop {
            let current_name = try!(peek_at_name(stack));
            if current_name == "EmailAddress" {
                obj.email_address = Some(try!(EmailAddressParser::parse_xml("EmailAddress", stack)));
                continue;
            }
            if current_name == "xsi:type" {
                obj.foo_type = try!(TypeParser::parse_xml("xsi:type", stack));
                continue;
            }
            if current_name == "DisplayName" {
                obj.display_name = Some(try!(DisplayNameParser::parse_xml("DisplayName", stack)));
                continue;
            }
            if current_name == "ID" {
                obj.id = Some(try!(IDParser::parse_xml("ID", stack)));
                continue;
            }
            if current_name == "URI" {
                obj.uri = Some(try!(URIParser::parse_xml("URI", stack)));
                continue;
            }
            break;
        }
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}

pub type DisplayName = String;
/// Parse `DisplayName` from XML
struct DisplayNameParser;
impl DisplayNameParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<DisplayName, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `DisplayName` contents to a `SignedRequest`
struct DisplayNameWriter;
impl DisplayNameWriter {
    fn write_params(params: &mut Params, name: &str, obj: &DisplayName) {
        params.put(name, obj);
    }
}

pub type GrantRead = String;
/// Parse `GrantRead` from XML
struct GrantReadParser;
impl GrantReadParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<GrantRead, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `GrantRead` contents to a `SignedRequest`
struct GrantReadWriter;
impl GrantReadWriter {
    fn write_params(params: &mut Params, name: &str, obj: &GrantRead) {
        params.put(name, obj);
    }
}

pub type CreationDate = String;
/// Parse `CreationDate` from XML
struct CreationDateParser;
impl CreationDateParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<CreationDate, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `CreationDate` contents to a `SignedRequest`
struct CreationDateWriter;
impl CreationDateWriter {
    fn write_params(params: &mut Params, name: &str, obj: &CreationDate) {
        params.put(name, obj);
    }
}

pub type GrantWriteACP = String;
/// Parse `GrantWriteACP` from XML
struct GrantWriteACPParser;
impl GrantWriteACPParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<GrantWriteACP, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `GrantWriteACP` contents to a `SignedRequest`
struct GrantWriteACPWriter;
impl GrantWriteACPWriter {
    fn write_params(params: &mut Params, name: &str, obj: &GrantWriteACP) {
        params.put(name, obj);
    }
}

pub type GrantWrite = String;
/// Parse `GrantWrite` from XML
struct GrantWriteParser;
impl GrantWriteParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<GrantWrite, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `GrantWrite` contents to a `SignedRequest`
struct GrantWriteWriter;
impl GrantWriteWriter {
    fn write_params(params: &mut Params, name: &str, obj: &GrantWrite) {
        params.put(name, obj);
    }
}

#[derive(Debug, Default)]
pub struct CreateBucketConfiguration {
    /// Specifies the region where the bucket will be created. If you don't specify a
    /// region, the bucket will be created in US Standard.
    pub location_constraint: BucketLocationConstraint,
}

pub type GrantFullControl = String;
/// Parse `GrantFullControl` from XML
struct GrantFullControlParser;
impl GrantFullControlParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<GrantFullControl, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `GrantFullControl` contents to a `SignedRequest`
struct GrantFullControlWriter;
impl GrantFullControlWriter {
    fn write_params(params: &mut Params, name: &str, obj: &GrantFullControl) {
        params.put(name, obj);
    }
}

/// The requested bucket name is not available. The bucket namespace is shared by
/// all users of the system. Please select a different name and try again.
#[derive(Debug, Default)]
pub struct BucketAlreadyExists;

/// Parse `BucketAlreadyExists` from XML
struct BucketAlreadyExistsParser;
impl BucketAlreadyExistsParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<BucketAlreadyExists, XmlParseError> {
        try!(start_element(tag_name, stack));
        let mut obj = BucketAlreadyExists::default();
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `BucketAlreadyExists` contents to a `SignedRequest`
struct BucketAlreadyExistsWriter;
impl BucketAlreadyExistsWriter {
    fn write_params(params: &mut Params, name: &str, obj: &BucketAlreadyExists) {
        let mut prefix = name.to_string();
        if prefix != "" { prefix.push_str("."); }
    }
}
pub type BucketLocationConstraint = String;
/// Parse `BucketLocationConstraint` from XML
struct BucketLocationConstraintParser;
impl BucketLocationConstraintParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<BucketLocationConstraint, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `BucketLocationConstraint` contents to a `SignedRequest`
struct BucketLocationConstraintWriter;
impl BucketLocationConstraintWriter {
    fn write_params(params: &mut Params, name: &str, obj: &BucketLocationConstraint) {
        params.put(name, obj);
    }
}

#[derive(Debug, Default)]
pub struct Owner {
    pub display_name: DisplayName,
    pub id: ID,
}

/// Parse `Owner` from XML
struct OwnerParser;
impl OwnerParser {
    fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<Owner, XmlParseError> {
        try!(start_element(tag_name, stack));
        let mut obj = Owner::default();
        loop {
            let current_name = try!(peek_at_name(stack));
            match current_name.as_ref() {
                "DisplayName" => {
                    obj.display_name = try!(DisplayNameParser::parse_xml("DisplayName", stack));
                    continue;
                },
                "ID" => {
                    obj.id = try!(IDParser::parse_xml("ID", stack));
                    continue;
                },
                _ => break,
            }
        }
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `Owner` contents to a `SignedRequest`
struct OwnerWriter;
impl OwnerWriter {
    fn write_params(params: &mut Params, name: &str, obj: &Owner) {
        let mut prefix = name.to_string();
        if prefix != "" { prefix.push_str("."); }
        DisplayNameWriter::write_params(params, &(prefix.to_string() + "DisplayName"), &obj.display_name);
        IDWriter::write_params(params, &(prefix.to_string() + "ID"), &obj.id);
    }
}

pub type Code = String;
/// Parse `Code` from XML
pub struct CodeParser;

impl CodeParser {
    pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<Code, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `Code` contents to a `SignedRequest`
pub struct CodeWriter;

impl CodeWriter {
    pub fn write_params(params: &mut Params, name: &str, obj: &Code) {
        params.put(name, obj);
    }
}

#[derive(Debug, Default)]
pub struct ObjectVersion {
    /// Date and time the object was last modified.
    pub last_modified: LastModified,
    /// Version ID of an object.
    pub version_id: ObjectVersionId,
    pub e_tag: ETag,
    /// The class of storage used to store the object.
    pub storage_class: ObjectVersionStorageClass,
    /// The object key.
    pub key: ObjectKey,
    pub owner: Owner,
    /// Specifies whether the object is (true) or is not (false) the latest version of
    /// an object.
    pub is_latest: IsLatest,
    /// Size in bytes of the object.
    pub size: Size,
}

/// Parse `ObjectVersion` from XML
pub struct ObjectVersionParser;
impl ObjectVersionParser {
    pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<ObjectVersion, XmlParseError> {
        try!(start_element(tag_name, stack));
        let mut obj = ObjectVersion::default();
        loop {
            let current_name = try!(peek_at_name(stack));
            if current_name == "LastModified" {
                obj.last_modified = try!(LastModifiedParser::parse_xml("LastModified", stack));
                continue;
            }
            if current_name == "VersionId" {
                obj.version_id = try!(ObjectVersionIdParser::parse_xml("VersionId", stack));
                continue;
            }
            if current_name == "ETag" {
                obj.e_tag = try!(ETagParser::parse_xml("ETag", stack));
                continue;
            }
            if current_name == "StorageClass" {
                obj.storage_class = try!(ObjectVersionStorageClassParser::parse_xml("StorageClass", stack));
                continue;
            }
            if current_name == "Key" {
                obj.key = try!(ObjectKeyParser::parse_xml("Key", stack));
                continue;
            }
            if current_name == "Owner" {
                obj.owner = try!(OwnerParser::parse_xml("Owner", stack));
                continue;
            }
            if current_name == "IsLatest" {
                obj.is_latest = try!(IsLatestParser::parse_xml("IsLatest", stack));
                continue;
            }
            if current_name == "Size" {
                obj.size = try!(SizeParser::parse_xml("Size", stack));
                continue;
            }
            break;
        }
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `ObjectVersion` contents to a `SignedRequest`
pub struct ObjectVersionWriter;

impl ObjectVersionWriter {
    pub fn write_params(params: &mut Params, name: &str, obj: &ObjectVersion) {
        let mut prefix = name.to_string();
        if prefix != "" { prefix.push_str("."); }
        LastModifiedWriter::write_params(params, &(prefix.to_string() + "LastModified"), &obj.last_modified);
        ObjectVersionIdWriter::write_params(params, &(prefix.to_string() + "VersionId"), &obj.version_id);
        ETagWriter::write_params(params, &(prefix.to_string() + "ETag"), &obj.e_tag);
        ObjectVersionStorageClassWriter::write_params(params, &(prefix.to_string() + "StorageClass"), &obj.storage_class);
        ObjectKeyWriter::write_params(params, &(prefix.to_string() + "Key"), &obj.key);
        OwnerWriter::write_params(params, &(prefix.to_string() + "Owner"), &obj.owner);
        IsLatestWriter::write_params(params, &(prefix.to_string() + "IsLatest"), &obj.is_latest);
        SizeWriter::write_params(params, &(prefix.to_string() + "Size"), &obj.size);
    }
}

pub type ObjectKey = String;
/// Parse `ObjectKey` from XML
pub struct ObjectKeyParser;
impl ObjectKeyParser {
    pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<ObjectKey, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `ObjectKey` contents to a `SignedRequest`
pub struct ObjectKeyWriter;
impl ObjectKeyWriter {
    pub fn write_params(params: &mut Params, name: &str, obj: &ObjectKey) {
        params.put(name, obj);
    }
}

pub type ObjectVersionStorageClass = String;
/// Parse `ObjectVersionStorageClass` from XML
pub struct ObjectVersionStorageClassParser;
impl ObjectVersionStorageClassParser {
    pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<ObjectVersionStorageClass, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `ObjectVersionStorageClass` contents to a `SignedRequest`
pub struct ObjectVersionStorageClassWriter;
impl ObjectVersionStorageClassWriter {
    pub fn write_params(params: &mut Params, name: &str, obj: &ObjectVersionStorageClass) {
        params.put(name, obj);
    }
}

pub type Size = i32;
/// Parse `Size` from XML
pub struct SizeParser;
impl SizeParser {
    pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<Size, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = i32::from_str(try!(characters(stack)).as_ref()).unwrap();
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `Size` contents to a `SignedRequest`
pub struct SizeWriter;
impl SizeWriter {
    pub fn write_params(params: &mut Params, name: &str, obj: &Size) {
        params.put(name, &obj.to_string());
    }
}

pub type IsLatest = bool;
/// Parse `IsLatest` from XML
pub struct IsLatestParser;
impl IsLatestParser {
    pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<IsLatest, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = bool::from_str(try!(characters(stack)).as_ref()).unwrap();
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `IsLatest` contents to a `SignedRequest`
pub struct IsLatestWriter;
impl IsLatestWriter {
    pub fn write_params(params: &mut Params, name: &str, obj: &IsLatest) {
        params.put(name, &obj.to_string());
    }
}

pub type ETag = String;
/// Parse `ETag` from XML
pub struct ETagParser;
impl ETagParser {
    pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<ETag, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `ETag` contents to a `SignedRequest`
pub struct ETagWriter;
impl ETagWriter {
    pub fn write_params(params: &mut Params, name: &str, obj: &ETag) {
        params.put(name, obj);
    }
}

pub type ObjectVersionId = String;
/// Parse `ObjectVersionId` from XML
pub struct ObjectVersionIdParser;
impl ObjectVersionIdParser {
    pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<ObjectVersionId, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `ObjectVersionId` contents to a `SignedRequest`
pub struct ObjectVersionIdWriter;
impl ObjectVersionIdWriter {
    pub fn write_params(params: &mut Params, name: &str, obj: &ObjectVersionId) {
        params.put(name, obj);
    }
}

pub type LastModified = String;
/// Parse `LastModified` from XML
pub struct LastModifiedParser;
impl LastModifiedParser {
    pub fn parse_xml<T: Peek + Next>(tag_name: &str, stack: &mut T) -> Result<LastModified, XmlParseError> {
        try!(start_element(tag_name, stack));
        let obj = try!(characters(stack));
        try!(end_element(tag_name, stack));
        Ok(obj)
    }
}
/// Write `LastModified` contents to a `SignedRequest`
pub struct LastModifiedWriter;
impl LastModifiedWriter {
    pub fn write_params(params: &mut Params, name: &str, obj: &LastModified) {
        params.put(name, obj);
    }
}