From 3a9f283e99eb512cdd44622cb0dfc4286951bfde Mon Sep 17 00:00:00 2001 From: Markus Scully Date: Thu, 14 Nov 2024 20:56:28 +0000 Subject: [PATCH] Init commit --- .envrc | 3 + .gitignore | 4 + Cargo.lock | 54 +++++++++++ Cargo.toml | 6 ++ LICENSE | 21 +++++ README.md | 25 +++++ autolingual-macro/Cargo.toml | 14 +++ autolingual-macro/src/lib.rs | 18 ++++ autolingual/Cargo.toml | 9 ++ autolingual/src/lib.rs | 12 +++ devenv.lock | 178 +++++++++++++++++++++++++++++++++++ devenv.nix | 60 ++++++++++++ devenv.yaml | 14 +++ 13 files changed, 418 insertions(+) create mode 100644 .envrc create mode 100644 .gitignore create mode 100644 Cargo.lock create mode 100644 Cargo.toml create mode 100644 LICENSE create mode 100644 README.md create mode 100644 autolingual-macro/Cargo.toml create mode 100644 autolingual-macro/src/lib.rs create mode 100644 autolingual/Cargo.toml create mode 100644 autolingual/src/lib.rs create mode 100644 devenv.lock create mode 100644 devenv.nix create mode 100644 devenv.yaml diff --git a/.envrc b/.envrc new file mode 100644 index 0000000..5bf8fc1 --- /dev/null +++ b/.envrc @@ -0,0 +1,3 @@ +source_url "https://raw.githubusercontent.com/cachix/devenv/95f329d49a8a5289d31e0982652f7058a189bfca/direnvrc" "sha256-d+8cBpDfDBj41inrADaJt+bDWhOktwslgoP5YiGJ1v0=" + +use devenv \ No newline at end of file diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..4403765 --- /dev/null +++ b/.gitignore @@ -0,0 +1,4 @@ +/target +.devenv* +devenv.local.nix +.pre-commit-config.yaml diff --git a/Cargo.lock b/Cargo.lock new file mode 100644 index 0000000..599e2ed --- /dev/null +++ b/Cargo.lock @@ -0,0 +1,54 @@ +# This file is automatically @generated by Cargo. +# It is not intended for manual editing. +version = 3 + +[[package]] +name = "autolingual" +version = "0.1.0" +dependencies = [ + "autolingual-macro", +] + +[[package]] +name = "autolingual-macro" +version = "0.1.0" +dependencies = [ + "proc-macro2", + "quote", + "syn", +] + +[[package]] +name = "proc-macro2" +version = "1.0.89" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "f139b0662de085916d1fb67d2b4169d1addddda1919e696f3252b740b629986e" +dependencies = [ + "unicode-ident", +] + +[[package]] +name = "quote" +version = "1.0.37" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "b5b9d34b8991d19d98081b46eacdd8eb58c6f2b201139f7c5f643cc155a633af" +dependencies = [ + "proc-macro2", +] + +[[package]] +name = "syn" +version = "2.0.87" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25aa4ce346d03a6dcd68dd8b4010bcb74e54e62c90c573f394c46eae99aba32d" +dependencies = [ + "proc-macro2", + "quote", + "unicode-ident", +] + +[[package]] +name = "unicode-ident" +version = "1.0.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "e91b56cd4cadaeb79bbf1a5645f6b4f8dc5bde8834ad5894a8db35fda9efa1fe" diff --git a/Cargo.toml b/Cargo.toml new file mode 100644 index 0000000..c1987b8 --- /dev/null +++ b/Cargo.toml @@ -0,0 +1,6 @@ +[workspace] +members = [ + "autolingual", + "autolingual-macro" +] +resolver = "2" diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..d9dcb8a --- /dev/null +++ b/LICENSE @@ -0,0 +1,21 @@ +MIT License + +Copyright (c) 2024 Markus Scully + +Permission is hereby granted, free of charge, to any person obtaining a copy +of this software and associated documentation files (the "Software"), to deal +in the Software without restriction, including without limitation the rights +to use, copy, modify, merge, publish, distribute, sublicense, and/or sell +copies of the Software, and to permit persons to whom the Software is +furnished to do so, subject to the following conditions: + +The above copyright notice and this permission notice shall be included in all +copies or substantial portions of the Software. + +THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR +IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, +FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE +AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER +LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, +OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE +SOFTWARE. diff --git a/README.md b/README.md new file mode 100644 index 0000000..a5b67ce --- /dev/null +++ b/README.md @@ -0,0 +1,25 @@ +# Autolingual + +Procedural macros in Rust can execute arbitrary code at compile time, even performing network requests. This has potentially terrifying security implications, but we'll use it to our advantage! + +Autolingual is a Rust macro which automatically translates strings of natural language text into multiple foreign languages at once at compile time. The produced translations are `&'static str` values, so they are very lightweight. + + +## Examples + +```rs +use autolingual::{translate, TranslationSet}; + +// No context +let button_text_french: &str = translate!("Sign up").fr; + +// With context (context will not show up in the final translation) +let button_text_french: &str = translate!("Sign up", "A button for creating a user account.").fr; + +// Every supported language at once. +let translation_set: TranslationSet = translate!("Hello!"); +let french = translation_set.fr; +let german = translation_set.de; +let portuguese = translation_set.pt; +// ... +``` diff --git a/autolingual-macro/Cargo.toml b/autolingual-macro/Cargo.toml new file mode 100644 index 0000000..2b83f84 --- /dev/null +++ b/autolingual-macro/Cargo.toml @@ -0,0 +1,14 @@ +[package] +name = "autolingual-macro" +version = "0.1.0" +edition = "2021" +license = "MIT" +description = "Procedural macros for autolingual" + +[lib] +proc-macro = true + +[dependencies] +proc-macro2 = "1.0" +quote = "1.0" +syn = { version = "2.0", features = ["full"] } diff --git a/autolingual-macro/src/lib.rs b/autolingual-macro/src/lib.rs new file mode 100644 index 0000000..69302f2 --- /dev/null +++ b/autolingual-macro/src/lib.rs @@ -0,0 +1,18 @@ +use proc_macro::TokenStream; +use quote::quote; +use syn::{parse_macro_input, LitStr}; + +#[proc_macro] +pub fn translate(input: TokenStream) -> TokenStream { + let input = parse_macro_input!(input as LitStr); + let text = input.value(); + + // TODO: Implement actual translation logic + + quote! { + ::autolingual::TranslationSet { + en: #text, + fr: "TODO", + } + }.into() +} diff --git a/autolingual/Cargo.toml b/autolingual/Cargo.toml new file mode 100644 index 0000000..a6f12a1 --- /dev/null +++ b/autolingual/Cargo.toml @@ -0,0 +1,9 @@ +[package] +name = "autolingual" +version = "0.1.0" +edition = "2021" +license = "MIT" +description = "Compile-time natural language translations using translation APIs" + +[dependencies] +autolingual-macro = { path = "../autolingual-macro", version = "0.1.0" } diff --git a/autolingual/src/lib.rs b/autolingual/src/lib.rs new file mode 100644 index 0000000..05ab740 --- /dev/null +++ b/autolingual/src/lib.rs @@ -0,0 +1,12 @@ +pub use autolingual_macro::translate; + +pub struct Language { + pub code: &'static str, + pub name: &'static str, +} + +#[derive(Debug)] +pub struct TranslationSet { + pub en: &'static str, + pub fr: &'static str, +} diff --git a/devenv.lock b/devenv.lock new file mode 100644 index 0000000..3d25646 --- /dev/null +++ b/devenv.lock @@ -0,0 +1,178 @@ +{ + "nodes": { + "devenv": { + "locked": { + "dir": "src/modules", + "lastModified": 1731619804, + "owner": "cachix", + "repo": "devenv", + "rev": "87edaaf1dddf17fe16eabab3c8edaf7cca2c3bc2", + "treeHash": "c0c2fd8edf08b32c9da79d4bb917690dc5b126d0", + "type": "github" + }, + "original": { + "dir": "src/modules", + "owner": "cachix", + "repo": "devenv", + "type": "github" + } + }, + "fenix": { + "inputs": { + "nixpkgs": [ + "nixpkgs" + ], + "rust-analyzer-src": "rust-analyzer-src" + }, + "locked": { + "lastModified": 1731565929, + "owner": "nix-community", + "repo": "fenix", + "rev": "4c6c7d5088f12f57afd4ba6449f9eb168ca05620", + "treeHash": "06a361535bdfec44eb64eb02dc5628ea71b2f488", + "type": "github" + }, + "original": { + "owner": "nix-community", + "repo": "fenix", + "type": "github" + } + }, + "flake-compat": { + "flake": false, + "locked": { + "lastModified": 1696426674, + "owner": "edolstra", + "repo": "flake-compat", + "rev": "0f9255e01c2351cc7d116c072cb317785dd33b33", + "treeHash": "2addb7b71a20a25ea74feeaf5c2f6a6b30898ecb", + "type": "github" + }, + "original": { + "owner": "edolstra", + "repo": "flake-compat", + "type": "github" + } + }, + "gitignore": { + "inputs": { + "nixpkgs": [ + "pre-commit-hooks", + "nixpkgs" + ] + }, + "locked": { + "lastModified": 1709087332, + "owner": "hercules-ci", + "repo": "gitignore.nix", + "rev": "637db329424fd7e46cf4185293b9cc8c88c95394", + "treeHash": "ca14199cabdfe1a06a7b1654c76ed49100a689f9", + "type": "github" + }, + "original": { + "owner": "hercules-ci", + "repo": "gitignore.nix", + "type": "github" + } + }, + "nixpkgs": { + "locked": { + "lastModified": 1716977621, + "owner": "cachix", + "repo": "devenv-nixpkgs", + "rev": "4267e705586473d3e5c8d50299e71503f16a6fb6", + "treeHash": "6d9f1f7ca0faf1bc2eeb397c78a49623260d3412", + "type": "github" + }, + "original": { + "owner": "cachix", + "ref": "rolling", + "repo": "devenv-nixpkgs", + "type": "github" + } + }, + "nixpkgs-stable": { + "locked": { + "lastModified": 1731386116, + "owner": "NixOS", + "repo": "nixpkgs", + "rev": "689fed12a013f56d4c4d3f612489634267d86529", + "treeHash": "8786ed18f037bad8b80b7a75c0e7b21a6769ee87", + "type": "github" + }, + "original": { + "owner": "NixOS", + "ref": "nixos-24.05", + "repo": "nixpkgs", + "type": "github" + } + }, + "nixpkgs-unstable": { + "locked": { + "lastModified": 1731531548, + "owner": "nixos", + "repo": "nixpkgs", + "rev": "24f0d4acd634792badd6470134c387a3b039dace", + "treeHash": "a35f71a0fa18e0a51dfeead7b9c971ed73043752", + "type": "github" + }, + "original": { + "owner": "nixos", + "ref": "nixpkgs-unstable", + "repo": "nixpkgs", + "type": "github" + } + }, + "pre-commit-hooks": { + "inputs": { + "flake-compat": "flake-compat", + "gitignore": "gitignore", + "nixpkgs": [ + "nixpkgs" + ], + "nixpkgs-stable": "nixpkgs-stable" + }, + "locked": { + "lastModified": 1731363552, + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "rev": "cd1af27aa85026ac759d5d3fccf650abe7e1bbf0", + "treeHash": "d246c8c2f312332367d56063d2b049419880acc7", + "type": "github" + }, + "original": { + "owner": "cachix", + "repo": "pre-commit-hooks.nix", + "type": "github" + } + }, + "root": { + "inputs": { + "devenv": "devenv", + "fenix": "fenix", + "nixpkgs": "nixpkgs", + "nixpkgs-unstable": "nixpkgs-unstable", + "pre-commit-hooks": "pre-commit-hooks" + } + }, + "rust-analyzer-src": { + "flake": false, + "locked": { + "lastModified": 1731342671, + "owner": "rust-lang", + "repo": "rust-analyzer", + "rev": "fc98e0657abf3ce07eed513e38274c89bbb2f8ad", + "treeHash": "7c4a6b17fbe9e655cac7d03a3c034456b8f60a1f", + "type": "github" + }, + "original": { + "owner": "rust-lang", + "ref": "nightly", + "repo": "rust-analyzer", + "type": "github" + } + } + }, + "root": "root", + "version": 7 +} diff --git a/devenv.nix b/devenv.nix new file mode 100644 index 0000000..5aa697f --- /dev/null +++ b/devenv.nix @@ -0,0 +1,60 @@ +{ + pkgs, + lib, + config, + inputs, + ... +}: + +let + pkgs-unstable = import inputs.nixpkgs-unstable { + system = pkgs.stdenv.system; + config.allowUnfree = true; + }; +in +{ + # https://devenv.sh/basics/ + env.GREET = "devenv"; + + # https://devenv.sh/packages/ + packages = [ + pkgs-unstable.git + pkgs-unstable.rustup + pkgs-unstable.code-cursor + pkgs-unstable.curl + pkgs-unstable.jq + ]; + + # https://devenv.sh/languages/ + languages.rust = { + channel = "nightly"; + components = [ + "cargo" + "rust-src" + "rustc" + "clippy" + ]; + enable = true; + }; + + enterShell = '' + rustup component add clippy + ''; + + enterTest = '' + echo "Running tests" + git --version | grep --color=auto "${pkgs.git.version}" + cargo clippy -- -D warnings + ''; + + pre-commit.hooks = { + shellcheck.enable = true; + clippy = { + enable = true; + name = "clippy"; + entry = "cargo clippy -- -D warnings"; + files = "\\.rs$"; + language = "system"; + }; + }; +} diff --git a/devenv.yaml b/devenv.yaml new file mode 100644 index 0000000..90a9f99 --- /dev/null +++ b/devenv.yaml @@ -0,0 +1,14 @@ +# yaml-language-server: $schema=https://devenv.sh/devenv.schema.json +inputs: + fenix: + url: github:nix-community/fenix + inputs: + nixpkgs: + follows: nixpkgs + nixpkgs: + url: github:cachix/devenv-nixpkgs/rolling + nixpkgs-unstable: + url: github:nixos/nixpkgs/nixpkgs-unstable + +allowUnfree: true +