Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 7484db5

Browse files
committed
feat: Url::port_or_default() to fill in default numbers for ports if possible. (#450)
1 parent 2d81b9f commit 7484db5

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

git-url/src/lib.rs

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -92,6 +92,20 @@ impl Url {
9292
pub fn path_is_root(&self) -> bool {
9393
self.path == "/"
9494
}
95+
/// Returns the actual or default port for use according to the url scheme.
96+
/// Note that there may be no default port either.
97+
pub fn port_or_default(&self) -> Option<u16> {
98+
self.port.or_else(|| {
99+
use Scheme::*;
100+
Some(match self.scheme {
101+
Http => 80,
102+
Https => 443,
103+
Ssh => 21,
104+
Git => 9418,
105+
File | Ext(_) => return None,
106+
})
107+
})
108+
}
95109
}
96110

97111
/// Serialization

0 commit comments

Comments
 (0)