Skip to content

Commit

Permalink
getting started
Browse files Browse the repository at this point in the history
  • Loading branch information
zkat committed Apr 2, 2024
1 parent 7e62eb8 commit c0f6020
Show file tree
Hide file tree
Showing 13 changed files with 344 additions and 70 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ span = []
miette = "5.7.0"
nom = "7.1.1"
thiserror = "1.0.40"
winnow = "0.5.40"

[dev-dependencies]
miette = { version = "5.7.0", features = ["fancy"] }
Expand Down
19 changes: 11 additions & 8 deletions src/document.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use miette::SourceSpan;
use std::{fmt::Display, str::FromStr};

use crate::{parser, IntoKdlQuery, KdlError, KdlNode, KdlQueryIterator, KdlValue, NodeKey};
use crate::{v1_parser, IntoKdlQuery, KdlDiagnostic, KdlNode, KdlQueryIterator, KdlValue, NodeKey};

/// Represents a KDL
/// [`Document`](https://github.com/kdl-org/kdl/blob/main/SPEC.md#document).
Expand Down Expand Up @@ -259,7 +259,10 @@ impl KdlDocument {
/// Any query selectors that try to select the toplevel `scope()` will
/// fail to match when using this method, since there's no [`KdlNode`] to
/// return in this case.
pub fn query_all(&self, query: impl IntoKdlQuery) -> Result<KdlQueryIterator<'_>, KdlError> {
pub fn query_all(
&self,
query: impl IntoKdlQuery,
) -> Result<KdlQueryIterator<'_>, KdlDiagnostic> {
let parsed = query.into_query()?;
Ok(KdlQueryIterator::new(None, Some(self), parsed))
}
Expand All @@ -272,7 +275,7 @@ impl KdlDocument {
/// Any query selectors that try to select the toplevel `scope()` will
/// fail to match when using this method, since there's no [`KdlNode`] to
/// return in this case.
pub fn query(&self, query: impl IntoKdlQuery) -> Result<Option<&KdlNode>, KdlError> {
pub fn query(&self, query: impl IntoKdlQuery) -> Result<Option<&KdlNode>, KdlDiagnostic> {
let mut iter = self.query_all(query)?;
Ok(iter.next())
}
Expand All @@ -290,7 +293,7 @@ impl KdlDocument {
&self,
query: impl IntoKdlQuery,
key: impl Into<NodeKey>,
) -> Result<Option<&KdlValue>, KdlError> {
) -> Result<Option<&KdlValue>, KdlDiagnostic> {
Ok(self.query(query)?.and_then(|node| node.get(key)))
}

Expand All @@ -308,7 +311,7 @@ impl KdlDocument {
&self,
query: impl IntoKdlQuery,
key: impl Into<NodeKey>,
) -> Result<impl Iterator<Item = &KdlValue>, KdlError> {
) -> Result<impl Iterator<Item = &KdlValue>, KdlDiagnostic> {
let key: NodeKey = key.into();
Ok(self
.query_all(query)?
Expand Down Expand Up @@ -371,11 +374,11 @@ impl IntoIterator for KdlDocument {
}

impl FromStr for KdlDocument {
type Err = KdlError;
type Err = KdlDiagnostic;

fn from_str(input: &str) -> Result<Self, Self::Err> {
let kdl_parser = parser::KdlParser::new(input);
kdl_parser.parse(parser::document(&kdl_parser))
let kdl_parser = v1_parser::KdlParser::new(input);
kdl_parser.parse(v1_parser::document(&kdl_parser))
}
}

Expand Down
8 changes: 4 additions & 4 deletions src/entry.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use miette::SourceSpan;
use std::{fmt::Display, str::FromStr};

use crate::{parser, KdlError, KdlIdentifier, KdlValue};
use crate::{v1_parser, KdlDiagnostic, KdlIdentifier, KdlValue};

/// KDL Entries are the "arguments" to KDL nodes: either a (positional)
/// [`Argument`](https://github.com/kdl-org/kdl/blob/main/SPEC.md#argument) or
Expand Down Expand Up @@ -232,11 +232,11 @@ where
}

impl FromStr for KdlEntry {
type Err = KdlError;
type Err = KdlDiagnostic;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let kdl_parser = parser::KdlParser::new(s);
kdl_parser.parse(parser::entry_with_trailing(&kdl_parser))
let kdl_parser = v1_parser::KdlParser::new(s);
kdl_parser.parse(v1_parser::entry_with_trailing(&kdl_parser))
}
}

Expand Down
12 changes: 11 additions & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,16 @@ use {
std::convert::{TryFrom, TryInto},
};

#[derive(Debug, Diagnostic, Clone, Eq, PartialEq, Error)]
#[error("Failed to parse KDL document.")]
pub struct KdlParseFailure {
#[source_code]
pub input: String,

#[related]
pub errors: Vec<KdlDiagnostic>,
}

/// An error that occurs when parsing a KDL document.
///
/// This error implements [`miette::Diagnostic`] and can be used to display
Expand All @@ -36,7 +46,7 @@ use {
/// ```
#[derive(Debug, Diagnostic, Clone, Eq, PartialEq, Error)]
#[error("{kind}")]
pub struct KdlError {
pub struct KdlDiagnostic {
/// Source string for the KDL document that failed to parse.
#[source_code]
pub input: String,
Expand Down
8 changes: 4 additions & 4 deletions src/fmt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ pub(crate) fn fmt_leading(leading: &mut String, indent: usize, no_comments: bool
let mut result = String::new();
if !no_comments {
let input = leading.trim();
let kdl_parser = crate::parser::KdlParser { full_input: input };
let kdl_parser = crate::v1_parser::KdlParser { full_input: input };
let comments = kdl_parser
.parse(crate::parser::leading_comments(&kdl_parser))
.parse(crate::v1_parser::leading_comments(&kdl_parser))
.expect("invalid leading text");
for line in comments {
let trimmed = line.trim();
Expand All @@ -30,9 +30,9 @@ pub(crate) fn fmt_trailing(decor: &mut String, no_comments: bool) {
let mut result = String::new();
if !no_comments {
let input = &*decor;
let kdl_parser = crate::parser::KdlParser { full_input: input };
let kdl_parser = crate::v1_parser::KdlParser { full_input: input };
let comments = kdl_parser
.parse(crate::parser::trailing_comments(&kdl_parser))
.parse(crate::v1_parser::trailing_comments(&kdl_parser))
.expect("invalid trailing text");
for comment in comments {
result.push_str(comment);
Expand Down
8 changes: 4 additions & 4 deletions src/identifier.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
use miette::SourceSpan;
use std::{fmt::Display, str::FromStr};

use crate::{parser, KdlError};
use crate::{v1_parser, KdlDiagnostic};

/// Represents a KDL
/// [Identifier](https://github.com/kdl-org/kdl/blob/main/SPEC.md#identifier).
Expand Down Expand Up @@ -201,11 +201,11 @@ impl From<KdlIdentifier> for String {
}

impl FromStr for KdlIdentifier {
type Err = KdlError;
type Err = KdlDiagnostic;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let kdl_parser = crate::parser::KdlParser::new(s);
kdl_parser.parse(parser::identifier(&kdl_parser))
let kdl_parser = crate::v1_parser::KdlParser::new(s);
kdl_parser.parse(v1_parser::identifier(&kdl_parser))
}
}

Expand Down
6 changes: 4 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@

#![deny(missing_debug_implementations, nonstandard_style)]
#![warn(missing_docs, unreachable_pub, rust_2018_idioms, unreachable_pub)]
#![cfg_attr(test, deny(warnings))]
// #![cfg_attr(test, deny(warnings))]
#![doc(html_favicon_url = "https://kdl.dev/favicon.ico")]
#![doc(html_logo_url = "https://kdl.dev/logo.svg")]

Expand All @@ -178,7 +178,9 @@ mod fmt;
mod identifier;
mod node;
mod nom_compat;
mod parser;
mod query;
mod query_parser;
mod v1_parser;
mod value;

mod v2_parser;
19 changes: 11 additions & 8 deletions src/node.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use std::{
use miette::SourceSpan;

use crate::{
parser, IntoKdlQuery, KdlDocument, KdlEntry, KdlError, KdlIdentifier, KdlQueryIterator,
v1_parser, IntoKdlQuery, KdlDiagnostic, KdlDocument, KdlEntry, KdlIdentifier, KdlQueryIterator,
KdlValue,
};

Expand Down Expand Up @@ -420,14 +420,17 @@ impl KdlNode {

/// Queries this Node according to the KQL query language,
/// returning an iterator over all matching nodes.
pub fn query_all(&self, query: impl IntoKdlQuery) -> Result<KdlQueryIterator<'_>, KdlError> {
pub fn query_all(
&self,
query: impl IntoKdlQuery,
) -> Result<KdlQueryIterator<'_>, KdlDiagnostic> {
let q = query.into_query()?;
Ok(KdlQueryIterator::new(Some(self), None, q))
}

/// Queries this Node according to the KQL query language,
/// returning the first match, if any.
pub fn query(&self, query: impl IntoKdlQuery) -> Result<Option<&KdlNode>, KdlError> {
pub fn query(&self, query: impl IntoKdlQuery) -> Result<Option<&KdlNode>, KdlDiagnostic> {
Ok(self.query_all(query)?.next())
}

Expand All @@ -438,7 +441,7 @@ impl KdlNode {
&self,
query: impl IntoKdlQuery,
key: impl Into<NodeKey>,
) -> Result<Option<&KdlValue>, KdlError> {
) -> Result<Option<&KdlValue>, KdlDiagnostic> {
Ok(self.query(query)?.and_then(|node| node.get(key)))
}

Expand All @@ -450,7 +453,7 @@ impl KdlNode {
&self,
query: impl IntoKdlQuery,
key: impl Into<NodeKey>,
) -> Result<impl Iterator<Item = &KdlValue>, KdlError> {
) -> Result<impl Iterator<Item = &KdlValue>, KdlDiagnostic> {
let key: NodeKey = key.into();
Ok(self
.query_all(query)?
Expand Down Expand Up @@ -517,11 +520,11 @@ impl IndexMut<&str> for KdlNode {
}

impl FromStr for KdlNode {
type Err = KdlError;
type Err = KdlDiagnostic;

fn from_str(input: &str) -> Result<Self, Self::Err> {
let kdl_parser = crate::parser::KdlParser::new(input);
kdl_parser.parse(parser::node(&kdl_parser))
let kdl_parser = crate::v1_parser::KdlParser::new(input);
kdl_parser.parse(v1_parser::node(&kdl_parser))
}
}

Expand Down
14 changes: 7 additions & 7 deletions src/query.rs
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
use std::{collections::VecDeque, str::FromStr, sync::Arc};

use crate::{query_parser::KdlQueryParser, KdlDocument, KdlError, KdlNode, KdlValue};
use crate::{query_parser::KdlQueryParser, KdlDiagnostic, KdlDocument, KdlNode, KdlValue};

/// A parsed KQL query. For details on the syntax, see the [KQL
/// spec](https://github.com/kdl-org/kdl/blob/main/QUERY-SPEC.md).
#[derive(Debug, Clone, PartialEq)]
pub struct KdlQuery(pub(crate) Vec<KdlQuerySelector>);

impl FromStr for KdlQuery {
type Err = KdlError;
type Err = KdlDiagnostic;

fn from_str(s: &str) -> Result<Self, Self::Err> {
let parser = KdlQueryParser::new(s);
Expand All @@ -26,29 +26,29 @@ impl<'a> IntoKdlQuery for &'a String {}

#[doc(hidden)]
pub trait IntoQuerySealed {
fn into_query(self) -> Result<KdlQuery, KdlError>;
fn into_query(self) -> Result<KdlQuery, KdlDiagnostic>;
}

impl IntoQuerySealed for KdlQuery {
fn into_query(self) -> Result<KdlQuery, KdlError> {
fn into_query(self) -> Result<KdlQuery, KdlDiagnostic> {
Ok(self)
}
}

impl IntoQuerySealed for &str {
fn into_query(self) -> Result<KdlQuery, KdlError> {
fn into_query(self) -> Result<KdlQuery, KdlDiagnostic> {
self.parse()
}
}

impl IntoQuerySealed for String {
fn into_query(self) -> Result<KdlQuery, KdlError> {
fn into_query(self) -> Result<KdlQuery, KdlDiagnostic> {
self.parse()
}
}

impl IntoQuerySealed for &String {
fn into_query(self) -> Result<KdlQuery, KdlError> {
fn into_query(self) -> Result<KdlQuery, KdlDiagnostic> {
self.parse()
}
}
Expand Down
Loading

0 comments on commit c0f6020

Please sign in to comment.