diff --git a/.editorconfig b/.editorconfig index 5b5cb2f..aab3429 100644 --- a/.editorconfig +++ b/.editorconfig @@ -7,7 +7,7 @@ trim_trailing_whitespace = true charset = utf-8 [*.rs] -indent_style = tab +indent_style = space indent_size = 4 [*.nix] diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..ea8c4bf --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/target diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..d5fd1ed --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,521 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 4 + +[[package]] +name = "anstream" +version = "0.6.21" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "43d5b281e737544384e969a5ccad3f1cdd24b48086a0fc1b2a5262a26b8f4f4a" +dependencies = [ + "anstyle", + "anstyle-parse", + "anstyle-query", + "anstyle-wincon", + "colorchoice", + "is_terminal_polyfill", + "utf8parse", +] + +[[package]] +name = "anstyle" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5192cca8006f1fd4f7237516f40fa183bb07f8fbdfedaa0036de5ea9b0b45e78" + +[[package]] +name = "anstyle-parse" +version = "0.2.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "4e7644824f0aa2c7b9384579234ef10eb7efb6a0deb83f9630a49594dd9c15c2" +dependencies = [ + "utf8parse", +] + +[[package]] +name = "anstyle-query" +version = "1.1.5" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "40c48f72fd53cd289104fc64099abca73db4166ad86ea0b4341abe65af83dadc" +dependencies = [ + "windows-sys", +] + +[[package]] +name = "anstyle-wincon" +version = "3.0.11" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "291e6a250ff86cd4a820112fb8898808a366d8f9f58ce16d1f538353ad55747d" +dependencies = [ + "anstyle", + "once_cell_polyfill", + "windows-sys", +] + +[[package]] +name = "append-override" +version = "0.1.0" +dependencies = [ + "clap", + "command-error", + "fs-err", + "libc", + "serde", + "serde_json", +] + +[[package]] +name = "autocfg" +version = "1.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c08606f8c3cbf4ce6ec8e28fb0014a2c086708fe954eaa885384a6165172e7e8" + +[[package]] +name = "bitflags" +version = "2.10.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "812e12b5285cc515a9c72a5c1d3b6d46a19dac5acfef5265968c166106e31dd3" + +[[package]] +name = "cfg-if" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9330f8b2ff13f34540b44e946ef35111825727b38d33286ef986142615121801" + +[[package]] +name = "cfg_aliases" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "613afe47fcd5fac7ccf1db93babcb082c5994d996f20b8b159f2ad1658eb5724" + +[[package]] +name = "clap" +version = "4.5.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c6e6ff9dcd79cff5cd969a17a545d79e84ab086e444102a591e288a8aa3ce394" +dependencies = [ + "clap_builder", + "clap_derive", +] + +[[package]] +name = "clap_builder" +version = "4.5.54" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fa42cf4d2b7a41bc8f663a7cab4031ebafa1bf3875705bfaf8466dc60ab52c00" +dependencies = [ + "anstream", + "anstyle", + "clap_lex", + "strsim", +] + +[[package]] +name = "clap_derive" +version = "4.5.49" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2a0b5487afeab2deb2ff4e03a807ad1a03ac532ff5a2cee5d86884440c7f7671" +dependencies = [ + "heck", + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "clap_lex" +version = "0.7.7" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c3e64b0cc0439b12df2fa678eae89a1c56a529fd067a9115f7827f1fffd22b32" + +[[package]] +name = "colorchoice" +version = "1.0.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b05b61dc5112cbb17e4b6cd61790d9845d13888356391624cbe7e41efeac1e75" + +[[package]] +name = "command-error" +version = "0.8.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2b6ac3abfcf15b4536b079bcee923683fe3dc1173b70be5e05ccf28ca112862e" +dependencies = [ + "dyn-clone", + "process-wrap", + "shell-words", + "utf8-command", +] + +[[package]] +name = "dyn-clone" +version = "1.0.20" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d0881ea181b1df73ff77ffaaf9c7544ecc11e82fba9b5f27b262a3c73a332555" + +[[package]] +name = "equivalent" +version = "1.0.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "877a4ace8713b0bcf2a4e7eec82529c029f1d0619886d18145fea96c3ffe5c0f" + +[[package]] +name = "fs-err" +version = "3.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "baf68cef89750956493a66a10f512b9e58d9db21f2a573c079c0bdf1207a54a7" +dependencies = [ + "autocfg", +] + +[[package]] +name = "hashbrown" +version = "0.16.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "841d1cc9bed7f9236f321df977030373f4a4163ae1a7dbfe1a51a2c1a51d9100" + +[[package]] +name = "heck" +version = "0.5.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "2304e00983f87ffb38b55b444b5e3b60a884b5d30c0fca7d82fe33449bbe55ea" + +[[package]] +name = "indexmap" +version = "2.13.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7714e70437a7dc3ac8eb7e6f8df75fd8eb422675fc7678aff7364301092b1017" +dependencies = [ + "equivalent", + "hashbrown", +] + +[[package]] +name = "is_terminal_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a6cb138bb79a146c1bd460005623e142ef0181e3d0219cb493e02f7d08a35695" + +[[package]] +name = "itoa" +version = "1.0.17" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "92ecc6618181def0457392ccd0ee51198e065e016d1d527a7ac1b6dc7c1f09d2" + +[[package]] +name = "libc" +version = "0.2.180" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "bcc35a38544a891a5f7c865aca548a982ccb3b8650a5b06d0fd33a10283c56fc" + +[[package]] +name = "memchr" +version = "2.7.6" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f52b00d39961fc5b2736ea853c9cc86238e165017a493d1d5c8eac6bdc4cc273" + +[[package]] +name = "nix" +version = "0.30.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74523f3a35e05aba87a1d978330aef40f67b0304ac79c1c00b294c9830543db6" +dependencies = [ + "bitflags", + "cfg-if", + "cfg_aliases", + "libc", +] + +[[package]] +name = "once_cell" +version = "1.21.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "42f5e15c9953c5e4ccceeb2e7382a716482c34515315f7b03532b8b4e8393d2d" + +[[package]] +name = "once_cell_polyfill" +version = "1.70.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "384b8ab6d37215f3c5301a95a4accb5d64aa607f1fcb26a11b5303878451b4fe" + +[[package]] +name = "pin-project-lite" +version = "0.2.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3b3cff922bd51709b605d9ead9aa71031d81447142d828eb4a6eba76fe619f9b" + +[[package]] +name = "proc-macro2" +version = "1.0.105" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "535d180e0ecab6268a3e718bb9fd44db66bbbc256257165fc699dadf70d16fe7" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "process-wrap" +version = "8.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "a3ef4f2f0422f23a82ec9f628ea2acd12871c81a9362b02c43c1aa86acfc3ba1" +dependencies = [ + "indexmap", + "nix", + "tracing", + "windows", +] + +[[package]] +name = "quote" +version = "1.0.43" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc74d9a594b72ae6656596548f56f667211f8a97b3d4c3d467150794690dc40a" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "serde" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9a8e94ea7f378bd32cbbd37198a4a91436180c5bb472411e48b5ec2e2124ae9e" +dependencies = [ + "serde_core", + "serde_derive", +] + +[[package]] +name = "serde_core" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "41d385c7d4ca58e59fc732af25c3983b67ac852c1a25000afe1175de458b67ad" +dependencies = [ + "serde_derive", +] + +[[package]] +name = "serde_derive" +version = "1.0.228" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d540f220d3187173da220f885ab66608367b6574e925011a9353e4badda91d79" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "serde_json" +version = "1.0.149" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "83fc039473c5595ace860d8c4fafa220ff474b3fc6bfdb4293327f1a37e94d86" +dependencies = [ + "itoa", + "memchr", + "serde", + "serde_core", + "zmij", +] + +[[package]] +name = "shell-words" +version = "1.1.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dc6fe69c597f9c37bfeeeeeb33da3530379845f10be461a66d16d03eca2ded77" + +[[package]] +name = "strsim" +version = "0.11.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7da8b5736845d9f2fcb837ea5d9e2628564b3b043a70948a3f0b778838c5fb4f" + +[[package]] +name = "syn" +version = "2.0.114" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "d4d107df263a3013ef9b1879b0df87d706ff80f65a86ea879bd9c31f9b307c2a" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "tracing" +version = "0.1.44" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "63e71662fa4b2a2c3a26f570f037eb95bb1f85397f3cd8076caed2f026a6d100" +dependencies = [ + "pin-project-lite", + "tracing-attributes", + "tracing-core", +] + +[[package]] +name = "tracing-attributes" +version = "0.1.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "7490cfa5ec963746568740651ac6781f701c9c5ea257c58e057f3ba8cf69e8da" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "tracing-core" +version = "0.1.36" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "db97caf9d906fbde555dd62fa95ddba9eecfd14cb388e4f491a66d74cd5fb79a" +dependencies = [ + "once_cell", +] + +[[package]] +name = "unicode-ident" +version = "1.0.22" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9312f7c4f6ff9069b165498234ce8be658059c6728633667c526e27dc2cf1df5" + +[[package]] +name = "utf8-command" +version = "1.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e4b151524c94cda49046b29e6d20b03092ff9363b02acc1bf3994da60910c55b" + +[[package]] +name = "utf8parse" +version = "0.2.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "06abde3611657adf66d383f00b093d7faecc7fa57071cce2578660c9f1010821" + +[[package]] +name = "windows" +version = "0.61.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9babd3a767a4c1aef6900409f85f5d53ce2544ccdfaa86dad48c91782c6d6893" +dependencies = [ + "windows-collections", + "windows-core", + "windows-future", + "windows-link 0.1.3", + "windows-numerics", +] + +[[package]] +name = "windows-collections" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3beeceb5e5cfd9eb1d76b381630e82c4241ccd0d27f1a39ed41b2760b255c5e8" +dependencies = [ + "windows-core", +] + +[[package]] +name = "windows-core" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "c0fdd3ddb90610c7638aa2b3a3ab2904fb9e5cdbecc643ddb3647212781c4ae3" +dependencies = [ + "windows-implement", + "windows-interface", + "windows-link 0.1.3", + "windows-result", + "windows-strings", +] + +[[package]] +name = "windows-future" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "fc6a41e98427b19fe4b73c550f060b59fa592d7d686537eebf9385621bfbad8e" +dependencies = [ + "windows-core", + "windows-link 0.1.3", + "windows-threading", +] + +[[package]] +name = "windows-implement" +version = "0.60.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "053e2e040ab57b9dc951b72c264860db7eb3b0200ba345b4e4c3b14f67855ddf" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-interface" +version = "0.59.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "3f316c4a2570ba26bbec722032c4099d8c8bc095efccdc15688708623367e358" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "windows-link" +version = "0.1.3" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "5e6ad25900d524eaabdbbb96d20b4311e1e7ae1699af4fb28c17ae66c80d798a" + +[[package]] +name = "windows-link" +version = "0.2.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f0805222e57f7521d6a62e36fa9163bc891acd422f971defe97d64e70d0a4fe5" + +[[package]] +name = "windows-numerics" +version = "0.2.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9150af68066c4c5c07ddc0ce30421554771e528bde427614c61038bc2c92c2b1" +dependencies = [ + "windows-core", + "windows-link 0.1.3", +] + +[[package]] +name = "windows-result" +version = "0.3.4" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56f42bd332cc6c8eac5af113fc0c1fd6a8fd2aa08a0119358686e5160d0586c6" +dependencies = [ + "windows-link 0.1.3", +] + +[[package]] +name = "windows-strings" +version = "0.4.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "56e6c93f3a0c3b36176cb1327a4958a0353d5d166c2a35cb268ace15e91d3b57" +dependencies = [ + "windows-link 0.1.3", +] + +[[package]] +name = "windows-sys" +version = "0.61.2" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "ae137229bcbd6cdf0f7b80a31df61766145077ddf49416a728b02cb3921ff3fc" +dependencies = [ + "windows-link 0.2.1", +] + +[[package]] +name = "windows-threading" +version = "0.1.0" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b66463ad2e0ea3bbf808b7f1d371311c80e115c0b71d60efc142cafbcfb057a6" +dependencies = [ + "windows-link 0.1.3", +] + +[[package]] +name = "zmij" +version = "1.0.16" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "dfcd145825aace48cff44a8844de64bf75feec3080e0aa5cdbde72961ae51a65" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..d0ea161 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,20 @@ +[package] +name = "append-override" +version = "0.1.0" +edition = "2024" + +[[bin]] +name = "append-override" +path = "src/main.rs" + +[lib] +name = "append_override" +path = "src/lib.rs" + +[dependencies] +clap = { version = "4.5.54", features = ["color", "derive"] } +command-error = "0.8.0" +fs-err = "3.2.2" +libc = { version = "0.2.180", features = ["extra_traits"] } +serde = { version = "1.0.228", features = ["derive"] } +serde_json = "1.0.149" diff --git a/flake.lock b/flake.lock new file mode 100644 index 0000000..9998eff --- /dev/null +++ b/flake.lock @@ -0,0 +1,79 @@ +{ + "nodes": { + "flake-utils": { + "inputs": { + "systems": "systems" + }, + "locked": { + "lastModified": 1731533236, + "narHash": "sha256-l0KFg5HjrsfsO/JpG+r7fRrqm12kzFHyUHqHCVpMMbI=", + "owner": "numtide", + "repo": "flake-utils", + "rev": "11707dc2f618dd54ca8739b309ec4fc024de578b", + "type": "github" + }, + "original": { + "owner": "numtide", + "repo": "flake-utils", + "type": "github" + } + }, + "nixpkgs": { + "flake": false, + "locked": { + "lastModified": 1768875095, + "narHash": "sha256-dYP3DjiL7oIiiq3H65tGIXXIT1Waiadmv93JS0sS+8A=", + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "ed142ab1b3a092c4d149245d0c4126a5d7ea00b0", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "qyriad-nur": { + "flake": false, + "locked": { + "lastModified": 1767357308, + "narHash": "sha256-PWDIBupTHASnzwPUuafIhwBCFKxjsSVj4QRAdX5y/z4=", + "owner": "Qyriad", + "repo": "nur-packages", + "rev": "c08309f918a0528ceb23659c0cc4a3c901fe8afa", + "type": "github" + }, + "original": { + "owner": "Qyriad", + "repo": "nur-packages", + "type": "github" + } + }, + "root": { + "inputs": { + "flake-utils": "flake-utils", + "nixpkgs": "nixpkgs", + "qyriad-nur": "qyriad-nur" + } + }, + "systems": { + "locked": { + "lastModified": 1681028828, + "narHash": "sha256-Vy1rq5AaRuLzOxct8nz4T6wlgyUR7zLU309k9mBC768=", + "owner": "nix-systems", + "repo": "default", + "rev": "da67096a3b9bf56a91d16901293e51ba5b49a27e", + "type": "github" + }, + "original": { + "owner": "nix-systems", + "repo": "default", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/package.nix b/package.nix index ef200cb..68878aa 100644 --- a/package.nix +++ b/package.nix @@ -2,10 +2,11 @@ lib, stdenv, rustHooks, + rustPackages, + versionCheckHook, +}: lib.callWith' rustPackages ({ rustPlatform, cargo, - clippy, - versionCheckHook, }: let cargoToml = lib.importTOML ./Cargo.toml; cargoPackage = cargoToml.package; @@ -50,13 +51,17 @@ in stdenv.mkDerivation (self: { in mkShell' { name = "${self.pname}-devshell-${self.version}"; inputsFrom = [ self.finalPackage ]; + packages = [ + rustPackages.rustc + rustPackages.rustfmt + ]; }; passthru.tests.clippy = self.finalPackage.overrideAttrs (prev: { pname = "${self.pname}-clippy"; nativeCheckInputs = prev.nativeCheckInputs or [ ] ++ [ - clippy + rustPackages.clippy ]; dontConfigure = true; @@ -78,4 +83,4 @@ in stdenv.mkDerivation (self: { meta = { mainProgram = "PKGNAME"; }; -}) +})) diff --git a/src/args.rs b/src/args.rs new file mode 100644 index 0000000..8de2c39 --- /dev/null +++ b/src/args.rs @@ -0,0 +1,75 @@ +use clap::ColorChoice; + +use crate::prelude::*; + +//#[derive(Debug, Clone, PartialEq)] +//#[derive(clap::Args)] +//#[group(required = true, multiple = false)] +//pub enum Config +//{ +// Flake, +//} + +#[derive(Debug, Clone, PartialEq)] +pub struct NixOsOption { + name: String, + value: String, +} + +#[derive(Debug, Clone, PartialEq)] +pub struct NixOptionParseError(pub Box); + +impl Display for NixOptionParseError { + fn fmt(&self, f: &mut Formatter) -> FmtResult { + write!(f, "{}", self.0) + } +} + +impl From for NixOptionParseError { + fn from(value: String) -> Self { + Self(value.into_boxed_str()) + } +} + +impl StdError for NixOptionParseError {} + +impl FromStr for NixOsOption { + type Err = NixOptionParseError; + + fn from_str(s: &str) -> Result { + // FIXME: allow escaping equals sign? + let Some(delim) = s.find('=') else { + return Err(format!("equals sign not found in {}", s).into()); + }; + + todo!(); + } +} + +#[derive(Debug, Clone, PartialEq, clap::Parser)] +#[command(version, about, author, arg_required_else_help(true))] +pub struct Parser { + #[arg(long, default_value = "auto")] + pub color: ColorChoice, + + #[arg(long)] + pub file: Box, + + #[arg(required = true)] + pub name: Box, + #[arg(required = true)] + pub value: Box, + ///// Flakeref to a base configuration to modify. + //#[arg(group = "config", long, default_value("."))] + //#[arg(long, default_value(Some(".")))] + //flake: Option>>, + // + //#[arg(group = "config", long)] + //expr: Option, +} + +//impl Parser { +// fn eval_cmd(&self) { +// todo!(); +// } +//} diff --git a/src/color.rs b/src/color.rs new file mode 100644 index 0000000..ea20507 --- /dev/null +++ b/src/color.rs @@ -0,0 +1,48 @@ +use std::{ + env, + sync::{LazyLock, OnceLock}, +}; + +#[allow(unused_imports)] +use crate::prelude::*; + +pub static CLI_ENABLE_COLOR: OnceLock = OnceLock::new(); + +fn is_color_reqd() -> bool { + CLI_ENABLE_COLOR.get().copied().unwrap_or(false) +} + +fn is_clicolor_forced() -> bool { + env::var("CLICOLOR_FORCE") + .map(|value| { + if value.is_empty() || value == "0" { + false + } else { + true + } + }) + .unwrap_or(false) +} + +pub static SHOULD_COLOR: LazyLock = LazyLock::new(|| is_clicolor_forced() || is_color_reqd()); + +/// Silly wrapper around LazyLock<&'static str> to impl Display. +pub(crate) struct _LazyLockDisplay(LazyLock<&'static str>); +impl Display for _LazyLockDisplay { + fn fmt(&self, f: &mut Formatter) -> FmtResult { + Display::fmt(&*self.0, f) + } +} + +pub(crate) const ANSI_CYAN: _LazyLockDisplay = _LazyLockDisplay(LazyLock::new(|| { + SHOULD_COLOR + .then_some("\x1b[36m") + .unwrap_or_default() +})); + +pub(crate) const ANSI_RESET: _LazyLockDisplay = _LazyLockDisplay(LazyLock::new(|| { + SHOULD_COLOR + // C'mon rustfmt, just format it to match ^. + .then_some("\x1b[0m") + .unwrap_or_default() +})); diff --git a/src/lib.rs b/src/lib.rs new file mode 100644 index 0000000..42f48c5 --- /dev/null +++ b/src/lib.rs @@ -0,0 +1,157 @@ +pub(crate) mod prelude { + #![allow(unused_imports)] + + pub use std::{ + error::Error as StdError, + ffi::{OsStr, OsString}, + fmt::{Display, Formatter, Result as FmtResult}, + io::{Error as IoError, Read, Seek, SeekFrom}, + path::{Path, PathBuf}, + process::{Command, ExitCode}, + str::FromStr, + }; + + #[cfg(unix)] + pub use std::os::{ + fd::AsRawFd, + unix::ffi::{OsStrExt, OsStringExt}, + }; + + pub type BoxDynError = Box; + + pub use command_error::{CommandExt, OutputLike}; + pub use fs_err::File; + #[cfg(unix)] + pub use fs_err::os::unix::fs::{FileExt, OpenOptionsExt}; +} + +use prelude::*; + +use std::{ + io::{BufRead, BufReader}, + sync::Arc, +}; + +pub mod args; +pub use args::Parser; +mod color; +pub use color::{CLI_ENABLE_COLOR, SHOULD_COLOR}; +pub mod line; +pub use line::Line; +pub mod source; +pub use source::SourceLine; + +use serde::{Deserialize, Serialize}; + +#[derive(Debug, Clone, PartialEq, Hash, Serialize, Deserialize)] +pub struct DefinitionWithLocation { + pub file: Box, + pub value: Box, +} + +pub fn get_where(option_name: &str, configuration_nix: &Path) -> Result, BoxDynError> { + let expr: OsString = [ + // foo + OsStr::new("import { configuration = "), + configuration_nix.as_os_str(), + OsStr::new("; }"), + ] + .into_iter() + .map(ToOwned::to_owned) + .collect(); + let result = Command::new("nix-instantiate") + .arg("--eval") + .arg("--json") + .arg("--strict") + .arg("--expr") + .arg(expr) + .arg("-A") + .arg(format!("options.{}.definitionsWithLocations", option_name)) + .output_checked_utf8()?; + let stdout = result.stdout(); + + let definitions: Box<[DefinitionWithLocation]> = serde_json::from_str(&stdout)?; + let last_location = definitions.last().unwrap(); + let file = &*last_location.file; + dbg!(&file); + + Ok(Box::from(file)) +} + +pub fn get_highest_prio( + option_name: &str, + file_with_definition: &Path, +) -> Result { + let mut file = File::options() + .read(true) + .write(true) + .create(false) + .custom_flags(libc::O_CLOEXEC) + .open(file_with_definition)?; + + // TODO: seek and read backwards. + + let mut lines = BufReader::new(&mut file) + .lines() + .enumerate() + .map(|(index, line_res)| { + line_res.map(|line| SourceLine { + line: Line::from_index(index as u64), + path: Arc::from(file_with_definition), + text: Arc::from(line), + }) + }) + .collect::, IoError>>()?; + lines.reverse(); + let lines_reversed = lines; + + // Get the current highest priority. + + let expr: OsString = [ + OsStr::new("import { configuration = "), + file_with_definition.as_os_str(), + OsStr::new("; }"), + ] + .into_iter() + .map(ToOwned::to_owned) + .collect(); + + // Get the highest priority, and the file its defined in. + let result = Command::new("nix-instantiate") + .arg("--eval") + .arg("--json") + .arg("--strict") + .arg("--expr") + .arg(expr) + .arg("-A") + .arg(format!("options.{}.highestPrio", option_name)) + .output_checked_utf8()?; + let stdout = result.stdout(); + let highest_prio = stdout.trim(); + + let needle = format!("lib.mkOverride ({})", highest_prio); + eprintln!("looking for {needle:?}"); + + let line_with_current_highest = lines_reversed + .iter() + .position(|line| { + eprintln!("looking for {needle} in {line}"); + + line.text.contains(&needle) + }) + .unwrap_or_else(|| { + panic!( + "couldn't find override number {highest_prio} in {}", + file_with_definition.display() + ) + }); + + let line = lines_reversed + .into_iter() + .nth(line_with_current_highest) + .unwrap(); + + eprintln!("found, on line index {}", line); + + Ok(line) +} diff --git a/src/line.rs b/src/line.rs new file mode 100644 index 0000000..7961b95 --- /dev/null +++ b/src/line.rs @@ -0,0 +1,32 @@ +use std::num::NonZeroU64; + +#[allow(unused_imports)] +use crate::prelude::*; + + +#[derive(Debug, Copy, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] +pub struct Line(pub u64); + +/// Constructors. +impl Line { + pub fn from_index(index: u64) -> Self { + Self(index) + } + + pub fn from_linenr(linenr: NonZeroU64) -> Self { + Self(linenr.get() - 1) + } +} + +/// Getters. +impl Line { + /// 0-indexed + pub fn index(self) -> u64 { + self.0 + } + + /// 1-indexed + pub fn linenr(self) -> u64 { + self.0 + 1 + } +} diff --git a/src/main.rs b/src/main.rs new file mode 100644 index 0000000..3889120 --- /dev/null +++ b/src/main.rs @@ -0,0 +1,40 @@ +use std::error::Error as StdError; +use std::io::IsTerminal; +use std::path::Path; +use std::process::ExitCode; + + +use clap::{ColorChoice, Parser as _}; + +fn main_wrapped() -> Result<(), Box> { + let args = append_override::Parser::parse(); + + let success = append_override::CLI_ENABLE_COLOR.set(match args.color { + ColorChoice::Always => true, + ColorChoice::Auto => std::io::stdin().is_terminal(), + ColorChoice::Never => false, + }); + if cfg!(debug_assertions) { + success.expect("logic error in CLI_ENABLE_COLOR"); + } + + // FIXME: handle relative paths without leading ./ + let filepath = Path::new(&args.file); + + // Get what file that thing is defined in. + let def_path = append_override::get_where(&args.name, filepath)?; + + append_override::get_highest_prio(&args.name, &def_path)?; + + Ok(()) +} + +fn main() -> ExitCode { + match main_wrapped() { + Ok(_) => ExitCode::SUCCESS, + Err(e) => { + eprintln!("append-override: error: {}", e); + ExitCode::FAILURE + } + } +} diff --git a/src/source.rs b/src/source.rs new file mode 100644 index 0000000..0a76b75 --- /dev/null +++ b/src/source.rs @@ -0,0 +1,59 @@ +use std::{ + hash::{Hash, Hasher}, + sync::{Arc, LazyLock}, +}; + +use crate::Line; +#[allow(unused_imports)] +use crate::prelude::*; +use crate::color::{ANSI_CYAN, ANSI_RESET}; + +#[derive(Debug, Clone, PartialEq, Hash)] +pub struct SourceLine { + pub line: Line, + pub path: Arc, + pub text: Arc, +} + +impl Display for SourceLine { + fn fmt(&self, f: &mut Formatter) -> FmtResult { + write!(f, "line {:03}: `{ANSI_CYAN}{}{ANSI_RESET}`", self.line.linenr(), self.text.trim()) + } +} + +#[derive(Debug, Clone, PartialEq, Hash)] +pub struct SourcePath { + path: Arc, +} + +#[derive(Debug, Clone)] +pub struct SourceFile { + path: Arc, + file: Arc, +} + +impl PartialEq for SourceFile { + fn eq(&self, other: &Self) -> bool { + let paths_match = *self.path == *other.path; + if paths_match && self.file.as_raw_fd() != other.file.as_raw_fd() { + todo!("handle the case where source file paths match but FD numbers don't"); + } + paths_match + } +} + +impl Hash for SourceFile { + fn hash(&self, state: &mut H) { + self.path.hash(state); + self.file.as_raw_fd().hash(state); + } +} + +#[derive(Debug, Clone, PartialEq, Hash)] +enum SourceInner { + Path(SourcePath), + File(SourceFile), +} + +#[derive(Debug, Clone, PartialEq, Hash)] +pub struct Source(SourceInner);