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
//! Repository interface
extern crate futures;
extern crate serde_json;

use std::collections::HashMap;
use std::fmt;

use futures::future;
use hyper::client::connect::Connect;
use url::{form_urlencoded, Url};

use branches::Branches;
use content::Content;
use deployments::Deployments;
use git::Git;
use hooks::Hooks;
use issues::{IssueRef, Issues};
use keys::Keys;
use labels::Labels;
use pulls::PullRequests;
use releases::Releases;
use statuses::Statuses;
use teams::RepoTeams;
use traffic::Traffic;
use users::Contributors;
use users::User;
use {unfold, Future, Github, SortDirection, Stream};

fn identity<T>(x: T) -> T {
    x
}

/// describes repository visibilities
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Visibility {
    All,
    Public,
    Private,
}

impl fmt::Display for Visibility {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Visibility::All => "all",
            Visibility::Public => "public",
            Visibility::Private => "private",
        }
        .fmt(f)
    }
}

/// Describes sorting options for repositories
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Sort {
    Created,
    Updated,
    Pushed,
    FullName,
}

impl fmt::Display for Sort {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Sort::Created => "created",
            Sort::Updated => "updated",
            Sort::Pushed => "pushed",
            Sort::FullName => "full_name",
        }
        .fmt(f)
    }
}

/// Describes member affiliation types for repositories
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Affiliation {
    Owner,
    Collaborator,
    OrganizationMember,
}

impl fmt::Display for Affiliation {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Affiliation::Owner => "owner",
            Affiliation::Collaborator => "collaborator",
            Affiliation::OrganizationMember => "organization_member",
        }
        .fmt(f)
    }
}

/// Describes types of repositories
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum Type {
    All,
    Owner,
    Public,
    Private,
    Member,
}

impl fmt::Display for Type {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            Type::All => "all",
            Type::Owner => "owner",
            Type::Public => "public",
            Type::Private => "private",
            Type::Member => "member",
        }
        .fmt(f)
    }
}

/// Describes types of organization repositories
#[derive(Clone, Copy, Debug, PartialEq)]
pub enum OrgRepoType {
    All,
    Public,
    Private,
    Forks,
    Sources,
    Member,
}

impl fmt::Display for OrgRepoType {
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
        match *self {
            OrgRepoType::All => "all",
            OrgRepoType::Public => "public",
            OrgRepoType::Private => "private",
            OrgRepoType::Forks => "forks",
            OrgRepoType::Sources => "sources",
            OrgRepoType::Member => "member",
        }
        .fmt(f)
    }
}

#[derive(Clone)]
pub struct Repositories<C>
where
    C: Clone + Connect + 'static,
{
    github: Github<C>,
}

impl<C: Clone + Connect + 'static> Repositories<C> {
    #[doc(hidden)]
    pub fn new(github: Github<C>) -> Self {
        Self { github }
    }

    fn path(&self, more: &str) -> String {
        format!("/user/repos{}", more)
    }

    /// Create a new repository
    /// https://developer.github.com/v3/repos/#create
    pub fn create(&self, repo: &RepoOptions) -> Future<Repo> {
        self.github.post(&self.path(""), json!(repo))
    }

    /// list the authenticated users repositories
    /// https://developer.github.com/v3/repos/#list-your-repositories
    pub fn list(&self, options: &RepoListOptions) -> Future<Vec<Repo>> {
        let mut uri = vec![self.path("")];
        if let Some(query) = options.serialize() {
            uri.push(query);
        }
        self.github.get(&uri.join("?"))
    }

    /// provides a stream over all pages of the authenticated users repositories
    /// https://developer.github.com/v3/repos/#list-your-repositories
    pub fn iter(&self, options: &RepoListOptions) -> Stream<Repo> {
        let mut uri = vec![self.path("")];
        if let Some(query) = options.serialize() {
            uri.push(query);
        }
        unfold(
            self.github.clone(),
            self.github.get_pages(&uri.join("?")),
            identity,
        )
    }
}

/// Provides access to the authenticated user's repositories
pub struct OrgRepositories<C>
where
    C: Clone + Connect + 'static,
{
    github: Github<C>,
    org: String,
}

