File tree 2 files changed +34
-5
lines changed
2 files changed +34
-5
lines changed Original file line number Diff line number Diff line change @@ -12,6 +12,12 @@ pub fn get_ip4_string() -> Result<String, Box<dyn std::error::Error>> {
12
12
Ok ( body)
13
13
}
14
14
15
+ pub fn get_ip4_i64 ( ) -> Result < i64 , Box < dyn std:: error:: Error > > {
16
+ let body = reqwest:: blocking:: get ( "https://api.ipify.org" ) ?. text ( ) ?;
17
+ let result = ip_to_int ( body) ;
18
+ Ok ( result)
19
+ }
20
+
15
21
pub fn get_ip_json ( ) -> Result < HashMap < String , String > , Box < dyn std:: error:: Error > > {
16
22
let body = reqwest:: blocking:: get ( "https://api.ipify.org?format=json" ) ?
17
23
. json :: < HashMap < String , String > > ( ) ?;
@@ -22,3 +28,17 @@ pub fn get_ip_string() -> Result<String, Box<dyn std::error::Error>> {
22
28
let body = reqwest:: blocking:: get ( "https://api.ipify.org" ) ?. text ( ) ?;
23
29
Ok ( body)
24
30
}
31
+
32
+ fn ip_to_int ( ip : String ) -> i64 {
33
+ let mut result : i64 = 0 ;
34
+ let splitted_ip : Vec < & str > = ip. split ( '.' ) . collect ( ) ;
35
+ if splitted_ip. len ( ) == 4 {
36
+ let first_block : i64 = splitted_ip[ 0 ] . parse :: < i64 > ( ) . unwrap ( ) ;
37
+ let second_block : i64 = splitted_ip[ 1 ] . parse :: < i64 > ( ) . unwrap ( ) ;
38
+ let third_block : i64 = splitted_ip[ 2 ] . parse :: < i64 > ( ) . unwrap ( ) ;
39
+ let fourth_block : i64 = splitted_ip[ 3 ] . parse :: < i64 > ( ) . unwrap ( ) ;
40
+
41
+ result = ( first_block<<24 ) +( second_block<<16 ) +( third_block<<8 ) +( fourth_block<<0 ) ;
42
+ }
43
+ result
44
+ }
Original file line number Diff line number Diff line change 1
- extern crate ipify_lib;
2
- use ipify_lib:: ipify:: get_ip_json;
3
- use ipify_lib:: ipify:: get_ip_string;
4
- use ipify_lib:: ipify:: get_ip4_json;
5
- use ipify_lib:: ipify:: get_ip4_string;
1
+ extern crate rust_ipify;
2
+ use rust_ipify:: ipify:: get_ip_json;
3
+ use rust_ipify:: ipify:: get_ip_string;
4
+ use rust_ipify:: ipify:: get_ip4_json;
5
+ use rust_ipify:: ipify:: get_ip4_string;
6
+ use rust_ipify:: ipify:: get_ip4_i64;
6
7
fn main ( ) {
7
8
println ! ( "inside main of test " ) ;
8
9
@@ -31,4 +32,12 @@ fn main() {
31
32
Ok ( r) => println ! ( "Result IP v4 ( String ): \n \t {:#?}\n " , r) ,
32
33
Err ( e) => println ! ( "error in request {:?}" , e) ,
33
34
}
35
+
36
+ let result_ip4_i64 = get_ip4_i64 ( ) ;
37
+ match result_ip4_i64 {
38
+ Ok ( r) => println ! ( "Result IP v4 ( i64 ): \n \t {:#?}\n " , r) ,
39
+ Err ( e) => println ! ( "error in request {:?}" , e) ,
40
+ }
41
+
42
+
34
43
}
You can’t perform that action at this time.
0 commit comments