aboutsummaryrefslogtreecommitdiffhomepage
path: root/crates/shirabe/src/util/forgejo_repository_data.rs
blob: 6303bfad11bed885a6c8bff7cc93cb47412bad4c (plain)
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
//! ref: composer/src/Composer/Util/ForgejoRepositoryData.php

use indexmap::IndexMap;
use shirabe_php_shim::PhpMixed;

#[derive(Debug)]
pub struct ForgejoRepositoryData {
    pub html_url: String,
    pub ssh_url: String,
    pub http_clone_url: String,
    pub is_private: bool,
    pub default_branch: String,
    pub has_issues: bool,
    pub is_archived: bool,
}

impl ForgejoRepositoryData {
    pub fn new(
        html_url: String,
        http_clone_url: String,
        ssh_url: String,
        is_private: bool,
        default_branch: String,
        has_issues: bool,
        is_archived: bool,
    ) -> Self {
        Self {
            html_url,
            http_clone_url,
            ssh_url,
            is_private,
            default_branch,
            has_issues,
            is_archived,
        }
    }

    pub fn from_remote_data(data: &IndexMap<String, PhpMixed>) -> anyhow::Result<Self> {
        let get_string = |key: &str| {
            data.get(key)
                .and_then(|v| v.as_string())
                .map(|s| s.to_owned())
                .ok_or_else(|| anyhow::anyhow!("missing or invalid string field: {key}"))
        };
        let get_bool = |key: &str| {
            data.get(key)
                .and_then(|v| v.as_bool())
                .ok_or_else(|| anyhow::anyhow!("missing or invalid bool field: {key}"))
        };
        Ok(Self::new(
            get_string("html_url")?,
            get_string("clone_url")?,
            get_string("ssh_url")?,
            get_bool("private")?,
            get_string("default_branch")?,
            get_bool("has_issues")?,
            get_bool("archived")?,
        ))
    }
}