impl<C: Clone + Connect + 'static> OrgRepositories<C> {
    #[doc(hidden)]
    pub fn new<O>(github: Github<C>, org: O) -> Self
    where
        O: Into<String>,
    {
        OrgRepositories {
            github,
            org: org.into(),
        }
    }

    fn path(&self, more: &str) -> String {
        format!("/orgs/{}/repos{}", self.org, more)
    }

    /// https://developer.github.com/v3/repos/#list-organization-repositories
    pub fn list(&self, options: &OrgRepoListOptions) -> Future<Vec<Repo>> {
        let mut uri = vec![self.path("")];
        if let Some(query) = options.serialize() {
            uri.push(query);
        }
        self.github.get(&uri.join("?"))
    }

    /// provides a stream over all pages of an orgs's repositories
    /// https://developer.github.com/v3/repos/#list-organization-repositories
    pub fn iter(&self, options: &OrgRepoListOptions) -> Stream<Repo> {
        let mut uri = vec![self.path("")];
        if let Some(query) = options.serialize() {
            uri.push(query);
        }
        unfold(
            self.github.clone(),
            self.github.get_pages(&uri.join("?")),
            identity,
        )
    }

    /// Create a new org repository
    /// https://developer.github.com/v3/repos/#create
    pub fn create(&self, repo: &RepoOptions) -> Future<Repo> {
        self.github.post(&self.path(""), json!(repo))
    }
}

/// Provides access to the authenticated user's repositories
pub struct UserRepositories<C>
where
    C: Clone + Connect + 'static,
{
    github: Github<C>,
    owner: String,
}

impl<C: Clone + Connect + 'static> UserRepositories<C> {
    #[doc(hidden)]
    pub fn new<O>(github: Github<C>, owner: O) -> Self
    where
        O: Into<String>,
    {
        UserRepositories {
            github,
            owner: owner.into(),
        }
    }

    fn path(&self, more: &str) -> String {
        format!("/users/{}/repos{}", self.owner, more)
    }

    /// https://developer.github.com/v3/repos/#list-user-repositories
    pub fn list(&self, options: &UserRepoListOptions) -> Future<Vec<Repo>> {
        let mut uri = vec![self.path("")];
        if let Some(query) = options.serialize() {
            uri.push(query);
        }
        self.github.get(&uri.join("?"))
    }

    /// provides a stream over all pages of a user's repositories
    /// https://developer.github.com/v3/repos/#list-your-repositories
    pub fn iter(&self, options: &UserRepoListOptions) -> Stream<Repo> {
        let mut uri = vec![self.path("")];
        if let Some(query) = options.serialize() {
            uri.push(query);
        }
        unfold(
            self.github.clone(),
            self.github.get_pages(&uri.join("?")),
            identity,
        )
    }
}

/// Provides access to an organization's repositories
pub struct OrganizationRepositories<C>
where
    C: Clone + Connect + 'static,
{
    github: Github<C>,
    org: String,
}

impl<C: Clone + Connect + 'static> OrganizationRepositories<C> {
    #[doc(hidden)]
    pub fn new<O>(github: Github<C>, org: O) -> Self
    where
        O: Into<String>,
    {
        OrganizationRepositories {
            github,
            org: org.into(),
        }
    }

    fn path(&self, more: &str) -> String {
        format!("/orgs/{}/repos{}", self.org, more)
    }

    /// list an organization's repositories
    /// https://developer.github.com/v3/repos/#list-organization-repositories
    pub fn list(&self, options: &OrganizationRepoListOptions) -> Future<Vec<Repo>> {
        let mut uri = vec![self.path("")];
        if let Some(query) = options.serialize() {
            uri.push(query);
        }
        self.github.get(&uri.join("?"))
    }

    /// Provides a stream over all pages of an organization's repositories
    /// https://developer.github.com/v3/repos/#list-organization-repositories
    pub fn iter(&self, options: &OrganizationRepoListOptions) -> Stream<Repo> {
        let mut uri = vec![self.path("")];
        if let Some(query) = options.serialize() {
            uri.push(query);
        }
        unfold(
            self.github.clone(),
            self.github.get_pages(&uri.join("?")),
            identity,
        )
    }
}

pub struct Repository<C>
where
    C: Clone + Connect + 'static,
{
    github: Github<C>,
    owner: String,
    repo: String,
}

