- 
                Notifications
    You must be signed in to change notification settings 
- Fork 240
Refactor version module #3861
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: master
Are you sure you want to change the base?
Refactor version module #3861
Conversation
85c48ae    to
    bddfb28      
    Compare
  
    60b47b3    to
    154edfe      
    Compare
  
            
          
                crates/scarb-api/src/version.rs
              
                Outdated
          
        
      | fn extract_versions(version_output: &str) -> Result<HashMap<String, Version>, VersionError> { | ||
| // https://semver.org/#is-there-a-suggested-regular-expression-regex-to-check-a-semver-string | ||
| static VERSION_REGEX: LazyLock<Regex> = LazyLock::new(|| { | ||
| Regex::new(r"(?P<tool>\w+)[\s:]+(?P<ver>(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(?:\+[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*)?)").expect("this should be a valid regex") | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
We don't need to mach unlimited number of whitespace between tool name and it's version, just an optional : and whitespace
| Regex::new(r"(?P<tool>\w+)[\s:]+(?P<ver>(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(?:\+[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*)?)").expect("this should be a valid regex") | |
| Regex::new(r"(?P<tool>\w+):?\s(?P<ver>(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(?:\+[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*)?)").expect("this should be a valid regex") | 
Also I think if you are already matching for digits, you don't need extra matches for numbers
| Regex::new(r"(?P<tool>\w+)[\s:]+(?P<ver>(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(?:\+[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*)?)").expect("this should be a valid regex") | |
| Regex::new(r"(?P<tool>\w+)[\s:]+(?P<ver>(?:\d*)\.(?:\d*)\.(?:\d*)(?:-(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(?:\+[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*)?)").expect("this should be a valid regex") | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
The other groups could be simplified as well
| Regex::new(r"(?P<tool>\w+)[\s:]+(?P<ver>(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)\.(?:0|[1-9]\d*)(?:-(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*)(?:\.(?:0|[1-9]\d*|\d*[a-zA-Z-][0-9a-zA-Z-]*))*)?(?:\+[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*)?)").expect("this should be a valid regex") | |
| Regex::new(r"(?P<tool>\w+):?\s(?P<ver>(?:\d*)\.(?:\d*)\.(?:\d*)(?:-(?:[0-9a-zA-Z-]+)(?:\.(?:[0-9a-zA-Z-]*))*)?(?:\+[0-9a-zA-Z-]+(?:\.[0-9a-zA-Z-]+)*)?)").expect("this should be a valid regex") | 
| .collect() | ||
| } | ||
| #[cfg(test)] | ||
| mod tests { | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Shouldn't we add some more tests (even generate them through some LLM) for more versions combinations? As a sanity check for regexes.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Added
bddfb28    to
    b5c96e0      
    Compare
  
    b91a450    to
    d62c883      
    Compare
  
    commit-id:e0444c34 --- **Stack**: - #3861 - #3860 - #3859 ⬅⚠️ *Part of a stack created by [spr](https://github.com/ejoffe/spr). Do not merge manually using the UI - doing so may have unexpected results.*
d62c883    to
    c7226bc      
    Compare
  
    b5c96e0    to
    0e868b9      
    Compare
  
    commit-id:a2fe82e0 --- **Stack**: - #3861 - #3860 ⬅⚠️ *Part of a stack created by [spr](https://github.com/ejoffe/spr). Do not merge manually using the UI - doing so may have unexpected results.*
commit-id:f673da00
c7226bc    to
    836a1e8      
    Compare
  
    | let output = run_command(dir)?; | ||
| parse_output(&output) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nit: What would you say to name it like this, or simillar:
| let output = run_command(dir)?; | |
| parse_output(&output) | |
| let output = run_version_command(dir)?; | |
| parse_version_output(&output) | 
I know that it is a private function, but it would take away this few seconds to compute, that it is not some generic run_command function 😅
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me
| let output = run_command(dir)?; | ||
| parse_output(&output) | 
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good to me
No description provided.