diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..cc5c18b --- /dev/null +++ b/.envrc @@ -0,0 +1,12 @@ +#!/usr/bin/env bash + +export DIRENV_WARN_TIMEOUT=20s + +eval "$(devenv direnvrc)" + +# `use devenv` supports the same options as the `devenv shell` command. +# +# To silence all output, use `--quiet`. +# +# Example usage: use devenv --quiet --impure --option services.postgres.enable:bool true +use devenv diff --git a/.gitignore b/.gitignore index a68c262..c33d1a0 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,14 @@ Cargo.lock *.bk .vscode *.sublime-workspace + +# Devenv +.devenv* +devenv.local.nix +devenv.local.yaml + +# direnv +.direnv + +# pre-commit +.pre-commit-config.yaml diff --git a/devenv.lock b/devenv.lock new file mode 100644 index 0000000..4d2fa1e --- /dev/null +++ b/devenv.lock @@ -0,0 +1,123 @@ +{ + "nodes": { + "devenv": { + "locked": { + "dir": "src/modules", + "lastModified": 1763939584, + "owner": "cachix", + "repo": "devenv", + "rev": "1ebc7baa0726314d24bb5cc34d5fc7927aa57929", + "type": "github" + }, + "original": { + "dir": "src/modules", + "owner": "cachix", + "repo": "devenv", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1761588595, + "owner": "edolstra", + "repo": "flake-compat", + "rev": "f387cd2afec9419c8ee37694406ca490c3f34ee5", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "git-hooks": { + "inputs": { + "flake-compat": "flake-compat", + "gitignore": "gitignore", + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1763741496, + "owner": "cachix", + "repo": "git-hooks.nix", + "rev": "20e71a403c5de9ce5bd799031440da9728c1cda1", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "git-hooks.nix", + "type": "github" + } + }, + "gitignore": { + "inputs": { + "nixpkgs": [ + "git-hooks", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1762808025, + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "cb5e3fdca1de58ccbc3ef53de65bd372b48f567c", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1761313199, + "owner": "cachix", + "repo": "devenv-nixpkgs", + "rev": "d1c30452ebecfc55185ae6d1c983c09da0c274ff", + "type": "github" + }, + "original": { + "owner": "cachix", + "ref": "rolling", + "repo": "devenv-nixpkgs", + "type": "github" + } + }, + "root": { + "inputs": { + "devenv": "devenv", + "git-hooks": "git-hooks", + "nixpkgs": "nixpkgs", + "pre-commit-hooks": [ + "git-hooks" + ], + "rust-overlay": "rust-overlay" + } + }, + "rust-overlay": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1763952169, + "owner": "oxalica", + "repo": "rust-overlay", + "rev": "ab726555a9a72e6dc80649809147823a813fa95b", + "type": "github" + }, + "original": { + "owner": "oxalica", + "repo": "rust-overlay", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/devenv.nix b/devenv.nix new file mode 100644 index 0000000..ba0b803 --- /dev/null +++ b/devenv.nix @@ -0,0 +1,59 @@ +{ + pkgs, + lib, + config, + inputs, + ... +}: { + # https://devenv.sh/basics/ + env.GREET = "devenv"; + + # https://devenv.sh/packages/ + packages = with pkgs; [ + git + bacon + cargo-nextest + gitui + helix + ]; + + # https://devenv.sh/languages/ + languages.rust = { + enable = true; + channel = "stable"; + }; + + # https://devenv.sh/processes/ + # processes.dev.exec = "${lib.getExe pkgs.watchexec} -n -- ls -la"; + + # https://devenv.sh/services/ + # services.postgres.enable = true; + + # https://devenv.sh/scripts/ + scripts.hello.exec = '' + echo hello from $GREET + ''; + + # https://devenv.sh/basics/ + enterShell = '' + hello # Run scripts directly + git --version # Use packages + ''; + + # https://devenv.sh/tasks/ + # tasks = { + # "myproj:setup".exec = "mytool build"; + # "devenv:enterShell".after = [ "myproj:setup" ]; + # }; + + # https://devenv.sh/tests/ + enterTest = '' + echo "Running tests" + git --version | grep --color=auto "${pkgs.git.version}" + ''; + + # https://devenv.sh/git-hooks/ + # git-hooks.hooks.shellcheck.enable = true; + + # See full reference at https://devenv.sh/reference/options/ +} diff --git a/devenv.yaml b/devenv.yaml new file mode 100644 index 0000000..37e8589 --- /dev/null +++ b/devenv.yaml @@ -0,0 +1,8 @@ +inputs: + nixpkgs: + url: github:cachix/devenv-nixpkgs/rolling + rust-overlay: + url: github:oxalica/rust-overlay + inputs: + nixpkgs: + follows: nixpkgs diff --git a/src/power.rs b/src/power.rs index fa7977b..e6ec905 100644 --- a/src/power.rs +++ b/src/power.rs @@ -8,6 +8,8 @@ pub const WATT_HORSEPOWER_FACTOR: f64 = 1.0 / 745.6998715822702; pub const WATT_BTU_MIN_FACTOR: f64 = 1.0 / 17.58426666666667; /// Number of kW in a W pub const WATT_KILOWATT_FACTOR: f64 = 1e-3; +/// Number of MW in a W +pub const WATT_MEGAWATT_FACTOR: f64 = 1e-6; /// Number of mW in a W pub const WATT_MILLIWATT_FACTOR: f64 = 1e3; /// Number of µW in a W @@ -74,6 +76,11 @@ impl Power { Self::from_watts(kw / WATT_KILOWATT_FACTOR) } + /// Create a new Power from a floating point value in Megawatts (kW) + pub fn from_megawatts(kw: f64) -> Power { + Self::from_watts(kw / WATT_MEGAWATT_FACTOR) + } + /// Convert this Power into a floating point value in Watts pub fn as_watts(&self) -> f64 { self.watts @@ -104,6 +111,11 @@ impl Power { self.watts * WATT_KILOWATT_FACTOR } + /// Convert this Power into a floating point value in megawatts (MW) + pub fn as_megawatts(&self) -> f64 { + self.watts * WATT_MEGAWATT_FACTOR + } + /// Convert this Power into a floating point value in milliwatts (mW) pub fn as_milliwatts(&self) -> f64 { self.watts * WATT_MILLIWATT_FACTOR @@ -190,6 +202,18 @@ mod test { assert_almost_eq(r2, 0.1); } + #[test] + pub fn as_megawatts() { + let i1 = Power::from_megawatts(100.0); + let r1 = i1.as_watts(); + + let i2 = Power::from_watts(100_000.0); + let r2 = i2.as_megawatts(); + + assert_almost_eq(r1, 100_000_000.0); + assert_almost_eq(r2, 0.1); + } + #[test] pub fn as_milliwatts() { let i1 = Power::from_milliwatts(100.0); diff --git a/src/voltage.rs b/src/voltage.rs index a4d02f7..ba677b6 100644 --- a/src/voltage.rs +++ b/src/voltage.rs @@ -42,6 +42,12 @@ impl Voltage { Self::from_volts(kilovolts * 1000.0) } + /// Create a new Voltage from a floating point value in Megavolts + pub fn from_megavolts(megavolts: f64) -> Self { + Self::from_volts(megavolts * 1000000.0) + } + + /// Convert this Voltage into a floating point value in Volts pub fn as_volts(&self) -> f64 { self.volts @@ -61,6 +67,11 @@ impl Voltage { pub fn as_kilovolts(&self) -> f64 { self.volts / 1000.0 } + + /// Convert this Voltage into a floating point value in Megavolts + pub fn as_megavolts(&self) -> f64 { + self.volts / 1000000.0 + } } impl Measurement for Voltage { @@ -108,6 +119,12 @@ mod test { assert_almost_eq(u.as_kilovolts(), 10.0); } + #[test] + pub fn as_megavolts() { + let u = Voltage::from_volts(10_000_000.0); + assert_almost_eq(u.as_megavolts(), 10.0); + } + #[test] pub fn as_volts() { let u = Voltage::from_kilovolts(1.234);