Skip to content

Commit

Permalink
feat(samples): introduce rust sample
Browse files Browse the repository at this point in the history
  • Loading branch information
steveoh committed Dec 31, 2024
1 parent 14e4d3b commit be29178
Show file tree
Hide file tree
Showing 4 changed files with 183 additions and 0 deletions.
10 changes: 10 additions & 0 deletions samples/rust/.gitignore
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
/target/
**/*.rs.bk
Cargo.lock
*.pdb
.env
.cargo/config.toml
.idea/
.vscode/
*.swp
*.swo
10 changes: 10 additions & 0 deletions samples/rust/Cargo.toml
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
[package]
name = "rust"
version = "0.1.0"
edition = "2021"

[dependencies]
reqwest = { version = "0.12.12", features = ["json"] }
serde = { version = "1.0.217", features = ["derive"] }
serde_json = "1.0.134"
tokio = { version = "1.42.0", features = ["full"] }
117 changes: 117 additions & 0 deletions samples/rust/src/main.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,117 @@
extern crate reqwest;
extern crate serde_json;

mod models;

use models::{GeocodeResult, ResultContainer};
use std::env;

#[derive(serde::Serialize)]
#[allow(dead_code)]
enum Locators {
All,
AddressPoints,
RoadCenterlines,
}
impl std::fmt::Display for Locators {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Locators::All => write!(f, "all"),
Locators::AddressPoints => write!(f, "addressPoints"),
Locators::RoadCenterlines => write!(f, "roadCenterlines"),
}
}
}

#[derive(serde::Serialize)]
#[allow(dead_code)]
enum Format {
Esrijson,
Geojson,
}
impl std::fmt::Display for Format {
fn fmt(&self, f: &mut std::fmt::Formatter) -> std::fmt::Result {
match self {
Format::Esrijson => write!(f, "esrijson"),
Format::Geojson => write!(f, "geojson"),
}
}
}

#[derive(serde::Serialize)]
struct GeocodingOptions {
accept_score: Option<i32>,
pobox: Option<bool>,
locators: Option<Locators>,
suggest: Option<i8>,
spatial_reference: Option<i32>,
format: Option<Format>,
callback: Option<String>,
}
impl Default for GeocodingOptions {
fn default() -> Self {
Self {
accept_score: None,
pobox: None,
locators: None,
suggest: None,
spatial_reference: None,
format: None,
callback: None,
}
}
}

struct GeocodeService {
api_key: String,
}

impl GeocodeService {
fn new(api_key: String) -> Self {
Self { api_key }
}

async fn locate(
&self,
street: &str,
zone: &str,
options: GeocodingOptions,
) -> Result<ResultContainer<Option<GeocodeResult>>, reqwest::Error> {
let url = format!(
"https://api-1057643351324.us-central1.run.app/api/v1/geocode/{}/{}",
street, zone
);

let response: ResultContainer<Option<GeocodeResult>> = reqwest::Client::new()
.get(url)
.query(&[("apiKey", self.api_key.clone())])
.query(&options)
.send()
.await?
.json()
.await?;

// Return the response
Ok(response)
}
}

#[tokio::main]
async fn main() {
let api_key = env::var("UGRC_API_KEY").expect("UGRC_API_KEY must be set");
let geocode = GeocodeService::new(api_key);

let result = geocode
.locate(
"123 south main street",
"slc",
GeocodingOptions {
accept_score: Some(90),
spatial_reference: Some(4326),
..Default::default()
},
)
.await;

println!("{:#?}", result);
}
46 changes: 46 additions & 0 deletions samples/rust/src/models.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,46 @@
extern crate serde;

use serde::Deserialize;

#[derive(Debug, Deserialize)]
#[allow(dead_code)]
pub struct ResultContainer<T> {
pub status: i32,
pub message: Option<String>,
pub result: T,
}

#[derive(Debug, Deserialize)]
#[allow(dead_code)]
pub struct GeocodeResult {
pub location: Location,
pub score: f64,
pub locator: String,
#[serde(rename = "matchAddress")]
pub match_address: Option<String>,
#[serde(rename = "inputAddress")]
pub input_address: String,
#[serde(rename = "standardizedAddress")]
pub standardized_address: Option<String>,
#[serde(rename = "addressGrid")]
pub address_grid: String,
pub candidates: Option<Vec<Candidate>>,
}

#[derive(Debug, Deserialize)]
#[allow(dead_code)]
pub struct Candidate {
address: String,
pub location: Location,
pub score: f64,
pub locator: String,
#[serde(rename = "addressGrid")]
pub address_grid: String,
}

#[derive(Debug, Deserialize)]
#[allow(dead_code)]
pub struct Location {
pub x: f64,
pub y: f64,
}

0 comments on commit be29178

Please sign in to comment.