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

use self::super::{Iter, Github, Result};
use branches::Branches;
use git::Git;
use hooks::Hooks;
use deployments::Deployments;
use keys::Keys;
use issues::{IssueRef, Issues};
use labels::Labels;
use pulls::PullRequests;
use releases::Releases;
use teams::RepoTeams;
use statuses::Statuses;
use users::User;

use std::fmt;
use super::SortDirection;
use url::form_urlencoded;
use std::collections::HashMap;
use url::Url;

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

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

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

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

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

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

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

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

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

/// Describes types of organization repositories
#[derive(Clone, 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 {
        write!(f,
               "{}",
               match *self {
                   OrgRepoType::All => "all",
                   OrgRepoType::Public => "public",
                   OrgRepoType::Private => "private",
                   OrgRepoType::Forks => "forks",
                   OrgRepoType::Sources => "sources",
                   OrgRepoType::Member => "member",
               })
    }
}

pub struct Repositories<'a> {
    github: &'a Github,
}

impl<'a> Repositories<'a> {
    pub fn new(github: &'a Github) -> Repositories<'a> {
        Repositories { github: 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) -> Result<Repo> {
        let data = try!(serde_json::to_string(&repo));
        self.github.post::<Repo>(&self.path(""), data.as_bytes())
    }

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

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

/// Provides access to the authenticated user's repositories
pub struct OrgRepositories<'a> {
    github: &'a Github,
    org: String,
}

impl<'a> OrgRepositories<'a> {
    pub fn new<O>(github: &'a Github, org: O) -> Self
        where O: Into<String>
    {
        OrgRepositories {
            github: 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) -> Result<Vec<Repo>> {
        let mut uri = vec![self.path("")];
        if let Some(query) = options.serialize() {
            uri.push(query);
        }
        self.github.get::<Vec<Repo>>(&uri.join("?"))
    }

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

/// Provides access to the authenticated user's repositories
pub struct UserRepositories<'a> {
    github: &'a Github,
    owner: String,
}

impl<'a> UserRepositories<'a> {
    pub fn new<O>(github: &'a Github, owner: O) -> UserRepositories<'a>
        where O: Into<String>
    {
        UserRepositories {
            github: 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) -> Result<Vec<Repo>> {
        let mut uri = vec![self.path("")];
        if let Some(query) = options.serialize() {
            uri.push(query);
        }
        self.github.get::<Vec<Repo>>(&uri.join("?"))
    }

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

/// Provides access to an organization's repositories
pub struct OrganizationRepositories<'a> {
    github: &'a Github,
    org: String,
}

impl<'a> OrganizationRepositories<'a> {
    pub fn new<O>(github: &'a Github, org: O) -> OrganizationRepositories<'a>
        where O: Into<String>
    {
        OrganizationRepositories {
            github: 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) -> Result<Vec<Repo>> {
        let mut uri = vec![self.path("")];
        if let Some(query) = options.serialize() {
            uri.push(query);
        }
        self.github.get::<Vec<Repo>>(&uri.join("?"))
    }

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

pub struct Repository<'a> {
    github: &'a Github,
    owner: String,
    repo: String,
}

impl<'a> Repository<'a> {
    pub fn new<O, R>(github: &'a Github, owner: O, repo: R) -> Repository<'a>
        where O: Into<String>,
              R: Into<String>
    {
        Repository {
            github: github,
            owner: owner.into(),
            repo: repo.into(),
        }
    }

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

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

    /// get a reference to repo hook operations
    pub fn hooks(&self) -> Hooks {
        Hooks::new(self.github, 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 {
        Deployments::new(self.github, self.owner.as_str(), self.repo.as_str())
    }

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

    /// get a reference to github issues associated with this repoistory ref
    pub fn issues(&self) -> Issues {
        Issues::new(self.github, 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 {
        Keys::new(self.github, self.owner.as_str(), self.repo.as_str())
    }

    /// get a list of labels associated with this repository ref
    pub fn labels(&self) -> Labels {
        Labels::new(self.github, 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 {
        PullRequests::new(self.github, 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 {
        Releases::new(self.github, self.owner.as_str(), self.repo.as_str())
    }

    /// get a reference to [statuses](https://developer.github.com/v3/repos/statuses/)
    /// associated with this reposoitory ref
    pub fn statuses(&self) -> Statuses {
        Statuses::new(self.github, 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 {
        RepoTeams::new(self.github, self.owner.as_str(), self.repo.as_str())
    }
}


// representations

#[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.
    pub fn languages(&self, github: &Github) -> Result<HashMap<String, i64>> {
        let url = Url::parse(&self.languages_url).unwrap();
        let uri: String = url.path().into();
        github.get(&uri)
    }
}


#[derive(Debug, 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>,
    #[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>,
}

#[derive(Default)]
pub struct RepoOptionsBuilder {
    name: String,
    description: Option<String>,
    homepage: Option<String>,
    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<String>,
    license_template: Option<String>,
}

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

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

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

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

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

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

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

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

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

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

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

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

impl RepoOptions {
    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>)
                               -> RepoOptions
        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: private,
            has_issues: has_issues,
            has_wiki: has_wiki,
            has_downloads: has_downloads,
            team_id: team_id,
            auto_init: 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::new()
    }

    /// 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 {
    params: HashMap<&'static str, String>,
}

impl RepoListOptionsBuilder {
    pub fn new() -> RepoListOptionsBuilder {
        RepoListOptionsBuilder { ..Default::default() }
    }

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

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

    pub fn affiliation(&mut self, affiliations: Vec<Affiliation>) -> &mut RepoListOptionsBuilder {
        self.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 RepoListOptionsBuilder {
        self.params.insert("type", tpe.to_string());
        self
    }

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

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

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

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

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

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

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

    /// 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 {
    params: HashMap<&'static str, String>,
}

impl OrgRepoListOptionsBuilder {
    pub fn new() -> Self {
        OrgRepoListOptionsBuilder { ..Default::default() }
    }

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

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

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

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

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

    /// 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 {
    params: HashMap<&'static str, String>,
}

impl UserRepoListOptionsBuilder {
    pub fn new() -> UserRepoListOptionsBuilder {
        UserRepoListOptionsBuilder { ..Default::default() }
    }

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

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

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

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

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

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

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

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

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

    /// 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 {
    params: HashMap<&'static str, String>,
}

impl OrganizationRepoListOptionsBuilder {
    pub fn new() -> OrganizationRepoListOptionsBuilder {
        OrganizationRepoListOptionsBuilder { ..Default::default() }
    }

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

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

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