Skip to content

Commit c981631

Browse files
author
Test User
committed
refactor: eliminate hardcoded values by introducing comprehensive constants
- Add 150+ new constants to constants.rs for all UI messages and prompts - Replace hardcoded strings in file_copy.rs with named constants - Update git.rs to use TIME_FORMAT constant - Update all test files to use constants instead of literals - Fix EMOJI_DETACHED constant to match test expectations This improves maintainability by centralizing all string literals and makes the codebase more consistent and easier to update.
1 parent 68342d4 commit c981631

9 files changed

+375
-93
lines changed

src/constants.rs

Lines changed: 241 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -46,6 +46,10 @@ pub const DEFAULT_BRANCH_DETACHED: &str = "detached";
4646
pub const DEFAULT_AUTHOR_UNKNOWN: &str = "Unknown";
4747
pub const DEFAULT_MESSAGE_NONE: &str = "No message";
4848

49+
// Git commit info defaults (used in git.rs)
50+
pub const GIT_COMMIT_AUTHOR_UNKNOWN: &str = "Unknown";
51+
pub const GIT_COMMIT_MESSAGE_NONE: &str = "No message";
52+
4953
// Time Format
5054
pub const TIME_FORMAT: &str = "%Y-%m-%d %H:%M";
5155

@@ -210,11 +214,51 @@ pub const PROMPT_DELETE_WORKTREE: &str = "Are you sure you want to delete worktr
210214
pub const PROMPT_RENAME_BRANCH: &str = "Rename branch '{}' to '{}' as well?";
211215
pub const PROMPT_NEW_WORKTREE_NAME: &str = "New worktree name";
212216
pub const PROMPT_CLEANUP_CONFIRM: &str = "Delete {} worktrees?";
217+
pub const PROMPT_CONFLICT_ACTION: &str = "What would you like to do?";
213218

214219
// Success messages
215220
pub const SUCCESS_WORKTREE_CREATED: &str = "Worktree created successfully!";
216221
pub const SUCCESS_OPERATION_COMPLETED: &str = "Operation completed!";
217222

223+
// Additional UI messages
224+
pub const MSG_CREATING_FIRST_WORKTREE: &str = "Creating first worktree...";
225+
pub const MSG_PRESS_ESC_TO_CANCEL: &str = " (ESC to cancel)";
226+
227+
// Git error messages
228+
pub const GIT_BRANCH_NOT_FOUND_MSG: &str = "Branch '{}' not found";
229+
pub const GIT_CANNOT_RENAME_CURRENT: &str =
230+
"Cannot rename current worktree. Please switch to another worktree first.";
231+
pub const GIT_WORKTREE_NOT_FOUND: &str = "Worktree not found: {}";
232+
pub const GIT_INVALID_BRANCH_NAME: &str = "Invalid branch name: {}";
233+
234+
// Emoji icons
235+
pub const EMOJI_HOME: &str = "🏠";
236+
pub const EMOJI_LOCKED: &str = "🔒";
237+
pub const EMOJI_BRANCH: &str = "🌿";
238+
pub const EMOJI_DETACHED: &str = "🔗";
239+
pub const EMOJI_FOLDER: &str = "📁";
240+
241+
// File operations
242+
pub const FILE_COPY_COPYING_FILES: &str = "Copying configured files...";
243+
pub const FILE_COPY_NO_FILES: &str = "No files were copied";
244+
pub const FILE_COPY_SKIPPED_LARGE: &str = "Skipping large file";
245+
pub const FILE_COPY_FAILED: &str = "Failed to copy";
246+
pub const FILE_COPY_SKIPPING_UNSAFE: &str = "Skipping unsafe path";
247+
pub const FILE_COPY_NOT_FOUND: &str = "Not found";
248+
pub const FILE_COPY_COPIED_SUCCESS: &str = "Copied";
249+
pub const SIZE_UNIT_MB: &str = "MB";
250+
251+
// Error detection patterns
252+
pub const ERROR_NO_SUCH_FILE: &str = "No such file or directory";
253+
pub const ERROR_NOT_FOUND: &str = "not found";
254+
255+
// Main worktree detection
256+
pub const MAIN_WORKTREE_NAMES: &[&str] = &["main", "master"];
257+
258+
// File size formatting
259+
pub const FILE_SIZE_MB_SUFFIX: &str = " MB";
260+
pub const FILE_SIZE_DECIMAL_PLACES: usize = 1;
261+
218262
// Warning messages
219263
pub const WARNING_NO_WORKTREES: &str = "• No worktrees found.";
220264
pub const WARNING_NO_WORKTREES_SEARCH: &str = "• No worktrees to search.";
@@ -296,6 +340,31 @@ pub const OPTION_CREATE_FROM_HEAD: &str = "Create from current HEAD";
296340
pub const OPTION_SELECT_BRANCH: &str = "Select existing branch";
297341
pub const OPTION_SELECT_TAG: &str = "Select tag";
298342

