mirror of
https://github.com/nushell/nushell.git
synced 2024-11-22 16:33:37 +01:00
2a3805c164
# Description Uses "normal" module `std/<submodule>/mod.nu` instead of renaming the files (as requested in #13842). # User-Facing Changes No user-facing changes other than in `view files` results. Imports remain the same after this PR. # Tests + Formatting - 🟢 `toolkit fmt` - 🟢 `toolkit clippy` - 🟢 `toolkit test` - 🟢 `toolkit test stdlib` Also manually confirmed that it does not interfere with nupm, since we did have a conflict at one point (and it's not possible to test here). # Performance Tests ## Linux ### Nushell Startup - No config ```nu bench --pretty -n 200 { <path_to>/nu -c "exit" } ``` | Release | Startup Time | | --- | --- | | 0.98.0 | 22ms 730µs 768ns +/- 1ms 515µs 942ns | This commit | 9ms 312µs 68ns +/- 709µs 378ns | Yesterday's nightly | 9ms 230µs 953ns +/- 9ms 67µs 689ns ### Nushell Startup - Load full standard library Measures relative impact of a full `use std *`, which isn't recommended, but worth tracking. ```nu bench --pretty -n 200 { <path_to>/nu -c "use std *; exit" } ``` | Release | Startup Time | | --- | --- | | 0.98.0 | 23ms 10µs 636ns +/- 1ms 277µs 854ns | This commit | 26ms 922µs 769ns +/- 562µs 538ns | Yesterday's nightly | 28ms 133µs 95ns +/- 761µs 943ns | `deprecated_dirs` removal PR * | 23ms 610µs 333ns +/- 369µs 436ns \* Current increase is partially due to double-loading `dirs` with removal warning in older version. # After Submitting Still TODO - Update standard library doc
219 lines
7.9 KiB
Plaintext
219 lines
7.9 KiB
Plaintext
# Utility functions to read, change and create XML data in format supported
|
|
# by `to xml` and `from xml` commands
|
|
|
|
# Get all xml entries matching simple xpath-inspired query
|
|
export def xaccess [
|
|
path: list # List of steps. Each step can be a
|
|
# 1. String with tag name. Finds all children with specified name. Equivalent to `child::A` in xpath
|
|
# 2. `*` string. Get all children without any filter. Equivalent to `descendant` in xpath
|
|
# 3. Int. Select n-th among nodes selected by previous path. Equivalent to `(...)[1]` in xpath, but is indexed from 0.
|
|
# 4. Closure. Predicate accepting entry. Selects all entries among nodes selected by previous path for which predicate returns true.
|
|
] {
|
|
let input = $in
|
|
if ($path | is-empty) {
|
|
let path_span = (metadata $path).span
|
|
error make {
|
|
msg: 'Empty path provided'
|
|
label: {
|
|
text: 'Use a non-empty list of path steps'
|
|
span: $path_span
|
|
}
|
|
}
|
|
}
|
|
# In xpath first element in path is applied to root element
|
|
# this way it is possible to apply first step to root element
|
|
# of nu xml without unrolling one step of loop
|
|
mut values: any = ()
|
|
$values = {content: [ { content: $input } ] }
|
|
for $step in ($path) {
|
|
match ($step | describe) {
|
|
'string' => {
|
|
if $step == '*' {
|
|
$values = ($values.content | flatten)
|
|
} else {
|
|
$values = ($values.content | flatten | where tag == $step)
|
|
}
|
|
},
|
|
'int' => {
|
|
$values = [ ($values | get $step) ]
|
|
},
|
|
'closure' => {
|
|
$values = ($values | where {|x| do $step $x})
|
|
},
|
|
$type => {
|
|
let step_span = (metadata $step).span
|
|
error make {
|
|
msg: $'Incorrect path step type ($type)'
|
|
label: {
|
|
text: 'Use a string or int as a step'
|
|
span: $step_span
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
if ($values | is-empty) {
|
|
return []
|
|
}
|
|
}
|
|
$values
|
|
}
|
|
|
|
def xupdate-string-step [ step: string rest: list updater: closure ] {
|
|
let input = $in
|
|
|
|
# Get a list of elements to be updated and their indices
|
|
let to_update = ($input.content | enumerate | filter {|it|
|
|
let item = $it.item
|
|
$step == '*' or $item.tag == $step
|
|
})
|
|
|
|
if ($to_update | is-empty) {
|
|
return $input
|
|
}
|
|
|
|
let new_values = ($to_update.item | xupdate-internal $rest $updater)
|
|
|
|
mut reenumerated_new_values: any = ($to_update.index | zip $new_values | each {|x| {index: $x.0 item: $x.1}})
|
|
|
|
mut new_content = []
|
|
for it in ($input.content | enumerate) {
|
|
let item = $it.item
|
|
let idx = $it.index
|
|
|
|
let next = (if (not ($reenumerated_new_values | is-empty)) and $idx == $reenumerated_new_values.0.index {
|
|
let tmp = $reenumerated_new_values.0
|
|
$reenumerated_new_values = ($reenumerated_new_values | skip 1)
|
|
$tmp.item
|
|
} else {
|
|
$item
|
|
})
|
|
|
|
$new_content = ($new_content | append $next)
|
|
}
|
|
|
|
{tag: $input.tag attributes: $input.attributes content: $new_content}
|
|
}
|
|
|
|
def xupdate-int-step [ step: int rest: list updater: closure ] {
|
|
$in | enumerate | each {|it|
|
|
let item = $it.item
|
|
let idx = $it.index
|
|
|
|
if $idx == $step {
|
|
[ $item ] | xupdate-internal $rest $updater | get 0
|
|
} else {
|
|
$item
|
|
}
|
|
}
|
|
}
|
|
|
|
def xupdate-closure-step [ step: closure rest: list updater: closure ] {
|
|
$in | each {|it|
|
|
if (do $step $it) {
|
|
[ $it ] | xupdate-internal $rest $updater | get 0
|
|
} else {
|
|
$it
|
|
}
|
|
}
|
|
}
|
|
|
|
def xupdate-internal [ path: list updater: closure ] {
|
|
let input = $in
|
|
|
|
if ($path | is-empty) {
|
|
$input | each $updater
|
|
} else {
|
|
let step = $path.0
|
|
let rest = ($path | skip 1)
|
|
|
|
match ($step | describe) {
|
|
'string' => {
|
|
$input | each {|x| $x | xupdate-string-step $step $rest $updater}
|
|
},
|
|
'int' => {
|
|
$input | xupdate-int-step $step $rest $updater
|
|
},
|
|
'closure' => {
|
|
$input | xupdate-closure-step $step $rest $updater
|
|
},
|
|
$type => {
|
|
let step_span = (metadata $step).span
|
|
error make {
|
|
msg: $'Incorrect path step type ($type)'
|
|
label: {
|
|
text: 'Use a string or int as a step'
|
|
span: $step_span
|
|
}
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
}
|
|
|
|
# Update xml data entries matching simple xpath-inspired query
|
|
export def xupdate [
|
|
path: list # List of steps. Each step can be a
|
|
# 1. String with tag name. Finds all children with specified name. Equivalent to `child::A` in xpath
|
|
# 2. `*` string. Get all children without any filter. Equivalent to `descendant` in xpath
|
|
# 3. Int. Select n-th among nodes selected by previous path. Equivalent to `(...)[1]` in xpath, but is indexed from 0.
|
|
# 4. Closure. Predicate accepting entry. Selects all entries among nodes selected by previous path for which predicate returns true.
|
|
updater: closure # A closure used to transform entries matching path.
|
|
] {
|
|
{tag:? attributes:? content: [$in]} | xupdate-internal $path $updater | get content.0
|
|
}
|
|
|
|
# Get type of an xml entry
|
|
#
|
|
# Possible types are 'tag', 'text', 'pi' and 'comment'
|
|
export def xtype [] {
|
|
let input = $in
|
|
if (($input | describe) == 'string' or
|
|
($input.tag? == null and $input.attributes? == null and ($input.content? | describe) == 'string')) {
|
|
'text'
|
|
} else if $input.tag? == '!' {
|
|
'comment'
|
|
} else if $input.tag? != null and ($input.tag? | str starts-with '?') {
|
|
'pi'
|
|
} else if $input.tag? != null {
|
|
'tag'
|
|
} else {
|
|
error make {msg: 'Not an xml emtry. Check valid types of xml entries via `help to xml`'}
|
|
}
|
|
}
|
|
|
|
# Insert new entry to elements matching simple xpath-inspired query
|
|
export def xinsert [
|
|
path: list # List of steps. Each step can be a
|
|
# 1. String with tag name. Finds all children with specified name. Equivalent to `child::A` in xpath
|
|
# 2. `*` string. Get all children without any filter. Equivalent to `descendant` in xpath
|
|
# 3. Int. Select n-th among nodes selected by previous path. Equivalent to `(...)[1]` in xpath, but is indexed from 0.
|
|
# 4. Closure. Predicate accepting entry. Selects all entries among nodes selected by previous path for which predicate returns true.
|
|
new_entry: record # A new entry to insert into `content` field of record at specified position
|
|
position?: int # Position to insert `new_entry` into. If specified inserts entry at given position (or end if
|
|
# position is greater than number of elements) in content of all entries of input matched by
|
|
# path. If not specified inserts at the end.
|
|
] {
|
|
$in | xupdate $path {|entry|
|
|
match ($entry | xtype) {
|
|
'tag' => {
|
|
let new_content = if $position == null {
|
|
$entry.content | append $new_entry
|
|
} else {
|
|
let position = if $position > ($entry.content | length) {
|
|
$entry.content | length
|
|
} else {
|
|
$position
|
|
}
|
|
$entry.content | insert $position $new_entry
|
|
}
|
|
|
|
|
|
{tag: $entry.tag attributes: $entry.attributes content: $new_content}
|
|
},
|
|
_ => (error make {msg: 'Can insert entry only into content of a tag node'})
|
|
}
|
|
}
|
|
}
|