From 2ac9170417df5d41906c3f02eb72e45dc9b872af Mon Sep 17 00:00:00 2001 From: rahul Date: Sun, 24 Dec 2023 14:25:03 +0530 Subject: [PATCH] feat: use enum for sort order --- Cargo.lock | 2 +- Cargo.toml | 2 +- examples/run.rs | 4 ++-- src/lib.rs | 29 +++++++++++++++++++++++++---- 4 files changed, 29 insertions(+), 8 deletions(-) diff --git a/Cargo.lock b/Cargo.lock index 9affa70..3a0d503 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -503,7 +503,7 @@ dependencies = [ [[package]] name = "ordiscan" -version = "0.1.0" +version = "0.1.1" dependencies = [ "reqwest", "serde", diff --git a/Cargo.toml b/Cargo.toml index 80d0637..024a41b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "ordiscan" -version = "0.1.0" +version = "0.1.1" edition = "2021" authors = ["Rahul "] description = "Ordiscan API Wrapper" diff --git a/examples/run.rs b/examples/run.rs index 84d4e21..67ef59f 100644 --- a/examples/run.rs +++ b/examples/run.rs @@ -1,4 +1,4 @@ -use ordiscan::{GetInscriptionInfoParams, GetListOfInscriptionParams, Ordiscan}; +use ordiscan::{GetInscriptionInfoParams, GetListOfInscriptionParams, Ordiscan, Sort}; #[tokio::main] async fn main() -> Result<(), Box> { @@ -22,8 +22,8 @@ async fn main() -> Result<(), Box> { let list_of_inscriptions = ordiclient .get_list_of_inscriptions(GetListOfInscriptionParams { address: Some("bc1qxy2kgdygjrsqtzq2n0yrf2493p83kkfjhx0wlh"), + sort: Sort::InscriptionNumberDesc, content_type: None, - sort: None, after_number: None, before_number: None, }) diff --git a/src/lib.rs b/src/lib.rs index 68d62fa..e23d405 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -23,7 +23,7 @@ pub struct Ordiscan { pub struct GetListOfInscriptionParams<'a> { pub address: Option<&'a str>, pub content_type: Option<&'a str>, - pub sort: Option<&'a str>, + pub sort: Sort, pub after_number: Option, pub before_number: Option, } @@ -34,6 +34,21 @@ pub struct GetInscriptionInfoParams<'a> { pub number: Option, } +#[derive(Debug)] +pub enum Sort { + InscriptionNumberDesc, + InscriptionNumberAsc, +} + +impl Sort { + pub fn as_str(&self) -> &'static str { + match self { + Sort::InscriptionNumberDesc => "inscription_number_desc", // default + Sort::InscriptionNumberAsc => "inscription_number_asc", + } + } +} + impl<'a> Ordiscan { // create a new Ordiscan client pub fn new(key: String) -> reqwest::Result { @@ -86,9 +101,15 @@ impl<'a> Ordiscan { params: GetListOfInscriptionParams<'a>, ) -> Result> { let header = format!("Bearer {}", self.api_key); - let sort = params.sort.unwrap_or("inscription_number_desc"); - let mut url = - Url::parse(format!("{}/inscriptions?sort={}", API_BASE_URL, sort).as_str()).unwrap(); + let mut url = Url::parse( + format!( + "{}/inscriptions?sort={}", + API_BASE_URL, + params.sort.as_str() + ) + .as_str(), + ) + .unwrap(); // TODO make this look better // dynamically create query params