nushell/crates/nu-protocol/src/config/completer.rs
Ian Manske 3b1d405b96
Remove the Value::Block case (#12582)
# Description
`Value` describes the types of first-class values that users and scripts
can create, manipulate, pass around, and store. However, `Block`s are
not first-class values in the language, so this PR removes it from
`Value`. This removes some unnecessary code, and this change should be
invisible to the user except for the change to `scope modules` described
below.

# User-Facing Changes
Breaking change: the output of `scope modules` was changed so that
`env_block` is now `has_env_block` which is a boolean value instead of a
`Block`.

# After Submitting
Update the language guide possibly.
2024-04-21 07:03:33 +02:00

56 lines
1.5 KiB
Rust

use std::str::FromStr;
use serde::{Deserialize, Serialize};
use crate::{record, Config, Span, Value};
use super::helper::ReconstructVal;
#[derive(Serialize, Deserialize, Clone, Copy, Debug, Default)]
pub enum CompletionAlgorithm {
#[default]
Prefix,
Fuzzy,
}
impl FromStr for CompletionAlgorithm {
type Err = &'static str;
fn from_str(s: &str) -> Result<Self, Self::Err> {
match s.to_ascii_lowercase().as_str() {
"prefix" => Ok(Self::Prefix),
"fuzzy" => Ok(Self::Fuzzy),
_ => Err("expected either 'prefix' or 'fuzzy'"),
}
}
}
impl ReconstructVal for CompletionAlgorithm {
fn reconstruct_value(&self, span: Span) -> Value {
let str = match self {
CompletionAlgorithm::Prefix => "prefix",
CompletionAlgorithm::Fuzzy => "fuzzy",
};
Value::string(str, span)
}
}
pub(super) fn reconstruct_external_completer(config: &Config, span: Span) -> Value {
if let Some(closure) = config.external_completer.as_ref() {
Value::closure(closure.clone(), span)
} else {
Value::nothing(span)
}
}
pub(super) fn reconstruct_external(config: &Config, span: Span) -> Value {
Value::record(
record! {
"max_results" => Value::int(config.max_external_completion_results, span),
"completer" => reconstruct_external_completer(config, span),
"enable" => Value::bool(config.enable_external_completion, span),
},
span,
)
}