Skip to content

Instantly share code, notes, and snippets.

@oscarvarto
Created September 21, 2024 06:49
Show Gist options
  • Save oscarvarto/10a8aa4b6489c0a264f3e23152ae1e42 to your computer and use it in GitHub Desktop.
Save oscarvarto/10a8aa4b6489c0a264f3e23152ae1e42 to your computer and use it in GitHub Desktop.
Simple command line parser with clap
use clap::Parser;
use std::fmt::Debug;
use std::str::FromStr;
#[derive(Debug, PartialEq, Clone)]
enum Browser {
Chrome,
Firefox,
Edge,
}
impl FromStr for Browser {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_lowercase().as_str() {
"chrome" => Ok(Browser::Chrome),
"firefox" => Ok(Browser::Firefox),
"edge" => Ok(Browser::Edge),
_ => Err("Invalid browser option"),
}
}
}
// Simple command-line argument parser
#[derive(Parser, Debug)]
#[command(version, about, long_about = None)]
struct Args {
// Name of the environment (and associated properties)
#[arg(long, default_value = "v2.0")]
env: String,
// (Optional) Test suite to execute in src/test/resources/testsuites
#[arg(long, default_value = None)]
xml: Option<String>,
// (Optional) Browser to use for testing
#[arg(long, default_value = "Chrome")]
browser: Browser,
// (Optional) Selenium WebDriverWait value in seconds
#[arg(long, default_value = "10")]
wait: u8,
// (Optional) Regex pattern for test cases to execute with maven failsafe plugin
#[arg(long, default_value = ".*")]
tests: String,
}
// cargo run -- --browser=Firefox --env=v1.0 --wait=30 --xml=smoke.xml
fn main() {
let args = Args::parse();
// Parse the environment or set the default value
let environment: String = args.env;
// Parse the test suite
let test_suite = args.xml;
// Parse the browser
let browser: String = format!("{:?}", args.browser);
// Parse the Selenium WebDriverWait value
let wait = args.wait;
// Parse the regex pattern for test cases
let tests = args.tests;
// Perform the desired actions based on the parsed arguments
println!("Environment: {}", environment);
println!("Test suite: {:?}", test_suite);
println!("Browser: {:?}", browser);
println!("Selenium WebDriverWait: {} seconds", wait);
if tests == ".*" {
println!("Regex pattern for test cases: Default (matches all test cases)");
} else {
println!("Regex pattern for test cases: {}", tests);
}
}
@oscarvarto
Copy link
Author

Some elisp to work with Rustic, Emacs:

;;; my-rust-config.el -*- lexical-binding: t; -*-