impl<C: Clone + Connect + 'static> Repository<C> {
    #[doc(hidden)]
    pub fn new<O, R>(github: Github<C>, owner: O, repo: R) -> Self
    where
        O: Into<String>,
        R: Into<String>,
    {
        Repository {
            github,
            owner: owner.into(),
            repo: repo.into(),
        }
    }

    fn path(&self, more: &str) -> String {
        format!("/repos/{}/{}{}", self.owner, self.repo, more)
    }

    /// get a reference to the GitHub repository object that this `Repository` refers to
    pub fn get(&self) -> Future<Repo> {
        self.github.get(&self.path(""))
    }

    /// https://developer.github.com/v3/repos/#edit
    pub fn edit(&self, options: &RepoEditOptions) -> Future<Repo> {
        // Note that this intentionally calls POST rather than PATCH,
        // even though the docs say PATCH.
        // In my tests (changing the default branch) POST works while PATCH doesn't.
        self.github.post(&self.path(""), json!(options))
    }

    /// get a reference to branch operations
    pub fn branches(&self) -> Branches<C> {
        Branches::new(self.github.clone(), self.owner.as_str(), self.repo.as_str())
    }

    /// get a reference to content operations
    pub fn content(&self) -> Content<C> {
        Content::new(self.github.clone(), self.owner.as_str(), self.repo.as_str())
    }

    /// get a reference to git operations
    pub fn git(&self) -> Git<C> {
        Git::new(self.github.clone(), self.owner.as_str(), self.repo.as_str())
    }

    /// get a reference to repo hook operations
    pub fn hooks(&self) -> Hooks<C> {
        Hooks::new(self.github.clone(), self.owner.as_str(), self.repo.as_str())
    }

    /// get a reference to [deployments](https://developer.github.com/v3/repos/deployments/)
    /// associated with this repository ref
    pub fn deployments(&self) -> Deployments<C> {
        Deployments::new(self.github.clone(), self.owner.as_str(), self.repo.as_str())
    }

    /// get a reference to a specific github issue associated with this repository ref
    pub fn issue(&self, number: u64) -> IssueRef<C> {
        IssueRef::new(
            self.github.clone(),
            self.owner.as_str(),
            self.repo.as_str(),
            number,
        )
    }

    /// get a reference to github issues associated with this repository ref
    pub fn issues(&self) -> Issues<C> {
        Issues::new(self.github.clone(), self.owner.as_str(), self.repo.as_str())
    }

    /// get a reference to [deploy keys](https://developer.github.com/v3/repos/keys/)
    /// associated with this repository ref
    pub fn keys(&self) -> Keys<C> {
        Keys::new(self.github.clone(), self.owner.as_str(), self.repo.as_str())
    }

    /// get a list of labels associated with this repository ref
    pub fn labels(&self) -> Labels<C> {
        Labels::new(self.github.clone(), self.owner.as_str(), self.repo.as_str())
    }

    /// get a list of [pulls](https://developer.github.com/v3/pulls/)
    /// associated with this repository ref
    pub fn pulls(&self) -> PullRequests<C> {
        PullRequests::new(self.github.clone(), self.owner.as_str(), self.repo.as_str())
    }

    /// get a reference to [releases](https://developer.github.com/v3/repos/releases/)
    /// associated with this repository ref
    pub fn releases(&self) -> Releases<C> {
        Releases::new(self.github.clone(), self.owner.as_str(), self.repo.as_str())
    }

    /// get a reference to [statuses](https://developer.github.com/v3/repos/statuses/)
    /// associated with this repository ref
    pub fn statuses(&self) -> Statuses<C> {
        Statuses::new(self.github.clone(), self.owner.as_str(), self.repo.as_str())
    }

    /// get a reference to [teams](https://developer.github.com/v3/repos/#list-teams)
    /// associated with this repository ref
    pub fn teams(&self) -> RepoTeams<C> {
        RepoTeams::new(self.github.clone(), self.owner.as_str(), self.repo.as_str())
    }

    /// get a reference to
    /// [contributors](https://developer.github.com/v3/repos/#list-contributors)
    /// associated with this repository ref
    pub fn contributors(&self) -> Contributors<C> {
        Contributors::new(self.github.clone(), self.owner.as_str(), self.repo.as_str())
    }

    /// get a reference of [traffic](https://developer.github.com/v3/repos/traffic/)
    /// associated with this repository ref
    pub fn traffic(&self) -> Traffic<C> {
        Traffic::new(self.github.clone(), self.owner.as_str(), self.repo.as_str())
    }
}

