Compare commits

...

4 Commits

Author SHA1 Message Date
3d885982cd Merge remote-tracking branch 'origin/main' 2024-08-14 20:08:28 -07:00
28b6982111 Added README 2024-08-14 20:00:51 -07:00
9954dd0b62 Miscellaneous cleanup 2024-03-09 14:12:08 -08:00
c3efab7b2a First commit 2024-02-25 16:32:47 -08:00
6 changed files with 743 additions and 3 deletions

1
.gitignore vendored
View File

@ -13,4 +13,3 @@ Cargo.lock
# MSVC Windows builds of rustc generate these, which store debugging information
*.pdb

441
Cargo.lock generated Normal file
View File

@ -0,0 +1,441 @@
# This file is automatically @generated by Cargo.
# It is not intended for manual editing.
version = 3
[[package]]
name = "anstream"
version = "0.6.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "96b09b5178381e0874812a9b157f7fe84982617e48f71f4e3235482775e5b540"
dependencies = [
"anstyle",
"anstyle-parse",
"anstyle-query",
"anstyle-wincon",
"colorchoice",
"utf8parse",
]
[[package]]
name = "anstyle"
version = "1.0.6"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8901269c6307e8d93993578286ac0edf7f195079ffff5ebdeea6a59ffb7e36bc"
[[package]]
name = "anstyle-parse"
version = "0.2.3"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c75ac65da39e5fe5ab759307499ddad880d724eed2f6ce5b5e8a26f4f387928c"
dependencies = [
"utf8parse",
]
[[package]]
name = "anstyle-query"
version = "1.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e28923312444cdd728e4738b3f9c9cac739500909bb3d3c94b43551b16517648"
dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "anstyle-wincon"
version = "3.0.2"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1cd54b81ec8d6180e24654d0b371ad22fc3dd083b6ff8ba325b72e00c87660a7"
dependencies = [
"anstyle",
"windows-sys 0.52.0",
]
[[package]]
name = "bar_weather"
version = "0.1.0"
dependencies = [
"clap",
"curl",
"serde",
"serde_json",
]
[[package]]
name = "cc"
version = "1.0.83"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "f1174fb0b6ec23863f8b971027804a42614e347eafb0a95bf0b12cdae21fc4d0"
dependencies = [
"libc",
]
[[package]]
name = "clap"
version = "4.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "c918d541ef2913577a0f9566e9ce27cb35b6df072075769e0b26cb5a554520da"
dependencies = [
"clap_builder",
"clap_derive",
]
[[package]]
name = "clap_builder"
version = "4.5.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9f3e7391dad68afb0c2ede1bf619f579a3dc9c2ec67f089baa397123a2f3d1eb"
dependencies = [
"anstream",
"anstyle",
"clap_lex",
"strsim",
]
[[package]]
name = "clap_derive"
version = "4.5.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "307bc0538d5f0f83b8248db3087aa92fe504e4691294d0c96c0eabc33f47ba47"
dependencies = [
"heck",
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "clap_lex"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "98cc8fbded0c607b7ba9dd60cd98df59af97e84d24e49c8557331cfc26d301ce"
[[package]]
name = "colorchoice"
version = "1.0.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "acbf1af155f9b9ef647e42cdc158db4b64a1b61f743629225fde6f3e0be2a7c7"
[[package]]
name = "curl"
version = "0.4.46"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1e2161dd6eba090ff1594084e95fd67aeccf04382ffea77999ea94ed42ec67b6"
dependencies = [
"curl-sys",
"libc",
"openssl-probe",
"openssl-sys",
"schannel",
"socket2",
"windows-sys 0.52.0",
]
[[package]]
name = "curl-sys"
version = "0.4.72+curl-8.6.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "29cbdc8314c447d11e8fd156dcdd031d9e02a7a976163e396b548c03153bc9ea"
dependencies = [
"cc",
"libc",
"libz-sys",
"openssl-sys",
"pkg-config",
"vcpkg",
"windows-sys 0.52.0",
]
[[package]]
name = "heck"
version = "0.4.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "95505c38b4572b2d910cecb0281560f54b440a19336cbbcb27bf6ce6adc6f5a8"
[[package]]
name = "itoa"
version = "1.0.10"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "b1a46d1a171d865aa5f83f92695765caa047a9b4cbae2cbf37dbd613a793fd4c"
[[package]]
name = "libc"
version = "0.2.153"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd"
[[package]]
name = "libz-sys"
version = "1.1.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "037731f5d3aaa87a5675e895b63ddff1a87624bc29f77004ea829809654e48f6"
dependencies = [
"cc",
"libc",
"pkg-config",
"vcpkg",
]
[[package]]
name = "openssl-probe"
version = "0.1.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ff011a302c396a5197692431fc1948019154afc178baf7d8e37367442a4601cf"
[[package]]
name = "openssl-sys"
version = "0.9.99"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "22e1bf214306098e4832460f797824c05d25aacdf896f64a985fb0fd992454ae"
dependencies = [
"cc",
"libc",
"pkg-config",
"vcpkg",
]
[[package]]
name = "pkg-config"
version = "0.3.30"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "d231b230927b5e4ad203db57bbcbee2802f6bce620b1e4a9024a07d94e2907ec"
[[package]]
name = "proc-macro2"
version = "1.0.78"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e2422ad645d89c99f8f3e6b88a9fdeca7fabeac836b1002371c4367c8f984aae"
dependencies = [
"unicode-ident",
]
[[package]]
name = "quote"
version = "1.0.35"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "291ec9ab5efd934aaf503a6466c5d5251535d108ee747472c3977cc5acc868ef"
dependencies = [
"proc-macro2",
]
[[package]]
name = "ryu"
version = "1.0.17"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e86697c916019a8588c99b5fac3cead74ec0b4b819707a682fd4d23fa0ce1ba1"
[[package]]
name = "schannel"
version = "0.1.23"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "fbc91545643bcf3a0bbb6569265615222618bdf33ce4ffbbd13c4bbd4c093534"
dependencies = [
"windows-sys 0.52.0",
]
[[package]]
name = "serde"
version = "1.0.196"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "870026e60fa08c69f064aa766c10f10b1d62db9ccd4d0abb206472bee0ce3b32"
dependencies = [
"serde_derive",
]
[[package]]
name = "serde_derive"
version = "1.0.196"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "33c85360c95e7d137454dc81d9a4ed2b8efd8fbe19cee57357b32b9771fccb67"
dependencies = [
"proc-macro2",
"quote",
"syn",
]
[[package]]
name = "serde_json"
version = "1.0.113"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "69801b70b1c3dac963ecb03a364ba0ceda9cf60c71cfe475e99864759c8b8a79"
dependencies = [
"itoa",
"ryu",
"serde",
]
[[package]]
name = "socket2"
version = "0.5.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "7b5fac59a5cb5dd637972e5fca70daf0523c9067fcdc4842f053dae04a18f8e9"
dependencies = [
"libc",
"windows-sys 0.48.0",
]
[[package]]
name = "strsim"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "5ee073c9e4cd00e28217186dbe12796d692868f432bf2e97ee73bed0c56dfa01"
[[package]]
name = "syn"
version = "2.0.49"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "915aea9e586f80826ee59f8453c1101f9d1c4b3964cd2460185ee8e299ada496"
dependencies = [
"proc-macro2",
"quote",
"unicode-ident",
]
[[package]]
name = "unicode-ident"
version = "1.0.12"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3354b9ac3fae1ff6755cb6db53683adb661634f67557942dea4facebec0fee4b"
[[package]]
name = "utf8parse"
version = "0.2.1"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "711b9620af191e0cdc7468a8d14e709c3dcdb115b36f838e601583af800a370a"
[[package]]
name = "vcpkg"
version = "0.2.15"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "accd4ea62f7bb7a82fe23066fb0957d48ef677f6eeb8215f372f52e48bb32426"
[[package]]
name = "windows-sys"
version = "0.48.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "677d2418bec65e3338edb076e806bc1ec15693c5d0104683f2efe857f61056a9"
dependencies = [
"windows-targets 0.48.5",
]
[[package]]
name = "windows-sys"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d"
dependencies = [
"windows-targets 0.52.0",
]
[[package]]
name = "windows-targets"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "9a2fa6e2155d7247be68c096456083145c183cbbbc2764150dda45a87197940c"
dependencies = [
"windows_aarch64_gnullvm 0.48.5",
"windows_aarch64_msvc 0.48.5",
"windows_i686_gnu 0.48.5",
"windows_i686_msvc 0.48.5",
"windows_x86_64_gnu 0.48.5",
"windows_x86_64_gnullvm 0.48.5",
"windows_x86_64_msvc 0.48.5",
]
[[package]]
name = "windows-targets"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8a18201040b24831fbb9e4eb208f8892e1f50a37feb53cc7ff887feb8f50e7cd"
dependencies = [
"windows_aarch64_gnullvm 0.52.0",
"windows_aarch64_msvc 0.52.0",
"windows_i686_gnu 0.52.0",
"windows_i686_msvc 0.52.0",
"windows_x86_64_gnu 0.52.0",
"windows_x86_64_gnullvm 0.52.0",
"windows_x86_64_msvc 0.52.0",
]
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "2b38e32f0abccf9987a4e3079dfb67dcd799fb61361e53e2882c3cbaf0d905d8"
[[package]]
name = "windows_aarch64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea"
[[package]]
name = "windows_aarch64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dc35310971f3b2dbbf3f0690a219f40e2d9afcf64f9ab7cc1be722937c26b4bc"
[[package]]
name = "windows_aarch64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef"
[[package]]
name = "windows_i686_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a75915e7def60c94dcef72200b9a8e58e5091744960da64ec734a6c6e9b3743e"
[[package]]
name = "windows_i686_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313"
[[package]]
name = "windows_i686_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "8f55c233f70c4b27f66c523580f78f1004e8b5a8b659e05a4eb49d4166cca406"
[[package]]
name = "windows_i686_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a"
[[package]]
name = "windows_x86_64_gnu"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "53d40abd2583d23e4718fddf1ebec84dbff8381c07cae67ff7768bbf19c6718e"
[[package]]
name = "windows_x86_64_gnu"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "0b7b52767868a23d5bab768e390dc5f5c55825b6d30b86c844ff2dc7414044cc"
[[package]]
name = "windows_x86_64_gnullvm"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e"
[[package]]
name = "windows_x86_64_msvc"
version = "0.48.5"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "ed94fce61571a4006852b7389a063ab983c02eb1bb37b47f8272ce92d06d9538"
[[package]]
name = "windows_x86_64_msvc"
version = "0.52.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04"

12
Cargo.toml Normal file
View File

@ -0,0 +1,12 @@
[package]
name = "bar_weather"
version = "0.1.0"
edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
[dependencies]
serde = "1.0.196"
serde_json = "1.0.113"
curl = "0.4.46"
clap = { version = "4.5.1", features = ["derive"] }

View File

@ -1,3 +1,21 @@
# bar_weather
# Bar weather
Simple tool written in Rust to query weather
Simple tool written in Rust to query weather
# Description
* Tool uses weather.gov API to query weather
# Usage
* Use the `--help` option for usage information:
```sh
Usage: bar_weather [OPTIONS]
Options:
-s, --station <STATION> Name of the weather station [default: khio]
-m, --metric Use Metric units. Imperial units otherwise
-h, --help Print help
-V, --version Print version
```

136
src/main.rs Normal file
View File

@ -0,0 +1,136 @@
use std::str;
use std::fmt;
use curl::easy::{Easy, List};
use serde_json::Value;
use clap::Parser;
//
// Enums
//
#[derive(Debug,PartialEq,Eq,Copy,Clone)]
enum TemperatureUnits {
Metric,
Imperial,
}
impl fmt::Display for TemperatureUnits {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
TemperatureUnits::Metric => write!(f, "Metric"),
TemperatureUnits::Imperial => write!(f, "Imperial"),
}
}
}
// Commandline parsing
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct CommandlineArgs {
/// Name of the weather station
#[arg(short, long, default_value = "khio")]
station: String,
/// Use Metric units. Imperial units otherwise
#[arg(short, long)]
metric: bool,
}
//
// Application options
//
#[derive(Debug,Clone)]
struct AppOptions {
units: TemperatureUnits,
station: String,
}
//
// Application (BarWeather)
//
struct BarWeather {
// acts :BarWeatherActions
opts :AppOptions,
}
impl BarWeather {
fn run(self) {
self.check_options();
self.get_weather();
}
fn get_weather(self) {
// Print a web page onto stdout
let mut curl = Easy::new();
let url_string_1 :String = "https://api.weather.gov/stations/".to_owned();
let url_string_2 :&str = self.opts.station.as_str();
let url_string_3 :String = "/observations?limit=1".to_owned();
let url_string = url_string_1 + url_string_2 + &url_string_3;
curl.url(url_string.as_str()).unwrap();
let mut list = List::new();
list.append("User-Agent: Bar Weather (mahesh@heshapps.com)").unwrap();
curl.http_headers(list).unwrap();
curl.write_function(move |data| {
// To debug returned JSON
// use std::io::{stdout, Write};
// stdout().write_all(data).unwrap();
let v: Value = serde_json::from_str(str::from_utf8(data).unwrap()).unwrap();
let temperature_value :f32 = self.get_current_temperature(v);
let temperature_unit :String = self.get_temperature_unit();
println!("{:.2}{}", temperature_value, temperature_unit);
Ok(data.len())
}
).unwrap();
curl.perform().unwrap();
}
fn check_options(&self) -> bool {
let mut all_good = true;
if ! ((self.opts.units == TemperatureUnits::Metric)
|| (self.opts.units == TemperatureUnits::Imperial)) {
all_good &= false;
}
return all_good;
}
fn get_current_temperature(&self, v: Value) -> f32 {
let deg_c :f32 = v["features"][0]["properties"]["temperature"]["value"]
.to_string().parse().unwrap();
return if self.opts.units == TemperatureUnits::Metric { deg_c }
else { (deg_c * 9.0 / 5.0) + 32.0};
}
fn get_temperature_unit(&self) -> String{
return if self.opts.units == TemperatureUnits::Metric { "°C".to_string() }
else { "°F".to_string() };
}
}
//
// Entry point
//
fn main() {
let cmd_args = CommandlineArgs::parse();
let app = BarWeather {
opts: AppOptions {
units: if cmd_args.metric { TemperatureUnits::Metric }
else { TemperatureUnits::Imperial },
station: cmd_args.station,
},
};
app.run();
}
// References:
// https://reintech.io/blog/working-with-json-in-rust

