import { logger } from '../../logger' ; import { regEx } from '../../util/regex' ; import { api as npm } from '../npm' ; import type { NewValueConfig , VersioningApi } from '../types' ; export const id = 'cargo' ; export const displayName = 'Cargo' ; export const urls = [ 'https://doc.rust-lang.org/cargo/reference/specifying-dependencies.html' , ] ; export const supportsRanges = true ; export const supportedRangeStrategies = [ 'bump' , 'extend' , 'pin' , 'replace' ] ; const isVersion = ( input ) : boolean => npm . isVersion ( input ) ; function convertToCaret ( item ) { if ( isVersion ( item ) ) { return '^' + item . trim ( ) ; } return item . trim ( ) ; } function cargo2npm ( input ) { let versions = input . split ( ',' ) ; versions = versions . map ( convertToCaret ) ; return versions . join ( ' ' ) ; } function notEmpty ( s ) { return s !== '' ; } function npm2cargo ( input ) { if ( ! input ) { return input ; } const res = input . split ( regEx ( /\s+,?\s*|\s*,?\s+/ ) ) . map ( ( str ) => str . trim ( ) ) . filter ( notEmpty ) ; const operators = [ '^' , '~' , '=' , '>' , '<' , '<=' , '>=' ] ; for ( let i = 0 ; i < res . length - 1 ; i += 1 ) { if ( operators . includes ( res [ i ] ) ) { const newValue = res [ i ] + ' ' + res [ i + 1 ] ; res . splice ( i , 2 , newValue ) ; } } return res . join ( ', ' ) ; } const isLessThanRange = ( version , range ) : boolean => npm . isLessThanRange ( version , cargo2npm ( range ) ) ; export const isValid = ( input ) : boolean => npm . isValid ( cargo2npm ( input ) ) ; const matches = ( version , range ) : boolean => npm . matches ( version , cargo2npm ( range ) ) ; const getSatisfyingVersion = ( versions , range ) : string => npm . getSatisfyingVersion ( versions , cargo2npm ( range ) ) ; const minSatisfyingVersion = ( versions , range ) : string => npm . minSatisfyingVersion ( versions , cargo2npm ( range ) ) ; const isSingleVersion = ( constraint ) : boolean => constraint . trim ( ) . startsWith ( '=' ) && isVersion ( constraint . trim ( ) . substring ( 1 ) . trim ( ) ) ; function getNewValue ( { currentValue , rangeStrategy , currentVersion , newVersion , } ) { if ( ! currentValue || currentValue === '*' ) { return currentValue ; } if ( rangeStrategy === 'pin' || isSingleVersion ( currentValue ) ) { let res = '=' ; if ( currentValue . startsWith ( '= ' ) ) { res += ' ' ; } res += newVersion ; return res ; } const newSemver = npm . getNewValue ( { currentValue : cargo2npm ( currentValue ) , rangeStrategy , currentVersion , newVersion , } ) ; let newCargo = npm2cargo ( newSemver ) ; if ( ! newCargo ) { logger . info ( { currentValue , newSemver } , 'Could not get cargo version from semver' ) ; return currentValue ; } if ( newCargo . startsWith ( '^' ) && ! currentValue . startsWith ( '^' ) ) { newCargo = newCargo . substring ( 1 ) ; } return newCargo ; } export const api = { ... npm , getNewValue , isLessThanRange , isSingleVersion , isValid , matches , getSatisfyingVersion , minSatisfyingVersion , } ; export default api ;