// representations (todo: replace with derive_builder)

#[derive(Debug, Deserialize)]
pub struct Repo {
    pub id: u64,
    pub owner: User,
    pub name: String,
    pub full_name: String,
    pub description: Option<String>,
    pub private: bool,
    pub fork: bool,
    pub url: String,
    pub html_url: String,
    pub archive_url: String,
    pub assignees_url: String,
    pub blobs_url: String,
    pub branches_url: String,
    pub clone_url: String,
    pub collaborators_url: String,
    pub comments_url: String,
    pub commits_url: String,
    pub compare_url: String,
    pub contents_url: String,
    pub contributors_url: String,
    pub deployments_url: String,
    pub downloads_url: String,
    pub events_url: String,
    pub forks_url: String,
    pub git_commits_url: String,
    pub git_refs_url: String,
    pub git_tags_url: String,
    pub git_url: String,
    pub hooks_url: String,
    pub issue_comment_url: String,
    pub issue_events_url: String,
    pub issues_url: String,
    pub keys_url: String,
    pub labels_url: String,
    pub languages_url: String,
    pub merges_url: String,
    pub milestones_url: String,
    pub mirror_url: Option<String>,
    pub notifications_url: String,
    pub pulls_url: String,
    pub releases_url: String,
    pub ssh_url: String,
    pub stargazers_url: String,
    pub statuses_url: String,
    pub subscribers_url: String,
    pub subscription_url: String,
    pub svn_url: String,
    pub tags_url: String,
    pub teams_url: String,
    pub trees_url: String,
    pub homepage: Option<String>,
    pub language: Option<String>,
    pub forks_count: u64,
    pub stargazers_count: u64,
    pub watchers_count: u64,
    pub size: u64,
    pub default_branch: String,
    pub open_issues_count: u64,
    pub has_issues: bool,
    pub has_wiki: bool,
    pub has_pages: bool,
    pub has_downloads: bool,
    pub pushed_at: String,
    pub created_at: String,
    pub updated_at: String, // permissions: Permissions
}

impl Repo {
    /// Returns a map containing the
    /// [languages](https://developer.github.com/v3/repos/#list-languages) that the repository is
    /// implemented in.
    ///
    /// The keys are the language names, and the values are the number of bytes of code written in
    /// that language.
    #[cfg_attr(feature = "cargo-clippy", allow(needless_pass_by_value))] // shippied public API
    pub fn languages<C>(&self, github: Github<C>) -> Future<HashMap<String, i64>>
    where
        C: Clone + Connect + 'static,
    {
        let url = Url::parse(&self.languages_url).unwrap();
        let uri: String = url.path().into();
        github.get(&uri)
    }
}

#[derive(Debug, Default, Serialize)]
pub struct RepoOptions {
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub homepage: Option<String>,
    /// false by default
    #[serde(skip_serializing_if = "Option::is_none")]
    pub private: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub has_issues: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub has_wiki: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub has_downloads: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub team_id: Option<i32>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub auto_init: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub gitignore_template: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub license_template: Option<String>,
}

pub struct RepoOptionsBuilder(RepoOptions);

impl RepoOptionsBuilder {
    pub(crate) fn new<N>(name: N) -> Self
    where
        N: Into<String>,
    {
        RepoOptionsBuilder(RepoOptions {
            name: name.into(),
            ..Default::default()
        })
    }

    pub fn description<D>(&mut self, description: D) -> &mut Self
    where
        D: Into<String>,
    {
        self.0.description = Some(description.into());
        self
    }

    pub fn homepage<H>(&mut self, homepage: H) -> &mut Self
    where
        H: Into<String>,
    {
        self.0.homepage = Some(homepage.into());
        self
    }

    pub fn private(&mut self, private: bool) -> &mut Self {
        self.0.private = Some(private);
        self
    }

    pub fn has_issues(&mut self, has_issues: bool) -> &mut Self {
        self.0.has_issues = Some(has_issues);
        self
    }

    pub fn has_wiki(&mut self, has_wiki: bool) -> &mut Self {
        self.0.has_wiki = Some(has_wiki);
        self
    }

    pub fn has_downloads(&mut self, has_downloads: bool) -> &mut Self {
        self.0.has_downloads = Some(has_downloads);
        self
    }

    pub fn team_id(&mut self, team_id: i32) -> &mut Self {
        self.0.team_id = Some(team_id);
        self
    }

    pub fn auto_init(&mut self, auto_init: bool) -> &mut Self {
        self.0.auto_init = Some(auto_init);
        self
    }

    pub fn gitignore_template<GI>(&mut self, gitignore_template: GI) -> &mut Self
    where
        GI: Into<String>,
    {
        self.0.gitignore_template = Some(gitignore_template.into());
        self
    }

    pub fn license_template<L>(&mut self, license_template: L) -> &mut Self
    where
        L: Into<String>,
    {
        self.0.license_template = Some(license_template.into());
        self
    }

    pub fn build(&self) -> RepoOptions {
        RepoOptions::new(
            self.0.name.as_str(),
            self.0.description.clone(),
            self.0.homepage.clone(),
            self.0.private,
            self.0.has_issues,
            self.0.has_wiki,
            self.0.has_downloads,
            self.0.team_id,
            self.0.auto_init,
            self.0.gitignore_template.clone(),
            self.0.license_template.clone(),
        )
    }
}

impl RepoOptions {
    #![cfg_attr(feature = "cargo-clippy", allow(too_many_arguments))] // exempted
    pub fn new<N, D, H, GI, L>(
        name: N,
        description: Option<D>,
        homepage: Option<H>,
        private: Option<bool>,
        has_issues: Option<bool>,
        has_wiki: Option<bool>,
        has_downloads: Option<bool>,
        team_id: Option<i32>,
        auto_init: Option<bool>,
        gitignore_template: Option<GI>,
        license_template: Option<L>,
    ) -> Self
    where
        N: Into<String>,
        D: Into<String>,
        H: Into<String>,
        GI: Into<String>,
        L: Into<String>,
    {
        RepoOptions {
            name: name.into(),
            description: description.map(|h| h.into()),
            homepage: homepage.map(|h| h.into()),
            private,
            has_issues,
            has_wiki,
            has_downloads,
            team_id,
            auto_init,
            gitignore_template: gitignore_template.map(|gi| gi.into()),
            license_template: license_template.map(|l| l.into()),
        }
    }

    pub fn builder<N: Into<String>>(name: N) -> RepoOptionsBuilder {
        RepoOptionsBuilder::new(name)
    }
}

#[derive(Default)]
pub struct RepoListOptions {
    params: HashMap<&'static str, String>,
}

impl RepoListOptions {
    pub fn builder() -> RepoListOptionsBuilder {
        RepoListOptionsBuilder::default()
    }

    /// serialize options as a string. returns None if no options are defined
    pub fn serialize(&self) -> Option<String> {
        if self.params.is_empty() {
            None
        } else {
            let encoded: String = form_urlencoded::Serializer::new(String::new())
                .extend_pairs(&self.params)
                .finish();
            Some(encoded)
        }
    }
}

#[derive(Default)]
pub struct RepoListOptionsBuilder(RepoListOptions);

impl RepoListOptionsBuilder {
    pub fn per_page(&mut self, n: usize) -> &mut Self {
        self.0.params.insert("per_page", n.to_string());
        self
    }

    pub fn visibility(&mut self, vis: Visibility) -> &mut Self {
        self.0.params.insert("visibility", vis.to_string());
        self
    }

    pub fn affiliation(&mut self, affiliations: Vec<Affiliation>) -> &mut Self {
        self.0.params.insert(
            "affiliation",
            affiliations
                .into_iter()
                .map(|a| a.to_string())
                .collect::<Vec<String>>()
                .join(","),
        );
        self
    }

    pub fn repo_type(&mut self, tpe: Sort) -> &mut Self {
        self.0.params.insert("type", tpe.to_string());
        self
    }

    pub fn sort(&mut self, sort: Sort) -> &mut Self {
        self.0.params.insert("sort", sort.to_string());
        self
    }

    pub fn asc(&mut self) -> &mut Self {
        self.direction(SortDirection::Asc)
    }

    pub fn desc(&mut self) -> &mut Self {
        self.direction(SortDirection::Desc)
    }

    pub fn direction(&mut self, direction: SortDirection) -> &mut Self {
        self.0.params.insert("direction", direction.to_string());
        self
    }

    pub fn build(&self) -> RepoListOptions {
        RepoListOptions {
            params: self.0.params.clone(),
        }
    }
}

#[derive(Debug, Default, Serialize)]
pub struct RepoEditOptions {
    pub name: String,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub description: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub homepage: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub private: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub has_issues: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub has_projects: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub has_wiki: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub default_branch: Option<String>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allow_squash_merge: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allow_merge_commit: Option<bool>,
    #[serde(skip_serializing_if = "Option::is_none")]
    pub allow_rebase_merge: Option<bool>,
}

impl RepoEditOptions {
    #![cfg_attr(feature = "cargo-clippy", allow(too_many_arguments))] // exempted
    pub fn new<N, D, H, DB>(
        name: N,
        description: Option<D>,
        homepage: Option<H>,
        private: Option<bool>,
        has_issues: Option<bool>,
        has_projects: Option<bool>,
        has_wiki: Option<bool>,
        default_branch: Option<DB>,
        allow_squash_merge: Option<bool>,
        allow_merge_commit: Option<bool>,
        allow_rebase_merge: Option<bool>,
    ) -> Self
    where
        N: Into<String>,
        D: Into<String>,
        H: Into<String>,
        DB: Into<String>,
    {
        RepoEditOptions {
            name: name.into(),
            description: description.map(|h| h.into()),
            homepage: homepage.map(|h| h.into()),
            private,
            has_issues,
            has_projects,
            has_wiki,
            default_branch: default_branch.map(|d| d.into()),
            allow_squash_merge,
            allow_merge_commit,
            allow_rebase_merge,
        }
    }

    pub fn builder<N: Into<String>>(name: N) -> RepoEditOptionsBuilder {
        RepoEditOptionsBuilder::new(name)
    }
}

pub struct RepoEditOptionsBuilder(RepoEditOptions);

impl RepoEditOptionsBuilder {
    pub(crate) fn new<N>(name: N) -> Self
    where
        N: Into<String>,
    {
        RepoEditOptionsBuilder(RepoEditOptions {
            name: name.into(),
            ..Default::default()
        })
    }

    pub fn description<D>(&mut self, description: D) -> &mut Self
    where
        D: Into<String>,
    {
        self.0.description = Some(description.into());
        self
    }

    pub fn homepage<H>(&mut self, homepage: H) -> &mut Self
    where
        H: Into<String>,
    {
        self.0.homepage = Some(homepage.into());
        self
    }

    pub fn private(&mut self, private: bool) -> &mut Self {
        self.0.private = Some(private);
        self
    }

    pub fn has_issues(&mut self, has_issues: bool) -> &mut Self {
        self.0.has_issues = Some(has_issues);
        self
    }

    pub fn has_projects(&mut self, has_projects: bool) -> &mut Self {
        self.0.has_projects = Some(has_projects);
        self
    }

    pub fn has_wiki(&mut self, has_wiki: bool) -> &mut Self {
        self.0.has_wiki = Some(has_wiki);
        self
    }

    pub fn default_branch<DB>(&mut self, default_branch: DB) -> &mut Self
    where
        DB: Into<String>,
    {
        self.0.default_branch = Some(default_branch.into());
        self
    }

    pub fn allow_squash_merge(&mut self, allow_squash_merge: bool) -> &mut Self {
        self.0.allow_squash_merge = Some(allow_squash_merge);
        self
    }

    pub fn allow_merge_commit(&mut self, allow_merge_commit: bool) -> &mut Self {
        self.0.allow_merge_commit = Some(allow_merge_commit);
        self
    }

    pub fn allow_rebase_merge(&mut self, allow_rebase_merge: bool) -> &mut Self {
        self.0.allow_rebase_merge = Some(allow_rebase_merge);
        self
    }

    pub fn build(&self) -> RepoEditOptions {
        RepoEditOptions::new(
            self.0.name.as_str(),
            self.0.description.clone(),
            self.0.homepage.clone(),
            self.0.private,
            self.0.has_issues,
            self.0.has_projects,
            self.0.has_wiki,
            self.0.default_branch.clone(),
            self.0.allow_squash_merge,
            self.0.allow_merge_commit,
            self.0.allow_rebase_merge,
        )
    }
}