343+
// Default selection indices
344+
pub const DEFAULT_MENU_SELECTION: usize = 0;
345+
346+
// Section headers
347+
pub const HEADER_WORKTREES: &str = "Worktrees";
348+
pub const HEADER_SEARCH_WORKTREES: &str = "Search Worktrees";
349+
pub const HEADER_CREATE_WORKTREE: &str = "Create New Worktree";
350+
351+
// Input prompts (additional)
352+
pub const PROMPT_SELECT_WORKTREE_SWITCH: &str = "Select a worktree to switch to";
353+
pub const PROMPT_SELECT_WORKTREE_LOCATION: &str = "Select worktree location pattern";
354+
pub const PROMPT_SELECT_BRANCH_OPTION: &str = "Select branch option";
355+
356+
// Environment variables
357+
pub const ENV_EDITOR: &str = "EDITOR";
358+
pub const ENV_VISUAL: &str = "VISUAL";
359+
360+
// Default editors
361+
pub const DEFAULT_EDITOR_WINDOWS: &str = "notepad";
362+
pub const DEFAULT_EDITOR_UNIX: &str = "vi";
363+
364+
// Error messages (additional)
365+
pub const ERROR_WORKTREE_NAME_EMPTY: &str = "Worktree name cannot be empty";
366+
pub const ERROR_CUSTOM_PATH_EMPTY: &str = "Custom path cannot be empty";
367+
299368
// Worktree location options
300369
pub const OPTION_SAME_LEVEL: &str = "Same level as repository";
301370
pub const OPTION_SUBDIRECTORY: &str = "In repository subdirectory (recommended)";
@@ -407,3 +476,175 @@ pub const GIT_URL_SUFFIX: &str = ".git";
407476
// Path component indices and minimums
408477
pub const PATH_COMPONENT_SECOND_INDEX: usize = 1;
409478
pub const MIN_PATH_COMPONENTS_FOR_SUBDIR: usize = 1;
479+
480+
// Additional constants that were identified during hardcode audit
481+
pub const MSG_NO_WORKTREES_TO_SEARCH: &str = "• No worktrees to search.";
482+
pub const MSG_SEARCH_FUZZY_ENABLED: &str = "Type to search worktrees (fuzzy search enabled):";
483+
pub const MSG_ALREADY_IN_WORKTREE: &str = "• Already in this worktree.";
484+
pub const SEARCH_CURRENT_INDICATOR: &str = " (current)";
485+
486+
// File copy operation constants
487+
pub const FILE_COPY_SAME_DIRECTORY: &str = "Source and destination are the same directory";
488+
pub const FILE_COPY_SKIPPING_LARGE: &str = "Skipping large file";
489+
490+
// Pluralization helpers
491+
pub const PLURAL_EMPTY: &str = "";
492+
pub const PLURAL_S: &str = "s";
493+
494+
// Git operation error messages
495+
pub const GIT_CANNOT_FIND_PARENT: &str = "Cannot find parent directory";
496+
pub const GIT_CANNOT_RENAME_DETACHED: &str = "Cannot rename worktree with detached HEAD";
497+
pub const GIT_NEW_NAME_NO_SPACES: &str = "New name cannot contain spaces";
498+
499+
// Additional hardcoded values found in fourth audit
500+
// Error context messages
501+
pub const ERROR_FAILED_TO_CREATE_PARENT_DIR: &str = "Failed to create parent directory: ";
502+
pub const ERROR_FAILED_TO_COPY_FILE: &str = "Failed to copy file from {} to {}";
503+
pub const ERROR_FAILED_TO_CREATE_DIR: &str = "Failed to create directory: ";
504+
pub const ERROR_SOURCE_PATH_NOT_FOUND: &str = "Source path not found: ";
505+
pub const ERROR_SOURCE_NOT_FILE_OR_DIR: &str = "Source is neither a file nor a directory: ";
506+
pub const ERROR_MAX_DEPTH_EXCEEDED: &str =
507+
"Maximum directory depth ({}) exceeded. Possible circular reference.";
508+
pub const ERROR_GIT_DIR_NO_PARENT: &str = "Git directory has no parent";
509+
pub const ERROR_NO_MAIN_WORKTREE_FOUND: &str = "No main worktree found with {} file";
510+
pub const ERROR_REPO_NO_WORKING_DIR: &str = "Repository has no working directory";
511+
512+
// Info messages
513+
pub const INFO_SKIPPING_SYMLINK: &str = "Skipping symlink: ";
514+
pub const INFO_SKIPPING_CIRCULAR_REF: &str = "Skipping circular reference: ";
515+
pub const INFO_FAILED_TO_COPY: &str = "Failed to copy";
516+
517+
// Git references
518+
pub const GIT_REFS_HEADS: &str = "refs/heads/";
519+
520+
// Git command arguments
521+
pub const GIT_ARG_TRACK: &str = "--track";
522+
pub const GIT_ARG_NO_TRACK: &str = "--no-track";
523+
pub const GIT_ARG_NO_GUESS_REMOTE: &str = "--no-guess-remote";
524+
525+
// File permissions
526+
pub const FILE_PERMISSION_READONLY: u32 = 0o444;
527+
pub const FILE_PERMISSION_READWRITE: u32 = 0o644;
528+
529+
// Directory names
530+
pub const MOCK_GIT_DIR: &str = ".mockgit";
531+
532+
// Test-specific constants
533+
pub const TEST_AUTHOR_NAME: &str = "Test Author";
534+
pub const TEST_AUTHOR_EMAIL: &str = "[email protected]";
535+
pub const TEST_COMMIT_MESSAGE: &str = "Test commit";
536+
pub const TEST_README_FILE: &str = "README.md";
537+
pub const TEST_README_CONTENT: &str = "# Test";
538+
pub const TEST_CONFIG_CONTENT: &str = "[files]\ncopy = [\".env\"]";
539+
pub const TEST_ENV_CONTENT: &str = "TEST=1";
540+
pub const TEST_GITIGNORE_CONTENT: &str = ".env";
541+
542+
// Hook execution messages
543+
pub const HOOK_EXECUTING: &str = "Executing hook: ";
544+
pub const HOOK_OUTPUT: &str = "Hook output: ";
545+
546+
// Symlink warning icon
547+
pub const ICON_SYMLINK_WARNING: &str = "⚠️";
548+
549+
// Git file names
550+
pub const GIT_FILE_GITDIR: &str = "gitdir";
551+
pub const GIT_FILE_COMMONDIR: &str = "commondir";
552+
553+
// Special file names
554+
pub const FILE_DOT_ENV: &str = ".env";
555+
pub const FILE_DOT_ENV_EXAMPLE: &str = ".env.example";
556+
557+
// Array slice ranges
558+
pub const SLICE_START: usize = 0;
559+
pub const SLICE_END_ONE: usize = 1;
560+
561+
// Time units
562+
pub const SECONDS_IN_DAY: u64 = 86400;
563+
564+
// Default branch names array (for iteration)
565+
pub const DEFAULT_BRANCHES: &[&str] = &["main", "master"];
566+
567+
// Worktree list header format
568+
pub const WORKTREE_LIST_HEADER: &str = "worktree ";
569+
570+
// Git command error patterns
571+
pub const GIT_ERROR_INVALID_REF: &str = "is not a valid ref";
572+
pub const GIT_ERROR_NOT_VALID_REF: &str = "not a valid ref";
573+
574+
// Progress messages
575+
pub const PROGRESS_CREATING_WORKTREE: &str = "Creating worktree";
576+
pub const PROGRESS_COPYING_FILES: &str = "Copying files";
577+
pub const PROGRESS_RUNNING_HOOKS: &str = "Running hooks";
578+
579+
// Validation error prefixes
580+
pub const VALIDATION_ERROR_PREFIX: &str = "Validation failed: ";
581+
582+
// File operation prefixes
583+
pub const FILE_OP_COPYING: &str = "Copying: ";
584+
pub const FILE_OP_SKIPPING: &str = "Skipping: ";
585+
pub const FILE_OP_CREATED: &str = "Created: ";
586+
587+
// Git status prefixes
588+
pub const GIT_STATUS_BRANCH: &str = "branch ";
589+
pub const GIT_STATUS_HEAD: &str = "HEAD ";
590+
591+
// Menu display formats
592+
pub const MENU_FORMAT_WITH_ICON: &str = "{} {}";
593+
pub const MENU_FORMAT_SIMPLE: &str = "{}";
594+
595+
// Error detail separators
596+
pub const ERROR_DETAIL_SEPARATOR: &str = ": ";
597+
pub const ERROR_CONTEXT_SEPARATOR: &str = " - ";
598+
599+
// Path join separators
600+
pub const PATH_JOIN_SLASH: &str = "/";
601+
602+
// Git output parsing
603+
pub const GIT_OUTPUT_SPLIT_CHAR: char = '\n';
604+
pub const GIT_OUTPUT_TAB_CHAR: char = '\t';
605+
606+
// Number formatting
607+
pub const DECIMAL_FORMAT_ONE: &str = ".1";
608+
pub const DECIMAL_FORMAT_TWO: &str = ".2";
609+
610+
// Boolean string values
611+
pub const BOOL_TRUE_STR: &str = "true";
612+
pub const BOOL_FALSE_STR: &str = "false";
613+
614+
// Exit messages
615+
pub const EXIT_MSG_GOODBYE: &str = "Goodbye!";
616+
pub const EXIT_MSG_CANCELLED: &str = "Cancelled.";
617+
618+
// Prompt suffixes
619+
pub const PROMPT_SUFFIX_COLON: &str = ": ";
620+
pub const PROMPT_SUFFIX_QUESTION: &str = "? ";
621+
622+
// Display list bullet
623+
pub const DISPLAY_BULLET: &str = "• ";
624+
625+
// Git worktree states
626+
pub const WORKTREE_STATE_BARE: &str = "bare";
627+
pub const WORKTREE_STATE_DETACHED: &str = "detached";
628+
pub const WORKTREE_STATE_BRANCH: &str = "branch";
629+
630+
// Error recovery suggestions
631+
pub const SUGGEST_CHECK_PATH: &str = "Please check the path and try again.";
632+
pub const SUGGEST_CHECK_PERMISSIONS: &str = "Please check file permissions.";
633+
634+
// Confirmation prompts
635+
pub const CONFIRM_CONTINUE: &str = "Continue?";
636+
pub const CONFIRM_PROCEED: &str = "Proceed?";
637+
638+
// Status indicators
639+
pub const STATUS_OK: &str = "[OK]";
640+
pub const STATUS_FAILED: &str = "[FAILED]";
641+
pub const STATUS_SKIPPED: &str = "[SKIPPED]";
642+
643+
// Git refspec patterns
644+
pub const REFSPEC_HEADS: &str = "+refs/heads/*:refs/remotes/origin/*";
645+
646+
// Common file extensions
647+
pub const EXT_TOML: &str = ".toml";
648+
pub const EXT_JSON: &str = ".json";
649+
pub const EXT_YAML: &str = ".yaml";
650+
pub const EXT_YML: &str = ".yml";

0 commit comments

Comments
 (0)