diff options
| author | nsfisis <nsfisis@gmail.com> | 2026-05-12 04:34:12 +0900 |
|---|---|---|
| committer | nsfisis <nsfisis@gmail.com> | 2026-05-12 04:38:06 +0900 |
| commit | 55d44d86077e8512c15389f9b2f9c9157b4a3137 (patch) | |
| tree | 2948b71fcd408a197635e6c61bd186c728cf9145 | |
| parent | fbf8b80058669f258786a7d07c3467d5d09f2a4d (diff) | |
| download | php-shirabe-55d44d86077e8512c15389f9b2f9c9157b4a3137.tar.gz php-shirabe-55d44d86077e8512c15389f9b2f9c9157b4a3137.tar.zst php-shirabe-55d44d86077e8512c15389f9b2f9c9157b4a3137.zip | |
feat(port): port ForgejoRepositoryData.php
| -rw-r--r-- | crates/shirabe-php-shim/src/lib.rs | 48 | ||||
| -rw-r--r-- | crates/shirabe/src/util/forgejo_repository_data.rs | 59 |
2 files changed, 107 insertions, 0 deletions
diff --git a/crates/shirabe-php-shim/src/lib.rs b/crates/shirabe-php-shim/src/lib.rs index ef74970..35202ba 100644 --- a/crates/shirabe-php-shim/src/lib.rs +++ b/crates/shirabe-php-shim/src/lib.rs @@ -11,6 +11,54 @@ pub enum PhpMixed { Array(IndexMap<String, Box<PhpMixed>>), } +impl PhpMixed { + pub fn as_bool(&self) -> Option<bool> { + match self { + PhpMixed::Bool(b) => Some(*b), + _ => None, + } + } + + pub fn as_int(&self) -> Option<i64> { + match self { + PhpMixed::Int(i) => Some(*i), + _ => None, + } + } + + pub fn as_float(&self) -> Option<f64> { + match self { + PhpMixed::Float(f) => Some(*f), + _ => None, + } + } + + pub fn as_string(&self) -> Option<&str> { + match self { + PhpMixed::String(s) => Some(s.as_str()), + _ => None, + } + } + + pub fn as_list(&self) -> Option<&Vec<Box<PhpMixed>>> { + match self { + PhpMixed::List(l) => Some(l), + _ => None, + } + } + + pub fn as_array(&self) -> Option<&IndexMap<String, Box<PhpMixed>>> { + match self { + PhpMixed::Array(a) => Some(a), + _ => None, + } + } + + pub fn is_null(&self) -> bool { + matches!(self, PhpMixed::Null) + } +} + #[derive(Debug)] pub struct Exception { pub message: String, diff --git a/crates/shirabe/src/util/forgejo_repository_data.rs b/crates/shirabe/src/util/forgejo_repository_data.rs index ddbe585..6303bfa 100644 --- a/crates/shirabe/src/util/forgejo_repository_data.rs +++ b/crates/shirabe/src/util/forgejo_repository_data.rs @@ -1 +1,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")?, + )) + } +} |