134
src/main.rs.backup Normal file
View File

@ -0,0 +1,134 @@
use std::str;
use std::fmt;
use curl::easy::{Easy, List};
use serde_json::Value;
//
// Enums
//
#[derive(Debug,PartialEq,Eq,Copy,Clone)]
enum TemperatureUnits {
Metric,
Imperial,
}
impl fmt::Display for TemperatureUnits {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
match self {
TemperatureUnits::Metric => write!(f, "Metric"),
TemperatureUnits::Imperial => write!(f, "Imperial"),
}
}
}
// Commandline parsing
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct CommandlineArgs {
/// Name of the weather station
#[arg(short, long, default_value = "khio")]
station: String,
}
//
// Application options
//
#[derive(Debug,Copy,Clone)]
struct AppOptions {
units: TemperatureUnits,
station: String,
}
//
// Application (BarWeather) actions
struct BarWeatherActions {
opts :AppOptions,
}
impl BarWeatherActions {
fn check_options(&self) -> bool {
let mut all_good = true;
if ! ((self.opts.units == TemperatureUnits::Metric)
|| (self.opts.units == TemperatureUnits::Imperial)) {
all_good &= false;
}
return all_good;
}
fn get_current_temperature(&self, v: Value) -> f32 {
let deg_c :f32 = v["features"][0]["properties"]["temperature"]["value"]
.to_string().parse().unwrap();
return if self.opts.units == TemperatureUnits::Metric { deg_c }
else { (deg_c * 9.0 / 5.0) + 32.0};
}
fn get_temperature_unit(&self) -> String{
return if self.opts.units == TemperatureUnits::Metric { "°C".to_string() }
else { "°F".to_string() };
}
}
//
// Application (BarWeather)
//
struct BarWeather {
acts :BarWeatherActions
}
impl BarWeather {
fn run(self) {
self.acts.check_options();
self.get_weather();
}
fn get_weather(self) {
// Print a web page onto stdout
let mut easy = Easy::new();
let app = self;
easy.url("https://api.weather.gov/stations/" + + "/observations?limit=1").unwrap();
let mut list = List::new();
list.append("User-Agent: Bar Weather (mahesh@heshapps.com)").unwrap();
easy.http_headers(list).unwrap();
easy.write_function(move |data| {
// To debug returned JSON
// use std::io::{stdout, Write};
// stdout().write_all(data).unwrap();
let v: Value = serde_json::from_str(str::from_utf8(data).unwrap()).unwrap();
let temperature_value :f32 = app.acts.get_current_temperature(v);
let temperature_unit :String = app.acts.get_temperature_unit();
println!("{}{}", temperature_value, temperature_unit);
println!("{:.2}{}", temperature_value, temperature_unit);
Ok(data.len())
}
).unwrap();
easy.perform().unwrap();
}
}
//
// Entry point
//
fn main() {
let app = BarWeather {
acts: BarWeatherActions {
opts: AppOptions {
units: TemperatureUnits::Imperial,
// units: TemperatureUnits::Metric,
},
},
};
app.run();
}
// References:
// https://reintech.io/blog/working-with-json-in-rust