This commit is contained in:
abdelkadous 2026-06-03 17:51:54 +01:00 committed by GitHub
commit 35940e7f05
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
7 changed files with 1695 additions and 1366 deletions

View File

@ -1702,6 +1702,7 @@ nothing #null
} }
#[test] #[test]
#[allow(clippy::approx_constant)]
fn float_values() { fn float_values() {
#[derive(Deserialize, Debug, PartialEq)] #[derive(Deserialize, Debug, PartialEq)]
struct Config { struct Config {

View File

@ -4,7 +4,10 @@ use std::fmt::Display;
#[cfg(feature = "v1")] #[cfg(feature = "v1")]
use crate::KdlNodeFormat; use crate::KdlNodeFormat;
use crate::{FormatConfig, KdlError, KdlNode, KdlValue}; use crate::{
FormatConfig, KdlError, KdlNode, KdlValue,
v2_parser::{KdlParser, KdlVersion},
};
/// Represents a KDL /// Represents a KDL
/// [`Document`](https://github.com/kdl-org/kdl/blob/main/SPEC.md#document). /// [`Document`](https://github.com/kdl-org/kdl/blob/main/SPEC.md#document).
@ -370,14 +373,15 @@ impl KdlDocument {
/// Parses a KDL v2 string into a document. /// Parses a KDL v2 string into a document.
pub fn parse_v2(s: &str) -> Result<Self, KdlError> { pub fn parse_v2(s: &str) -> Result<Self, KdlError> {
crate::v2_parser::try_parse(crate::v2_parser::document, s) let parser = KdlParser::new(KdlVersion::V2);
parser.try_parse(KdlParser::document, s)
} }
/// Parses a KDL v1 string into a document. /// Parses a KDL v1 string into a document.
#[cfg(feature = "v1")] #[cfg(feature = "v1")]
pub fn parse_v1(s: &str) -> Result<Self, KdlError> { pub fn parse_v1(s: &str) -> Result<Self, KdlError> {
let ret: Result<kdlv1::KdlDocument, kdlv1::KdlError> = s.parse(); let parser = KdlParser::new(KdlVersion::V1);
ret.map(|x| x.into()).map_err(|e| e.into()) parser.try_parse(KdlParser::document, s)
} }
/// Takes a KDL v1 document string and returns the same document, but /// Takes a KDL v1 document string and returns the same document, but
@ -920,10 +924,11 @@ foo 1 bar=0xdeadbeef {
if let Some(ty) = entry.ty() { if let Some(ty) = entry.ty() {
check_span_for_ident(ty, source); check_span_for_ident(ty, source);
} }
if let Some(KdlEntryFormat { value_repr, .. }) = entry.format() { if let Some(KdlEntryFormat { value_repr, .. }) = entry.format()
if entry.name().is_none() && entry.ty().is_none() { && entry.name().is_none()
check_span(value_repr, entry.span(), source); && entry.ty().is_none()
} {
check_span(value_repr, entry.span(), source);
} }
} }
if let Some(children) = node.children() { if let Some(children) = node.children() {

View File

@ -2,7 +2,10 @@
use miette::SourceSpan; use miette::SourceSpan;
use std::{fmt::Display, str::FromStr}; use std::{fmt::Display, str::FromStr};
use crate::{KdlError, KdlIdentifier, KdlValue, v2_parser}; use crate::{
KdlError, KdlIdentifier, KdlValue,
v2_parser::{self, KdlParser, KdlVersion},
};
/// KDL Entries are the "arguments" to KDL nodes: either a (positional) /// KDL Entries are the "arguments" to KDL nodes: either a (positional)
/// [`Argument`](https://github.com/kdl-org/kdl/blob/main/SPEC.md#argument) or /// [`Argument`](https://github.com/kdl-org/kdl/blob/main/SPEC.md#argument) or
@ -202,13 +205,15 @@ impl KdlEntry {
/// to parse again as a KDL v1 entry. If both fail, only the v2 parse /// to parse again as a KDL v1 entry. If both fail, only the v2 parse
/// errors will be returned. /// errors will be returned.
pub fn parse(s: &str) -> Result<Self, KdlError> { pub fn parse(s: &str) -> Result<Self, KdlError> {
let parser_v2 = KdlParser::new(KdlVersion::V2);
#[cfg(not(feature = "v1-fallback"))] #[cfg(not(feature = "v1-fallback"))]
{ {
v2_parser::try_parse(v2_parser::padded_node_entry, s) parser_v2.try_parse(KdlParser::padded_node_entry, s)
} }
#[cfg(feature = "v1-fallback")] #[cfg(feature = "v1-fallback")]
{ {
v2_parser::try_parse(v2_parser::padded_node_entry, s) parser_v2
.try_parse(KdlParser::padded_node_entry, s)
.or_else(|e| KdlEntry::parse_v1(s).map_err(|_| e)) .or_else(|e| KdlEntry::parse_v1(s).map_err(|_| e))
} }
} }
@ -216,8 +221,8 @@ impl KdlEntry {
/// Parses a KDL v1 string into an entry. /// Parses a KDL v1 string into an entry.
#[cfg(feature = "v1")] #[cfg(feature = "v1")]
pub fn parse_v1(s: &str) -> Result<Self, KdlError> { pub fn parse_v1(s: &str) -> Result<Self, KdlError> {
let ret: Result<kdlv1::KdlEntry, kdlv1::KdlError> = s.parse(); let parser_v1 = KdlParser::new(KdlVersion::V1);
ret.map(|x| x.into()).map_err(|e| e.into()) parser_v1.try_parse(KdlParser::padded_node_entry, s)
} }
/// Makes sure this entry is in v2 format. /// Makes sure this entry is in v2 format.

View File

@ -2,7 +2,10 @@
use miette::SourceSpan; use miette::SourceSpan;
use std::{fmt::Display, str::FromStr}; use std::{fmt::Display, str::FromStr};
use crate::{KdlError, KdlValue, v2_parser}; use crate::{
KdlError, KdlValue,
v2_parser::{KdlParser, KdlVersion},
};
/// Represents a KDL /// Represents a KDL
/// [Identifier](https://github.com/kdl-org/kdl/blob/main/SPEC.md#identifier). /// [Identifier](https://github.com/kdl-org/kdl/blob/main/SPEC.md#identifier).
@ -95,13 +98,15 @@ impl KdlIdentifier {
/// to parse again as a KDL v1 entry. If both fail, only the v2 parse /// to parse again as a KDL v1 entry. If both fail, only the v2 parse
/// errors will be returned. /// errors will be returned.
pub fn parse(s: &str) -> Result<Self, KdlError> { pub fn parse(s: &str) -> Result<Self, KdlError> {
let parser_v2 = KdlParser::new(KdlVersion::V2);
#[cfg(not(feature = "v1-fallback"))] #[cfg(not(feature = "v1-fallback"))]
{ {
v2_parser::try_parse(v2_parser::identifier, s) parser_v2.try_parse(KdlParser::identifier, s)
} }
#[cfg(feature = "v1-fallback")] #[cfg(feature = "v1-fallback")]
{ {
v2_parser::try_parse(v2_parser::identifier, s) parser_v2
.try_parse(KdlParser::identifier, s)
.or_else(|e| KdlIdentifier::parse_v1(s).map_err(|_| e)) .or_else(|e| KdlIdentifier::parse_v1(s).map_err(|_| e))
} }
} }
@ -109,8 +114,8 @@ impl KdlIdentifier {
/// Parses a KDL v1 string into an entry. /// Parses a KDL v1 string into an entry.
#[cfg(feature = "v1")] #[cfg(feature = "v1")]
pub fn parse_v1(s: &str) -> Result<Self, KdlError> { pub fn parse_v1(s: &str) -> Result<Self, KdlError> {
let ret: Result<kdlv1::KdlIdentifier, kdlv1::KdlError> = s.parse(); let parser_v1 = KdlParser::new(KdlVersion::V1);
ret.map(|x| x.into()).map_err(|e| e.into()) parser_v1.try_parse(KdlParser::identifier, s)
} }
} }

View File

@ -11,7 +11,7 @@ use miette::SourceSpan;
use crate::{ use crate::{
FormatConfig, KdlDocument, KdlDocumentFormat, KdlEntry, KdlError, KdlIdentifier, KdlValue, FormatConfig, KdlDocument, KdlDocumentFormat, KdlEntry, KdlError, KdlIdentifier, KdlValue,
v2_parser, v2_parser::{self, KdlParser, KdlVersion},
}; };
/// Represents an individual KDL /// Represents an individual KDL
@ -336,13 +336,15 @@ impl KdlNode {
/// to parse again as a KDL v1 node. If both fail, only the v2 parse /// to parse again as a KDL v1 node. If both fail, only the v2 parse
/// errors will be returned. /// errors will be returned.
pub fn parse(s: &str) -> Result<Self, KdlError> { pub fn parse(s: &str) -> Result<Self, KdlError> {
let parser_v2 = KdlParser::new(KdlVersion::V2);
#[cfg(not(feature = "v1-fallback"))] #[cfg(not(feature = "v1-fallback"))]
{ {
v2_parser::try_parse(v2_parser::padded_node, s) KdlParser::try_parse(|input: &mut Input<'_>| parser_v2.padded_node(input), s)
} }
#[cfg(feature = "v1-fallback")] #[cfg(feature = "v1-fallback")]
{ {
v2_parser::try_parse(v2_parser::padded_node, s) parser_v2
.try_parse(KdlParser::padded_node, s)
.or_else(|e| KdlNode::parse_v1(s).map_err(|_| e)) .or_else(|e| KdlNode::parse_v1(s).map_err(|_| e))
} }
} }
@ -350,8 +352,8 @@ impl KdlNode {
/// Parses a KDL v1 string into a document. /// Parses a KDL v1 string into a document.
#[cfg(feature = "v1")] #[cfg(feature = "v1")]
pub fn parse_v1(s: &str) -> Result<Self, KdlError> { pub fn parse_v1(s: &str) -> Result<Self, KdlError> {
let ret: Result<kdlv1::KdlNode, kdlv1::KdlError> = s.parse(); let parser_v1 = KdlParser::new(KdlVersion::V1);
ret.map(|x| x.into()).map_err(|e| e.into()) parser_v1.try_parse(KdlParser::padded_node, s)
} }
/// Makes sure this node is in v2 format. /// Makes sure this node is in v2 format.
@ -813,7 +815,8 @@ impl FromStr for KdlNode {
type Err = KdlError; type Err = KdlError;
fn from_str(input: &str) -> Result<Self, Self::Err> { fn from_str(input: &str) -> Result<Self, Self::Err> {
v2_parser::try_parse(v2_parser::padded_node, input) // TEST: need test
KdlNode::parse(input)
} }
} }

View File

@ -1833,6 +1833,7 @@ mod tests {
} }
#[test] #[test]
#[allow(clippy::approx_constant)]
fn float_value() { fn float_value() {
#[derive(Serialize)] #[derive(Serialize)]
struct Config { struct Config {

File diff suppressed because it is too large Load Diff