(custom-set-faces
  '(rustic-compilation-column ((t (:inherit compilation-column-number))))
  '(rustic-compilation-line ((t (:foreground "LimeGreen")))))

(defun rustic-mode-auto-save-hook ()
  "Enable auto-saving in rustic-mode buffers."
  (when buffer-file-name
    (setq-local compilation-ask-about-save nil)))
(add-hook 'rustic-mode-hook 'rustic-mode-auto-save-hook)

(setq rustic-rustfmt-args "+nightly")
(setq rustic-rustfmt-config-alist '((hard_tabs . t) (skip_children . nil)))

(use-package rust-mode
  :init
  (setq rust-mode-treesitter-derive t))

(use-package rustic
  :custom
  (let ((toolchain (or (getenv "RUST_TOOLCHAIN")
                       (string-trim (shell-command-to-string "nu -c \"rustup show | lines | get 14 | split row ' ' | get 0\"")) ;; e.g. "1.77.2-aarch64-apple-darwin"
                       "nightly")))
    (setq rustic-analyzer-command `("rustup" "run" ,toolchain "rust-analyzer"))))

(with-eval-after-load 'rustic-mode
  (add-hook 'rustic-mode-hook 'lsp-ui-mode)
  (add-hook 'flycheck-mode-hook #'flycheck-rust-setup))

;; TODO: https://robert.kra.hn/posts/rust-emacs-setup/
(use-package lsp-mode
  :commands lsp
  :custom
  ;; what to use when checking on-save. "check" is default, I prefer clippy
  (lsp-rust-analyzer-cargo-watch-command "clippy")
  (lsp-eldoc-render-all t)
  (lsp-idle-delay 0.6)
  ;; enable / disable the hints as you prefer:
  (lsp-inlay-hint-enable t)
  ;; These are optional configurations. See https://emacs-lsp.github.io/lsp-mode/page/lsp-rust-analyzer/#lsp-rust-analyzer-display-chaining-hints for a full list
  (lsp-rust-analyzer-display-lifetime-elision-hints-enable "skip_trivial")
  (lsp-rust-analyzer-display-chaining-hints t)
  (lsp-rust-analyzer-display-lifetime-elision-hints-use-parameter-names nil)
  (lsp-rust-analyzer-display-closure-return-type-hints t)
  (lsp-rust-analyzer-display-parameter-hints nil)
  (lsp-rust-analyzer-display-reborrow-hints nil)
  :config
  (add-hook 'lsp-mode-hook 'lsp-ui-mode))

(require 'lsp-ui)
(setq! lsp-ui-peek-always-show t
       lsp-ui-sideline-show-hover t
       lsp-ui-doc-enable t)

Use it with lsp-booster for better LSP performance:

(after! lsp-mode
 ;; https://github.com/emacs-lsp/lsp-mode/issues/3577#issuecomment-1709232622
 ;;(delete 'lsp-terraform lsp-client-packages)
 (setq! lsp-file-watch-threshold 20000
       lsp-inlay-hint-enable t))

(defun lsp-booster--advice-json-parse (old-fn &rest args)
 "Try to parse bytecode instead of json."
 (or
  (when (equal (following-char) ?#)
    (let ((bytecode (read (current-buffer))))
      (when (byte-code-function-p bytecode)
        (funcall bytecode))))
  (apply old-fn args)))

(advice-add (if (progn (require 'json)
                      (fboundp 'json-parse-buffer))
               'json-parse-buffer
             'json-read)
           :around
           #'lsp-booster--advice-json-parse)

(defun lsp-booster--advice-final-command (old-fn cmd &optional test?)
 "Prepend emacs-lsp-booster command to lsp CMD."
 (let ((orig-result (funcall old-fn cmd test?)))
   (if (and (not test?)                             ;; for check lsp-server-present?
            (not (file-remote-p default-directory)) ;; see lsp-resolve-final-command, it would add extra shell wrapper
            lsp-use-plists
            (not (functionp 'json-rpc-connection))  ;; native json-rpc
            (executable-find "emacs-lsp-booster"))
       (progn
         (message "Using emacs-lsp-booster for %s!" orig-result)
         (cons "emacs-lsp-booster" orig-result))
     orig-result)))

(advice-add 'lsp-resolve-final-command :around #'lsp-booster--advice-final-command)

And for debugging with DAP:

(use-package dap-mode
  :config
  (dap-ui-mode)
  (dap-ui-controls-mode 1)

  (require 'dap-lldb)
  (require 'dap-gdb-lldb)
  ;; installs .extension/vscode
  (dap-gdb-lldb-setup)
  ;;(setq dap-gdb-lldb-path "/Users/oscarvarto/doom-emacs/.local/etc/dap-extension/vscode/webfreak.debug")

  ;;https://users.rust-lang.org/t/debugging-in-emacs-doom/99540/2
  (require 'dap-codelldb)
  (dap-codelldb-setup)

  ;; TODO: Find a way to change the :program argument without hardcoding it's value (learn to use dap-hydra)
  (dap-register-debug-template
   "Rust::LLDB Run Configuration"
   (list :type "lldb"
         :request "launch"
         :name "LLDB::Run"
         :gdbpath "rust-lldb"
         :target nil
         :program "/Users/oscarvarto/git-repos/nushell/target/debug/nu"
         :cwd nil)))

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment