Skip to content

Commit 22b3fd0

Browse files
committed
feat: 函数优化和单侧用例编写
1 parent fd776c0 commit 22b3fd0

8 files changed

Lines changed: 149 additions & 14 deletions

File tree

Cargo.lock

Lines changed: 26 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

Cargo.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,5 +4,6 @@ version = "0.1.0"
44
edition = "2024"
55

66
[dependencies]
7+
addr = "0.15.6"
78
git2 = "0.20.4"
89
webbrowser = "1.2.0"

src/format.rs

Lines changed: 22 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,13 +1,28 @@
1+
use addr::parse_domain_name;
12
use git2::Repository;
3+
use std::net::IpAddr;
4+
5+
#[derive(Debug, PartialEq)]
6+
pub enum AddressType {
7+
IpAddress,
8+
DomainName,
9+
Invalid,
10+
}
211
/// 获取当前 Git 仓库的远程仓库 URL(默认 origin)
312
pub fn get_git_remote_url() -> Option<String> {
413
let repo = Repository::open_from_env().ok()?;
514
let remote = repo.find_remote("origin").ok()?;
615
remote.url().map(|s| s.to_string())
716
}
8-
// 判断是否数字ip地址
9-
pub fn is_number_ip(ip: &str) -> bool {
10-
ip.chars().all(|c| c.is_ascii_digit())
17+
// 获取域名类型
18+
pub fn get_domain_type(s: &str) -> AddressType {
19+
let domain = parse_domain_name(s);
20+
if domain.is_ok() && domain.unwrap().has_known_suffix() {
21+
return AddressType::DomainName;
22+
} else if s.parse::<IpAddr>().is_ok() {
23+
return AddressType::IpAddress;
24+
}
25+
AddressType::Invalid
1126
}
1227
// 格式化url为https开头
1328
pub fn format_git_url(url: String) -> String {
@@ -17,10 +32,10 @@ pub fn format_git_url(url: String) -> String {
1732
if result_url.starts_with(prefix) {
1833
let without_git = &result_url[4..];
1934
if let Some((domain, path)) = without_git.split_once(':') {
20-
let http_prefix = if is_number_ip(domain) {
21-
"http://"
22-
} else {
23-
"https://"
35+
let http_prefix = match get_domain_type(domain) {
36+
AddressType::DomainName => "https://",
37+
AddressType::IpAddress => "http://",
38+
AddressType::Invalid => "",
2439
};
2540
result_url = format!("{}{}/{}", http_prefix, domain, path);
2641
}

src/lib.rs

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
// src/lib.rs
2+
// 只在这里定义一次!
3+
pub mod format;
4+
pub mod operate;

src/main.rs

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,8 @@
1-
mod format;
2-
mod operate;
3-
use crate::format::{format_git_url, get_git_remote_url};
4-
use crate::operate::open_in_browser;
1+
use opengit::{
2+
format::{format_git_url, get_git_remote_url},
3+
operate::open_in_browser,
4+
};
5+
56
fn main() {
67
let remote_url = get_git_remote_url();
78
if remote_url.is_none() {

src/operate.rs

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,15 @@
11
use webbrowser;
22
// 使用默认浏览器打开当前链接
3-
pub fn open_in_browser(url: String) {
3+
pub fn open_in_browser(url: String) -> Option<String> {
44
let result = webbrowser::open(&url);
55
match result {
6-
Ok(_) => println!("已打开git远程仓库链接:{}", url),
7-
Err(e) => eprintln!("无法打开: {}", e),
6+
Ok(_) => {
7+
println!("already open git url:{}", url);
8+
Some(url)
9+
}
10+
Err(e) => {
11+
eprintln!("sorry: {}", e);
12+
None
13+
}
814
}
915
}

tests/test_format.rs

Lines changed: 73 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,73 @@
1+
use opengit::format::{AddressType, format_git_url, get_domain_type, get_git_remote_url};
2+
3+
#[test]
4+
fn domain_type_test() {
5+
let input = "github.com";
6+
let expected = AddressType::DomainName;
7+
assert_eq!(get_domain_type(input), expected);
8+
}
9+
#[test]
10+
fn ip_type_test() {
11+
let input = "192.168.1.1";
12+
let expected = AddressType::IpAddress;
13+
assert_eq!(get_domain_type(input), expected);
14+
}
15+
#[test]
16+
fn invalid_domain_type_test1() {
17+
let input = "localhost";
18+
let expected = AddressType::Invalid;
19+
assert_eq!(get_domain_type(input), expected);
20+
}
21+
#[test]
22+
fn invalid_domain_type_test2() {
23+
let input = "192-189-11-111";
24+
let expected = AddressType::Invalid;
25+
assert_eq!(get_domain_type(input), expected);
26+
}
27+
#[test]
28+
fn invalid_domain_type_test3() {
29+
let input = "255.255.255.259";
30+
let expected = AddressType::Invalid;
31+
assert_eq!(get_domain_type(input), expected);
32+
}
33+
34+
#[test]
35+
fn format_git_url_https_test() {
36+
let input = "https://github.com/log1997/opengit.git".to_string();
37+
let expected = "https://github.com/log1997/opengit";
38+
assert_eq!(format_git_url(input), expected);
39+
}
40+
41+
#[test]
42+
fn format_git_url_git_test() {
43+
let input = "git@github.com:log1997/opengit.git".to_string();
44+
let expected = "https://github.com/log1997/opengit";
45+
assert_eq!(format_git_url(input), expected);
46+
}
47+
48+
#[test]
49+
fn format_git_url_gitee_test() {
50+
let input = "https://gitee.com/log1997/opengit.git".to_string();
51+
let expected = "https://gitee.com/log1997/opengit";
52+
assert_eq!(format_git_url(input), expected);
53+
}
54+
55+
#[test]
56+
fn format_git_url_no_git_test() {
57+
let input = "https://gitee.com/log1997/opengit".to_string();
58+
let expected = "https://gitee.com/log1997/opengit";
59+
assert_eq!(format_git_url(input), expected);
60+
}
61+
62+
#[test]
63+
fn format_git_url_number_ip_test() {
64+
let input = "git@10.2.3.12:log1997/opengit.git".to_string();
65+
let expected = "http://10.2.3.12/log1997/opengit";
66+
assert_eq!(format_git_url(input), expected);
67+
}
68+
69+
#[test]
70+
fn get_git_remote_url_test() {
71+
let expected = "git@github.com:LOG1997/opengit.git";
72+
assert_eq!(get_git_remote_url(), Some(expected.to_string()));
73+
}

tests/test_opreate.rs

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,9 @@
1+
use opengit::operate::open_in_browser;
2+
3+
#[test]
4+
#[ignore]
5+
fn open_in_browser_test() {
6+
let input = "https://github.com/log1997/opengit";
7+
let expected = Some(input.to_string());
8+
assert_eq!(open_in_browser(input.to_string()), expected);
9+
}

0 commit comments

Comments
 (0)