Skip to content

Commit

Permalink
feat!: migrate config to support KDL v2
Browse files Browse the repository at this point in the history
This change updates kdl-rs to the latest release with support for KDL V2
spec. The release notes are [here](https://github.com/kdl-org/kdl/releases/tag/2.0.0)

BREAKING CHANGE
  • Loading branch information
EdenEast committed Dec 29, 2024
1 parent 77b7394 commit 2a2bd9d
Show file tree
Hide file tree
Showing 8 changed files with 197 additions and 160 deletions.
229 changes: 132 additions & 97 deletions Cargo.lock

Large diffs are not rendered by default.

4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -29,8 +29,8 @@ gix = { version = "0.69.1", default-features = false, features = [ "max-performa
indexmap = "2.7.0"
itertools = "0.13.0"
jwalk = "0.8.1"
kdl = "4.6.0"
miette = { version = "5.10.0", features = ["fancy"] }
kdl = "6.1.0"
miette = { version = "7.4.0", features = ["fancy"] }
nucleo = "0.5.0"
ratatui = "0.29.0"
rayon = "1.10.0"
Expand Down
36 changes: 18 additions & 18 deletions config.kdl
Original file line number Diff line number Diff line change
@@ -1,52 +1,52 @@
// Configure the list of search paths used to search for valid workspaces.
// Tux uses these valid workspaces as options to attach to.
// paths {
/-paths {
// Workspace paths are paths to recursivly search to find valid workspaces.
// Tux will recursivly search the workspace paths until the max depth is
// reached. To override the default workspace value set optional
// `default=false`.
// `default=#false`.
//
// Default:
// - "~"
//
// workspace default=true {
// - "~/code"
// }
/-workspace default=#true {
- "~/code"
}

// Single paths are paths that are added to the list of valid workspace
// paths. This is useful if you want to add a path that would not be defined
// as a valid workspace.
//
// Default: (empty)
//
// single default=false {
// - "~/.config/nvim"
// }
// }
/-single default=#false {
- "~/.config/nvim"
}
}

// Search depth for workspace paths.
// Default: 5
//
// depth 3
/-depth 3

// Select the repositories remote default branch if multiple worktrees are found. If the default
// worktree cannot be found the fallback will be to select the correct one.
//
// Default: false
// Default: #false
//
// default_worktree true
/-default_worktree #true

// Workspace directory crawler will prune the paths containing any of these components.
// Options:
// - default: boolean = true # Append to default value if true.
// - default: boolean = #true # Append to default value if true.
//
// Default:
// exclude_path default=true {
// exclude_path default=#true {
// - ".direnv"
// - "node_modules"
// }
//
// exclude_path {
// - ".direnv"
// - "node_modules"
// }
/-exclude_path {
- ".direnv"
- "node_modules"
}
10 changes: 5 additions & 5 deletions doc/configuration.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -59,7 +59,7 @@ Default: `false`

[source,javascript]
----
default_worktree true
default_worktree #true
----

=== exclude_paths
Expand All @@ -76,7 +76,7 @@ Default: `{ - ".direnv" "node_modules" }`

[source,javascript]
----
exclude_paths default=false {
exclude_paths default=#false {
- ".direnv"
- "node_modules"
}
Expand All @@ -90,7 +90,7 @@ attach to.
==== paths.workspace

Workspace paths are paths to recursivly search to find valid workspaces. Tux will recursivly search the workspace paths
until the max depth is reached. To override the default workspace value set optional `default=false`
until the max depth is reached. To override the default workspace value set optional `default=#false`

Optional arguments:

Expand All @@ -103,7 +103,7 @@ Default: `{ - "~" }`
[source, javascript]
----
paths {
workspaces default=false {
workspaces default=#false {
- "~/code"
}
}
Expand All @@ -125,7 +125,7 @@ Default: `{}`
[source, javascript]
----
paths {
single default=false {
single default=#false {
- "~/.config/nvim"
}
}
Expand Down
10 changes: 5 additions & 5 deletions readme.adoc
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ Default: `false`

[source,javascript]
----
default_worktree true
default_worktree #true
----

=== exclude_paths
Expand All @@ -280,7 +280,7 @@ Default: `{ - ".direnv" "node_modules" }`

[source,javascript]
----
exclude_paths default=false {
exclude_paths default=#false {
- ".direnv"
- "node_modules"
}
Expand All @@ -294,7 +294,7 @@ attach to.
==== paths.workspace

Workspace paths are paths to recursivly search to find valid workspaces. Tux will recursivly search the workspace paths
until the max depth is reached. To override the default workspace value set optional `default=false`
until the max depth is reached. To override the default workspace value set optional `default=#false`

Optional arguments:

Expand All @@ -307,7 +307,7 @@ Default: `{ - "~" }`
[source, javascript]
----
paths {
workspaces default=false {
workspaces default=#false {
- "~/code"
}
}
Expand All @@ -329,7 +329,7 @@ Default: `{}`
[source, javascript]
----
paths {
single default=false {
single default=#false {
- "~/.config/nvim"
}
}
Expand Down
4 changes: 2 additions & 2 deletions src/config/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -67,13 +67,13 @@ impl Config {
let config_path = util::get_config(&["config.kdl"]);
if config_path.exists() {
config = Parser::new(Source::load(config_path.display().to_string())?)
.parse_with_default(Some(config))?;
.parse_with_config(Some(config))?;
}

let local_path = util::get_local(&["config.kdl"]);
if local_path.exists() {
config = Parser::new(Source::load(local_path.display().to_string())?)
.parse_with_default(Some(config))?;
.parse_with_config(Some(config))?;
}

Ok(config)
Expand Down
62 changes: 32 additions & 30 deletions src/config/parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,15 +10,15 @@ pub struct Parser {

fn type_from_value(value: &KdlValue) -> &'static str {
match value {
KdlValue::RawString(_) | KdlValue::String(_) => "string",
KdlValue::Null => "null",
KdlValue::String(_) => "string",
KdlValue::Integer(_) => "integer",
KdlValue::Float(_) => "float",
KdlValue::Bool(_) => "boolean",
KdlValue::Base10Float(_) => "float",
_ => "number",
KdlValue::Null => "null",
}
}

fn get_dash_values<'a>(doc: &'a KdlDocument, name: &'static str) -> Vec<&'a KdlEntry> {
fn get_dash_values<'a>(doc: &'a KdlDocument, name: &'static str) -> Vec<&'a KdlValue> {
doc.get(name)
.and_then(|n| n.children())
.map(|doc| doc.nodes())
Expand All @@ -35,11 +35,11 @@ impl Parser {
}

pub fn parse(self) -> Result<Config, ParseError> {
self.parse_with_default(None)
self.parse_with_config(None)
}

pub fn parse_with_default(self, default: Option<Config>) -> Result<Config, ParseError> {
self.inner_parse(default.unwrap_or_default())
pub fn parse_with_config(self, config: Option<Config>) -> Result<Config, ParseError> {
self.inner_parse(config.unwrap_or_default())
}

fn inner_parse(self, mut config: Config) -> Result<Config, ParseError> {
Expand Down Expand Up @@ -94,7 +94,7 @@ impl Parser {
return Err(ParseError::UnknownConfigurationOption(
option.to_owned(),
self.src.clone(),
*node.name().span(),
node.name().span(),
));
}
}
Expand All @@ -107,7 +107,7 @@ impl Parser {
node.entries()
.iter()
.next()
.ok_or(ParseError::MissingValue(self.src.clone(), *node.span()))
.ok_or(ParseError::MissingValue(self.src.clone(), node.span()))
}

// fn first_entry_as_string<'a>(&'a self, node: &'a KdlNode) -> Result<&'a str, ParseError> {
Expand All @@ -123,12 +123,16 @@ impl Parser {

fn first_entry_as_i64<'a>(&'a self, node: &'a KdlNode) -> Result<i64, ParseError> {
self.first_entry(node).and_then(|entry| {
entry.value().as_i64().ok_or(ParseError::TypeMismatch(
"number",
type_from_value(entry.value()),
self.src.clone(),
*entry.span(),
))
entry
.value()
.as_integer()
.map(|n| n as i64)
.ok_or(ParseError::TypeMismatch(
"integer",
type_from_value(entry.value()),
self.src.clone(),
entry.span(),
))
})
}

Expand All @@ -138,18 +142,18 @@ impl Parser {
"bool",
type_from_value(entry.value()),
self.src.clone(),
*entry.span(),
entry.span(),
))
})
}

fn get_default_optional(&self, node: &KdlNode) -> Result<bool, ParseError> {
match node.get("default") {
Some(value) => value.value().as_bool().ok_or(ParseError::TypeMismatch(
Some(value) => value.as_bool().ok_or(ParseError::TypeMismatch(
"boolean",
type_from_value(value.value()),
type_from_value(value),
self.src.clone(),
*value.span(),
node.span(),
)),
None => Ok(true),
}
Expand All @@ -162,15 +166,14 @@ impl Parser {
) -> Result<Vec<String>, ParseError> {
get_dash_values(doc, name)
.into_iter()
.map(|entry| {
entry
.value()
.map(|value| {
value
.as_string()
.ok_or(ParseError::TypeMismatch(
"string",
type_from_value(entry.value()),
type_from_value(value),
self.src.clone(),
*entry.span(),
doc.span(),
))
.map(|s| s.to_string())
})
Expand All @@ -184,15 +187,14 @@ impl Parser {
) -> Result<Vec<String>, ParseError> {
get_dash_values(doc, name)
.into_iter()
.map(|entry| {
entry
.value()
.map(|value| {
value
.as_string()
.ok_or(ParseError::TypeMismatch(
"string",
type_from_value(entry.value()),
type_from_value(value),
self.src.clone(),
*entry.span(),
doc.span(),
))
.map(|s| shellexpand::tilde(s).to_string())
})
Expand Down
2 changes: 1 addition & 1 deletion src/config/source.rs
Original file line number Diff line number Diff line change
Expand Up @@ -47,7 +47,7 @@ impl SourceCode for Source {
}
}

impl From<&Source> for NamedSource {
impl From<&Source> for NamedSource<String> {
fn from(source: &Source) -> Self {
let name = source.path.clone();
let input = source.raw.clone();
Expand Down

0 comments on commit 2a2bd9d

Please sign in to comment.