From b67110564e72c4eeb78c7480558cdff49ba03cd7 Mon Sep 17 00:00:00 2001 From: notohh Date: Mon, 27 May 2024 09:47:46 -0400 Subject: [PATCH] init commit --- .envrc | 1 + .gitignore | 7 ++ .rustfmt.toml | 2 + Cargo.toml | 11 +++ flake.lock | 188 ++++++++++++++++++++++++++++++++++++++++++++++++++ flake.nix | 79 +++++++++++++++++++++ src/main.rs | 52 ++++++++++++++ statix.toml | 3 + 8 files changed, 343 insertions(+) create mode 100644 .envrc create mode 100644 .rustfmt.toml create mode 100644 Cargo.toml create mode 100644 flake.lock create mode 100644 flake.nix create mode 100644 src/main.rs create mode 100644 statix.toml diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..3550a30 --- /dev/null +++ b/.envrc @@ -0,0 +1 @@ +use flake diff --git a/.gitignore b/.gitignore index 3ca43ae..7a031a1 100644 --- a/.gitignore +++ b/.gitignore @@ -14,3 +14,10 @@ Cargo.lock # MSVC Windows builds of rustc generate these, which store debugging information *.pdb + + +# Added by cargo + +/target + +.pre-commit-config.yaml diff --git a/.rustfmt.toml b/.rustfmt.toml new file mode 100644 index 0000000..d84ffea --- /dev/null +++ b/.rustfmt.toml @@ -0,0 +1,2 @@ +imports_granularity = "Module" +fn_args_layout = "Vertical" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..8140bf8 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,11 @@ +[package] +name = "forex" +version = "0.1.0" +edition = "2021" + +[dependencies] +clap = { version = "4.5.4", features = ["derive"] } +reqwest = { version = "0.12.4", features = ["json"] } +serde = { version = "1.0.203", features = ["derive"] } +serde_json = "1.0.117" +tokio = { version = "1.37.0", features = ["full"] } diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..0bab794 --- /dev/null +++ b/flake.lock @@ -0,0 +1,188 @@ +{ + "nodes": { + "fenix": { + "inputs": { + "nixpkgs": "nixpkgs", + "rust-analyzer-src": "rust-analyzer-src" + }, + "locked": { + "lastModified": 1714544767, + "narHash": "sha256-kF1bX+YFMedf1g0PAJYwGUkzh22JmULtj8Rm4IXAQKs=", + "owner": "nix-community", + "repo": "fenix", + "rev": "73124e1356bde9411b163d636b39fe4804b7ca45", + "type": "github" + }, + "original": { + "owner": "nix-community", + "ref": "monthly", + "repo": "fenix", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1696426674, + "narHash": "sha256-kvjfFW7WAETZlt09AgDn1MrtKzP7t90Vf7vypd3OL1U=", + "owner": "edolstra", + "repo": "flake-compat", + "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "gitignore": { + "inputs": { + "nixpkgs": [ + "pre-commit-hooks", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1709087332, + "narHash": "sha256-HG2cCnktfHsKV0s4XW83gU3F57gaTljL9KNSuG6bnQs=", + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "637db329424fd7e46cf4185293b9cc8c88c95394", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1714253743, + "narHash": "sha256-mdTQw2XlariysyScCv2tTE45QSU9v/ezLcHJ22f0Nxc=", + "owner": "nixos", + "repo": "nixpkgs", + "rev": "58a1abdbae3217ca6b702f03d3b35125d88a2994", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixos-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-stable": { + "locked": { + "lastModified": 1710695816, + "narHash": "sha256-3Eh7fhEID17pv9ZxrPwCLfqXnYP006RKzSs0JptsN84=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "614b4613980a522ba49f0d194531beddbb7220d3", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-23.11", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_2": { + "locked": { + "lastModified": 1716715802, + "narHash": "sha256-usk0vE7VlxPX8jOavrtpOqphdfqEQpf9lgedlY/r66c=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "e2dd4e18cc1c7314e24154331bae07df76eb582f", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs_3": { + "locked": { + "lastModified": 1710765496, + "narHash": "sha256-p7ryWEeQfMwTB6E0wIUd5V2cFTgq+DRRBz2hYGnJZyA=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "e367f7a1fb93137af22a3908f00b9a35e2d286a7", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "pre-commit-hooks": { + "inputs": { + "flake-compat": "flake-compat", + "gitignore": "gitignore", + "nixpkgs": "nixpkgs_3", + "nixpkgs-stable": "nixpkgs-stable" + }, + "locked": { + "lastModified": 1716213921, + "narHash": "sha256-xrsYFST8ij4QWaV6HEokCUNIZLjjLP1bYC60K8XiBVA=", + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "rev": "0e8fcc54b842ad8428c9e705cb5994eaf05c26a0", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "type": "github" + } + }, + "root": { + "inputs": { + "fenix": "fenix", + "nixpkgs": "nixpkgs_2", + "pre-commit-hooks": "pre-commit-hooks", + "systems": "systems" + } + }, + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1714501997, + "narHash": "sha256-g31zfxwUFzkPgX0Q8sZLcrqGmOxwjEZ/iqJjNx4fEGo=", + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "49e502b277a8126a9ad10c802d1aaa3ef1a280ef", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" + } + }, + "systems": { + "locked": { + "lastModified": 1689347949, + "narHash": "sha256-12tWmuL2zgBgZkdoB6qXZsgJEH9LR3oUgpaQq2RbI80=", + "owner": "nix-systems", + "repo": "default-linux", + "rev": "31732fcf5e8fea42e59c2488ad31a0e651500f68", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default-linux", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/flake.nix b/flake.nix new file mode 100644 index 0000000..3c2532b --- /dev/null +++ b/flake.nix @@ -0,0 +1,79 @@ +{ + inputs = { + systems.url = "github:nix-systems/default-linux"; + nixpkgs.url = "github:NixOS/nixpkgs/nixpkgs-unstable"; + fenix.url = "github:nix-community/fenix/monthly"; + pre-commit-hooks.url = "github:cachix/pre-commit-hooks.nix"; + }; + + outputs = { + self, + systems, + nixpkgs, + fenix, + pre-commit-hooks, + }: let + eachSystem = nixpkgs.lib.genAttrs (import systems); + pkgsFor = eachSystem (system: + import nixpkgs { + localSystem.system = system; + overlays = [fenix.overlays.default]; + }); + in { + packages = eachSystem (system: let + pkgs = nixpkgs.legacyPackages.${system}; + inherit ((builtins.fromTOML (builtins.readFile ./Cargo.toml)).package) version; + in { + default = pkgsFor.${system}.rustPlatform.buildRustPackage { + pname = "forex"; + version = "${version}"; + + src = self; + + cargoLock = { + lockFile = ./.; + }; + + nativeBuildInputs = with pkgs; [pkg-config]; + buildInputs = with pkgs; [openssl]; + + doCheck = false; + }; + }); + checks = eachSystem (system: { + pre-commit-check = pre-commit-hooks.lib.${system}.run { + src = ./.; + hooks = { + # rust + rustfmt.enable = true; + taplo.enable = true; + # nix + statix.enable = true; + alejandra.enable = true; + deadnix.enable = true; + }; + }; + }); + devShells = eachSystem (system: { + default = pkgsFor.${system}.mkShell { + inherit (self.checks.${system}.pre-commit-check) shellHook; + packages = with pkgsFor.${system}; [ + nil + alejandra + openssl + pkg-config + (fenix.packages.${system}.complete.withComponents [ + "cargo" + "clippy" + "rust-src" + "rustc" + "rustfmt" + "rust-analyzer" + ]) + ]; + RUST_BACKTRACE = 1; + RUST_SRC_PATH = "${fenix.packages.${system}.complete.rust-src}/lib/rustlib/src/rust/library"; + }; + }); + }; +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..1d369a3 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,52 @@ +use clap::Parser; +use reqwest::Error; +use serde::{Deserialize, Serialize}; + +#[derive(Parser, Debug)] +#[command(version, about, long_about = None)] + +pub struct Args { + #[arg(short, long)] + app_id: String, + #[arg(short, long)] + from: String, + #[arg(short, long)] + to: String, +} + +#[derive(Debug, Deserialize, Serialize)] +pub struct Payload { + base_code: String, + target_code: String, + conversion_rate: f64, +} + +#[tokio::main] +async fn main() -> Result<(), Error> { + let args = Args::parse(); + let url = format!( + "https://v6.exchangerate-api.com/v6/{}/pair/{}/{}", + args.app_id, args.from, args.to + ); + + let client = reqwest::Client::new(); + + match client.get(url).send().await { + Ok(response) => { + if response.status().is_success() { + match response.json::().await { + Ok(payload) => { + println!("From: {:?}", payload.base_code); + println!("To: {:?}", payload.target_code); + println!("Rate: {:?}", payload.conversion_rate); + } + Err(e) => panic!("Failed: {}", e), + } + } else { + panic!("Unknown errors have occured!") + } + } + Err(e) => panic!("Request Error: {}", e), + } + Ok(()) +} diff --git a/statix.toml b/statix.toml new file mode 100644 index 0000000..fbe25a9 --- /dev/null +++ b/statix.toml @@ -0,0 +1,3 @@ +disabled = [] +nix_version = '2.4' +ignore = ['.direnv']