Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Adjust library layout and improve docs #9

Merged
merged 3 commits into from
Dec 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 10 additions & 10 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ resolver = "2"

[workspace.package]
authors = ["Rolv Apneseth"]
version = "0.0.3"
version = "0.0.4"
edition = "2021"
readme = "./README.md"
license = "MIT"
Expand Down
2 changes: 1 addition & 1 deletion cli/src/cli/convert.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use comfy_table::{
};
use lib_frankfurter::{
api::{self, ServerClient},
data::{Currency, CurrencyValue},
Currency, CurrencyValue,
};
use termcolor::StandardStream;

Expand Down
2 changes: 1 addition & 1 deletion cli/src/cli/period.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use comfy_table::{
};
use lib_frankfurter::{
api::{self, ServerClient},
data::{Currency, CurrencyValue},
Currency, CurrencyValue,
};
use termcolor::StandardStream;

Expand Down
2 changes: 1 addition & 1 deletion lib/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ strum = { workspace = true }
url = { workspace = true }
reqwest = { version = "0.12.8", features = ["json"] }
serde = { version = "^1.0", features = ["derive"] }
thiserror = { version = "1.0.64" }
thiserror = { version = "2.0.6" }

[dev-dependencies]
tokio = { workspace = true }
Expand Down
15 changes: 6 additions & 9 deletions lib/examples/basic.rs
Original file line number Diff line number Diff line change
@@ -1,19 +1,16 @@
extern crate reqwest;
extern crate serde_json;
extern crate tokio;
extern crate url;

use chrono::NaiveDate;
use lib_frankfurter::api::{convert, currencies, period, ServerClient};
use url::Url;
use lib_frankfurter::{
api::{convert, currencies, period, ServerClient},
chrono::NaiveDate,
url::Url,
};

#[tokio::main]
async fn main() {
let server_client: ServerClient = Url::parse("http://localhost:8080")
.map(ServerClient::new)
.unwrap_or_default();

// Fetch currencies - this request does not accept any additional options
// Fetch supported currencies - this request does not accept any additional options
match server_client
.currencies(currencies::Request::default())
.await
Expand Down
2 changes: 2 additions & 0 deletions lib/src/api/convert.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
//! [`Request`] and [`Response`] types for requesting exchange rates for a specific date (latest by default).

use std::borrow::Cow;

use chrono::NaiveDate;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/api/currencies.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//! [`Request`] and [`Response`] types for requesting the latest supported currency codes and their
//! names.

use std::{borrow::Cow, collections::BTreeMap};

use serde::{Deserialize, Serialize};
Expand Down
13 changes: 6 additions & 7 deletions lib/src/api/mod.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,4 @@
//! Bindings to the Frankfurter API
//!
//! The current bindings were generated using the
//! [HTTP API documentation](https://frankfurter.dev/).
//! Interface to the Frankfurter API.

pub mod convert;
pub mod currencies;
Expand Down Expand Up @@ -112,21 +109,23 @@ impl ServerClient {
self.get::<convert::Response>(req).await
}

/// Request a time series of historical exchange rates.
/// Request historical exchange rates for a given time period.
pub async fn period(&self, req: period::Request) -> Result<period::Response> {
self.get::<period::Response>(req).await
}

/// Request the supported currency codes and their full names.
/// Request the latest supported currency codes and their full names.
pub async fn currencies(&self, req: currencies::Request) -> Result<currencies::Response> {
self.get::<currencies::Response>(req).await
}
}

/// An endpoint's URL.
pub type EndpointUrl = Cow<'static, str>;
/// Query parameters to be passed to an endpoint.
pub type QueryParams = Vec<(&'static str, String)>;

/// Utility trait to provide a common interface for server client requests.
/// Utility trait to provide a common interface for requests.
pub trait ServerClientRequest {
fn get_url(&self) -> EndpointUrl;
fn ensure_valid(&self) -> Result<()>;
Expand Down
3 changes: 3 additions & 0 deletions lib/src/api/period.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
//! [`Request`] and [`Response`] types for requesting historical exchange rates for a
//! given time period.

use std::{borrow::Cow, collections::BTreeMap};

use chrono::{NaiveDate, Utc};
Expand Down
2 changes: 1 addition & 1 deletion lib/src/data.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,7 +75,7 @@ impl Default for Currency {
}
}

/// Value of a currency.
/// Value of a currency. Simple wrapper around an [`f64`].
///
/// This is a wrapper around [`f64`] to ensure that values are rounded to 2 decimal places.
#[derive(Clone, Copy, PartialEq, PartialOrd, Debug, Deserialize, Serialize)]
Expand Down
4 changes: 3 additions & 1 deletion lib/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,8 +3,10 @@ use reqwest::StatusCode;

use crate::data::Currency;

pub type Result<T> = std::result::Result<T, Error>;
/// [`std::result::Result`] wrapper for convenience.
pub(super) type Result<T> = std::result::Result<T, Error>;

/// Possible errors that can be encountered when making requests using the [`crate::api::ServerClient`].
#[derive(Debug, thiserror::Error)]
pub enum Error {
#[error("The target currencies '{}' include the base currency '{base}'", .targets.iter().map(|t| t.to_string()).collect::<Vec<String>>().join(","))]
Expand Down
21 changes: 19 additions & 2 deletions lib/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,20 @@
// #![doc = include_str!("../../README.md")]
//! Rust bindings to the [Frankfurter API](https://github.com/lineofflight/frankfurter)
//!
//! ## Usage
//!
//! ```rust
#![doc = include_str!("../examples/basic.rs")]
//! ```

pub mod api;
pub mod data;
pub mod error;
mod data;
mod error;

// RE-EXPORTS
pub use chrono;
pub use data::*;
pub use error::*;
pub use reqwest;
pub use serde_json;
pub use url;
5 changes: 1 addition & 4 deletions lib/tests/endpoint_convert.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
mod shared;
use chrono::NaiveDate;
use lib_frankfurter::{
api::convert,
data::{Currency, CurrencyValue},
};
use lib_frankfurter::{api::convert, Currency, CurrencyValue};
use pretty_assertions::assert_eq;
use shared::{get_invalid_server, get_server};

Expand Down
5 changes: 1 addition & 4 deletions lib/tests/endpoint_period.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,6 @@
mod shared;
use chrono::{Datelike, NaiveDate};
use lib_frankfurter::{
api::period,
data::{Currency, CurrencyValue},
};
use lib_frankfurter::{api::period, Currency, CurrencyValue};
use pretty_assertions::assert_eq;
use shared::{get_invalid_server, get_server};

Expand Down
Loading