#[derive(Default)]
pub struct OrgRepoListOptions {
    params: HashMap<&'static str, String>,
}

impl OrgRepoListOptions {
    pub fn builder() -> OrgRepoListOptionsBuilder {
        OrgRepoListOptionsBuilder::default()
    }

    /// serialize options as a string. returns None if no options are defined
    pub fn serialize(&self) -> Option<String> {
        if self.params.is_empty() {
            None
        } else {
            let encoded: String = form_urlencoded::Serializer::new(String::new())
                .extend_pairs(&self.params)
                .finish();
            Some(encoded)
        }
    }
}

#[derive(Default)]
pub struct OrgRepoListOptionsBuilder(OrgRepoListOptions);

impl OrgRepoListOptionsBuilder {
    pub fn per_page(&mut self, n: usize) -> &mut Self {
        self.0.params.insert("per_page", n.to_string());
        self
    }

    pub fn repo_type(&mut self, tpe: OrgRepoType) -> &mut Self {
        self.0.params.insert("type", tpe.to_string());
        self
    }

    pub fn build(&self) -> OrgRepoListOptions {
        OrgRepoListOptions {
            params: self.0.params.clone(),
        }
    }
}

#[derive(Default)]
pub struct UserRepoListOptions {
    params: HashMap<&'static str, String>,
}

impl UserRepoListOptions {
    pub fn builder() -> UserRepoListOptionsBuilder {
        UserRepoListOptionsBuilder::default()
    }

    /// serialize options as a string. returns None if no options are defined
    pub fn serialize(&self) -> Option<String> {
        if self.params.is_empty() {
            None
        } else {
            let encoded: String = form_urlencoded::Serializer::new(String::new())
                .extend_pairs(&self.params)
                .finish();
            Some(encoded)
        }
    }
}

#[derive(Default)]
pub struct UserRepoListOptionsBuilder(UserRepoListOptions);

impl UserRepoListOptionsBuilder {
    pub fn repo_type(&mut self, tpe: Type) -> &mut Self {
        self.0.params.insert("type", tpe.to_string());
        self
    }

    pub fn per_page(&mut self, n: usize) -> &mut Self {
        self.0.params.insert("per_page", n.to_string());
        self
    }

    pub fn sort(&mut self, sort: Type) -> &mut Self {
        self.0.params.insert("sort", sort.to_string());
        self
    }

    pub fn asc(&mut self) -> &mut Self {
        self.direction(SortDirection::Asc)
    }

    pub fn desc(&mut self) -> &mut Self {
        self.direction(SortDirection::Desc)
    }

    pub fn direction(&mut self, direction: SortDirection) -> &mut Self {
        self.0.params.insert("direction", direction.to_string());
        self
    }

    pub fn build(&self) -> UserRepoListOptions {
        UserRepoListOptions {
            params: self.0.params.clone(),
        }
    }
}

#[derive(Default)]
pub struct OrganizationRepoListOptions {
    params: HashMap<&'static str, String>,
}

impl OrganizationRepoListOptions {
    pub fn builder() -> OrganizationRepoListOptionsBuilder {
        OrganizationRepoListOptionsBuilder::default()
    }

    /// serialize options as a string. returns None if no options are defined
    pub fn serialize(&self) -> Option<String> {
        if self.params.is_empty() {
            None
        } else {
            let encoded: String = form_urlencoded::Serializer::new(String::new())
                .extend_pairs(&self.params)
                .finish();
            Some(encoded)
        }
    }
}

#[derive(Default)]
pub struct OrganizationRepoListOptionsBuilder(OrganizationRepoListOptions);

impl OrganizationRepoListOptionsBuilder {
    pub fn per_page(&mut self, n: usize) -> &mut Self {
        self.0.params.insert("per_page", n.to_string());
        self
    }

    pub fn repo_type(&mut self, tpe: OrgRepoType) -> &mut Self {
        self.0.params.insert("type", tpe.to_string());
        self
    }

    pub fn build(&self) -> OrganizationRepoListOptions {
        OrganizationRepoListOptions {
            params: self.0.params.clone(),
        }
    }
}