mirror of https://github.com/kdl-org/kdl-rs.git
Merge 5370c3dd05 into ce82d2ce3e
This commit is contained in:
commit
35940e7f05
|
|
@ -1702,6 +1702,7 @@ nothing #null
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[allow(clippy::approx_constant)]
|
||||
fn float_values() {
|
||||
#[derive(Deserialize, Debug, PartialEq)]
|
||||
struct Config {
|
||||
|
|
|
|||
|
|
@ -4,7 +4,10 @@ use std::fmt::Display;
|
|||
|
||||
#[cfg(feature = "v1")]
|
||||
use crate::KdlNodeFormat;
|
||||
use crate::{FormatConfig, KdlError, KdlNode, KdlValue};
|
||||
use crate::{
|
||||
FormatConfig, KdlError, KdlNode, KdlValue,
|
||||
v2_parser::{KdlParser, KdlVersion},
|
||||
};
|
||||
|
||||
/// Represents a KDL
|
||||
/// [`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.
|
||||
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.
|
||||
#[cfg(feature = "v1")]
|
||||
pub fn parse_v1(s: &str) -> Result<Self, KdlError> {
|
||||
let ret: Result<kdlv1::KdlDocument, kdlv1::KdlError> = s.parse();
|
||||
ret.map(|x| x.into()).map_err(|e| e.into())
|
||||
let parser = KdlParser::new(KdlVersion::V1);
|
||||
parser.try_parse(KdlParser::document, s)
|
||||
}
|
||||
|
||||
/// Takes a KDL v1 document string and returns the same document, but
|
||||
|
|
@ -920,12 +924,13 @@ foo 1 bar=0xdeadbeef {
|
|||
if let Some(ty) = entry.ty() {
|
||||
check_span_for_ident(ty, source);
|
||||
}
|
||||
if let Some(KdlEntryFormat { value_repr, .. }) = entry.format() {
|
||||
if entry.name().is_none() && entry.ty().is_none() {
|
||||
if let Some(KdlEntryFormat { value_repr, .. }) = entry.format()
|
||||
&& entry.name().is_none()
|
||||
&& entry.ty().is_none()
|
||||
{
|
||||
check_span(value_repr, entry.span(), source);
|
||||
}
|
||||
}
|
||||
}
|
||||
if let Some(children) = node.children() {
|
||||
check_spans_for_doc(children, source);
|
||||
}
|
||||
|
|
|
|||
15
src/entry.rs
15
src/entry.rs
|
|
@ -2,7 +2,10 @@
|
|||
use miette::SourceSpan;
|
||||
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)
|
||||
/// [`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
|
||||
/// errors will be returned.
|
||||
pub fn parse(s: &str) -> Result<Self, KdlError> {
|
||||
let parser_v2 = KdlParser::new(KdlVersion::V2);
|
||||
#[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")]
|
||||
{
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
|
@ -216,8 +221,8 @@ impl KdlEntry {
|
|||
/// Parses a KDL v1 string into an entry.
|
||||
#[cfg(feature = "v1")]
|
||||
pub fn parse_v1(s: &str) -> Result<Self, KdlError> {
|
||||
let ret: Result<kdlv1::KdlEntry, kdlv1::KdlError> = s.parse();
|
||||
ret.map(|x| x.into()).map_err(|e| e.into())
|
||||
let parser_v1 = KdlParser::new(KdlVersion::V1);
|
||||
parser_v1.try_parse(KdlParser::padded_node_entry, s)
|
||||
}
|
||||
|
||||
/// Makes sure this entry is in v2 format.
|
||||
|
|
|
|||
|
|
@ -2,7 +2,10 @@
|
|||
use miette::SourceSpan;
|
||||
use std::{fmt::Display, str::FromStr};
|
||||
|
||||
use crate::{KdlError, KdlValue, v2_parser};
|
||||
use crate::{
|
||||
KdlError, KdlValue,
|
||||
v2_parser::{KdlParser, KdlVersion},
|
||||
};
|
||||
|
||||
/// Represents a KDL
|
||||
/// [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
|
||||
/// errors will be returned.
|
||||
pub fn parse(s: &str) -> Result<Self, KdlError> {
|
||||
let parser_v2 = KdlParser::new(KdlVersion::V2);
|
||||
#[cfg(not(feature = "v1-fallback"))]
|
||||
{
|
||||
v2_parser::try_parse(v2_parser::identifier, s)
|
||||
parser_v2.try_parse(KdlParser::identifier, s)
|
||||
}
|
||||
#[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))
|
||||
}
|
||||
}
|
||||
|
|
@ -109,8 +114,8 @@ impl KdlIdentifier {
|
|||
/// Parses a KDL v1 string into an entry.
|
||||
#[cfg(feature = "v1")]
|
||||
pub fn parse_v1(s: &str) -> Result<Self, KdlError> {
|
||||
let ret: Result<kdlv1::KdlIdentifier, kdlv1::KdlError> = s.parse();
|
||||
ret.map(|x| x.into()).map_err(|e| e.into())
|
||||
let parser_v1 = KdlParser::new(KdlVersion::V1);
|
||||
parser_v1.try_parse(KdlParser::identifier, s)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
15
src/node.rs
15
src/node.rs
|
|
@ -11,7 +11,7 @@ use miette::SourceSpan;
|
|||
|
||||
use crate::{
|
||||
FormatConfig, KdlDocument, KdlDocumentFormat, KdlEntry, KdlError, KdlIdentifier, KdlValue,
|
||||
v2_parser,
|
||||
v2_parser::{self, KdlParser, KdlVersion},
|
||||
};
|
||||
|
||||
/// 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
|
||||
/// errors will be returned.
|
||||
pub fn parse(s: &str) -> Result<Self, KdlError> {
|
||||
let parser_v2 = KdlParser::new(KdlVersion::V2);
|
||||
#[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")]
|
||||
{
|
||||
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))
|
||||
}
|
||||
}
|
||||
|
|
@ -350,8 +352,8 @@ impl KdlNode {
|
|||
/// Parses a KDL v1 string into a document.
|
||||
#[cfg(feature = "v1")]
|
||||
pub fn parse_v1(s: &str) -> Result<Self, KdlError> {
|
||||
let ret: Result<kdlv1::KdlNode, kdlv1::KdlError> = s.parse();
|
||||
ret.map(|x| x.into()).map_err(|e| e.into())
|
||||
let parser_v1 = KdlParser::new(KdlVersion::V1);
|
||||
parser_v1.try_parse(KdlParser::padded_node, s)
|
||||
}
|
||||
|
||||
/// Makes sure this node is in v2 format.
|
||||
|
|
@ -813,7 +815,8 @@ impl FromStr for KdlNode {
|
|||
type Err = KdlError;
|
||||
|
||||
fn from_str(input: &str) -> Result<Self, Self::Err> {
|
||||
v2_parser::try_parse(v2_parser::padded_node, input)
|
||||
// TEST: need test
|
||||
KdlNode::parse(input)
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
|
|
@ -1833,6 +1833,7 @@ mod tests {
|
|||
}
|
||||
|
||||
#[test]
|
||||
#[allow(clippy::approx_constant)]
|
||||
fn float_value() {
|
||||
#[derive(Serialize)]
|
||||
struct Config {
|
||||
|
|
|
|||
2983
src/v2_parser.rs
2983
src/v2_parser.rs
File diff suppressed because it is too large
Load Diff
Loading…
Reference in New Issue