-
Notifications
You must be signed in to change notification settings - Fork 221
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Use a common from_value
method
#472
Open
polarathene
wants to merge
8
commits into
rust-cli:main
Choose a base branch
from
polarathene:refactor/common-parser-transform
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
1f49b4f
refactor: `format/toml.rs`
polarathene c9575a7
refactor: Use a common `from_value` method
polarathene 6f63595
fix: Support TOML `Datetime` value
polarathene a4f8c86
refactor: Simplify `ParsedString` deserializer
polarathene fe9ad9a
chore: Have `ParsedValue` deserialize to `Nil` as fallback
polarathene e597f6a
fix: Support Ron values
polarathene d4f2f35
fix: Support Yaml values
polarathene ccf35f3
chore: `ParsedString` should use a generic `char` variant
polarathene File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,105 +1,14 @@ | ||
use std::error::Error; | ||
use std::fmt; | ||
use std::mem; | ||
|
||
use yaml_rust as yaml; | ||
|
||
use crate::format; | ||
use crate::map::Map; | ||
use crate::value::{Value, ValueKind}; | ||
use crate::value::Value; | ||
|
||
pub fn parse( | ||
uri: Option<&String>, | ||
text: &str, | ||
) -> Result<Map<String, Value>, Box<dyn Error + Send + Sync>> { | ||
// Parse a YAML object from file | ||
let mut docs = yaml::YamlLoader::load_from_str(text)?; | ||
let root = match docs.len() { | ||
0 => yaml::Yaml::Hash(yaml::yaml::Hash::new()), | ||
1 => mem::replace(&mut docs[0], yaml::Yaml::Null), | ||
n => { | ||
return Err(Box::new(MultipleDocumentsError(n))); | ||
} | ||
}; | ||
|
||
let value = from_yaml_value(uri, &root)?; | ||
// Parse a YAML input from the provided text | ||
let value = format::from_parsed_value(uri, serde_yaml::from_str(text)?); | ||
format::extract_root_table(uri, value) | ||
} | ||
|
||
fn from_yaml_value( | ||
uri: Option<&String>, | ||
value: &yaml::Yaml, | ||
) -> Result<Value, Box<dyn Error + Send + Sync>> { | ||
match *value { | ||
yaml::Yaml::String(ref value) => Ok(Value::new(uri, ValueKind::String(value.clone()))), | ||
yaml::Yaml::Real(ref value) => { | ||
// TODO: Figure out in what cases this can panic? | ||
value | ||
.parse::<f64>() | ||
.map_err(|_| { | ||
Box::new(FloatParsingError(value.to_string())) as Box<(dyn Error + Send + Sync)> | ||
}) | ||
.map(ValueKind::Float) | ||
.map(|f| Value::new(uri, f)) | ||
} | ||
yaml::Yaml::Integer(value) => Ok(Value::new(uri, ValueKind::I64(value))), | ||
yaml::Yaml::Boolean(value) => Ok(Value::new(uri, ValueKind::Boolean(value))), | ||
yaml::Yaml::Hash(ref table) => { | ||
let mut m = Map::new(); | ||
for (key, value) in table { | ||
match key { | ||
yaml::Yaml::String(k) => m.insert(k.to_owned(), from_yaml_value(uri, value)?), | ||
yaml::Yaml::Integer(k) => m.insert(k.to_string(), from_yaml_value(uri, value)?), | ||
_ => unreachable!(), | ||
}; | ||
} | ||
Ok(Value::new(uri, ValueKind::Table(m))) | ||
} | ||
yaml::Yaml::Array(ref array) => { | ||
let mut l = Vec::new(); | ||
|
||
for value in array { | ||
l.push(from_yaml_value(uri, value)?); | ||
} | ||
|
||
Ok(Value::new(uri, ValueKind::Array(l))) | ||
} | ||
|
||
// 1. Yaml NULL | ||
// 2. BadValue – It shouldn't be possible to hit BadValue as this only happens when | ||
// using the index trait badly or on a type error but we send back nil. | ||
// 3. Alias – No idea what to do with this and there is a note in the lib that its | ||
// not fully supported yet anyway | ||
_ => Ok(Value::new(uri, ValueKind::Nil)), | ||
} | ||
} | ||
|
||
#[derive(Debug, Copy, Clone)] | ||
struct MultipleDocumentsError(usize); | ||
|
||
impl fmt::Display for MultipleDocumentsError { | ||
fn fmt(&self, format: &mut fmt::Formatter) -> fmt::Result { | ||
write!(format, "Got {} YAML documents, expected 1", self.0) | ||
} | ||
} | ||
|
||
impl Error for MultipleDocumentsError { | ||
fn description(&self) -> &str { | ||
"More than one YAML document provided" | ||
} | ||
} | ||
|
||
#[derive(Debug, Clone)] | ||
struct FloatParsingError(String); | ||
|
||
impl fmt::Display for FloatParsingError { | ||
fn fmt(&self, format: &mut fmt::Formatter) -> fmt::Result { | ||
write!(format, "Parsing {} as floating point number failed", self.0) | ||
} | ||
} | ||
|
||
impl Error for FloatParsingError { | ||
fn description(&self) -> &str { | ||
"Floating point number parsing failed" | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Required for the
Nil
variant fallback (scoped to it's own commit) ifdeserialize_any
fails to match anything prior.The crate could be avoided if you wanted to implement the equivalent directly 🤷♂️