mirror of
https://github.com/nushell/nushell.git
synced 2026-11-24 18:55:02 +08:00
## Description
This PR refactors the original SemVer implementation (previous commits
in this PR) from a first-class `Value::SemVer` type to a `CustomValue`
implementation. This change improves the CustomValue trait system while
maintaining all functionality through more idiomatic Nushell patterns.
### 1. CustomValue Trait Changes
- Modified `Type::Custom` to preserve and return the actual type name
instead of "custom"
- Updated `style_computer` to use custom type names for style lookup
- Custom values can now have type-specific coloring in tables
### 2. Two Custom Types Created
- **`SemverValue`**: Wraps `semver::Version` with helper methods for
bumping versions
- **`SemverRangeValue`**: Wraps `semver::VersionReq` for range matching
### 3. Removed Components
- **Parser component**: No more REPL syntax highlighting for semver
literals (e.g., `1.2.3` is now a string, not a semver)
- **Redundant commands**: `semver from-record`, `semver to-record`,
`semver is-valid`, `semver sort`, `semver match-req`
- **Value::SemVer, Type::SemVer, Expr::SemVer**: Completely removed from
codebase
## User-facing changes (Release notes)
Nushell now understands and allows you to work with Semantic Versioning.
All functionality works through idiomatic Nushell patterns:
### Converting to SemVer
```nushell
> '1.2.3' | into semver # Convert string to semver
{major: 1, minor: 2, patch: 3} | into semver # Convert record to semver
```
### Decomposing SemVer to Record
```nushell
> '1.2.3-alpha.1+build.2' | into semver | into record
╭───────────────────┬───────────────╮
│ major │ 1 │
│ minor │ 2 │
│ patch │ 3 │
│ pre │ alpha.1 │
│ build │ build.2 │
│ │ ╭───┬───────╮ │
│ pre_identifiers │ │ 0 │ alpha │ │
│ │ │ 1 │ 1 │ │
│ │ ╰───┴───────╯ │
│ │ ╭───┬───────╮ │
│ build_identifiers │ │ 0 │ build │ │
│ │ │ 1 │ 2 │ │
│ │ ╰───┴───────╯ │
╰───────────────────┴───────────────╯
```
### Building SemVer from Record
```nushell
> {major: 1, minor: 2, patch: 3, pre: "alpha.1"} | into semver
1.2.3-alpha.1
```
### Bumping SemVer
```nushell
> '1.2.3' | into semver | semver bump major
2.0.0
> '1.2.3' | into semver | semver bump minor
1.3.0
> '1.2.3' | into semver | semver bump patch
1.2.4
> '1.2.3' | into semver | semver bump alpha
1.2.3-alpha.0
> '1.2.3-alpha.1' | into semver | semver bump alpha
1.2.3-alpha.2
> '1.2.3-alpha' | into semver | semver bump release
1.2.3
```
or just from strings
```nushell
> '1.2.3' | semver bump major
2.0.0
> '1.2.3' | semver bump minor
1.3.0
```
Tab completions available for `major`, `minor`, `patch`, `alpha`,
`beta`, `rc`, `release`.
```nushell
> 📎 '1.2.3' | semver bump
alpha major patch release
beta minor rc
```
Completions also work with variables.
```nushell
> let v = '1.2.3' | into semver
> 📎 $v.<tab>
build minor pre
major patch
```
### Sorting SemVer Values
```nushell
> ['2.0.0', '1.0.0', '1.2.3'] | each { into semver } | sort
╭───┬───────╮
│ 0 │ 1.0.0 │
│ 1 │ 1.2.3 │
│ 2 │ 2.0.0 │
╰───┴───────╯
> ['2.0.0', '1.0.0', '1.2.3'] | each { into semver } | sort --reverse
╭───┬───────╮
│ 0 │ 2.0.0 │
│ 1 │ 1.2.3 │
│ 2 │ 1.0.0 │
╰───┴───────╯
```
### Checking Valid SemVer
```nushell
> try { '1.2.3' | into semver } | is-not-empty # Returns true if valid
true
> try { 'not-valid' | into semver } | is-not-empty
false
```
### Matching SemVer Against Requirements
```nushell
> '1.2.3' | into semver | $in in ('>=1.0.0' | into semver-range)
true
> '1.0.0' | into semver | $in in ('>=2.0.0' | into semver-range)
false
> '1.3.0-alpha' | into semver | $in in ('>=1.2.3' | into semver-range)
false
```
### Converting SemVer Range
```nushell
> '>=1.0.0' | into semver-range
>=1.0.0
> '^1.2.3' | into semver-range
^1.2.3
> '~1.2' | into semver-range
~1.2
```
### Describe Command
```nushell
> '1.2.3' | into semver | describe -d
╭───────────────┬───────────────────────────────────────────────────────────────────────╮
│ type │ custom │
│ detailed_type │ semver │
│ subtype │ semver │
│ rust_type │ &alloc::boxed::Box<dyn nu_protocol::value::custom_value::CustomValue> │
│ value │ 1.2.3 │
╰───────────────┴───────────────────────────────────────────────────────────────────────╯
```
### Table Coloring
**SemVer values are colored in tables:**
```nushell
> [[string, semver, semver-range]; ['1.2.3', ('1.2.3' | into semver), ('>=1.0.0' | into semver-range)]]
╭───┬────────┬────────┬──────────────╮
│ # │ string │ semver │ semver-range │
├───┼────────┼────────┼──────────────┤
│ 0 │ 1.2.3 │ 1.2.3 │ >=1.0.0 │
╰───┴────────┴────────┴──────────────╯
```
Both `semver` and `semver-range` values are displayed in cyan bold by
default.
### Configuration
```nushell
$env.config.color_config.semver = "cyan_bold"
$env.config.color_config.semver-range = "cyan_bold"
```
### Serialization (to/from commands)
**Works with all `to` commands:**
```nushell
> '1.2.3' | into semver | to json
"1.2.3"
> '1.2.3' | into semver | to yaml
'1.2.3'
> '1.2.3' | into semver | to toml
'"1.2.3"'
```
## Additional notes
closes #16436