Skip to content

Commit

Permalink
release 0.11.0
Browse files Browse the repository at this point in the history
  • Loading branch information
Zheoni committed Dec 26, 2023
1 parent cd41242 commit 17b9ab9
Show file tree
Hide file tree
Showing 8 changed files with 44 additions and 40 deletions.
8 changes: 8 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,14 @@

## Unreleased - ReleaseDate

## 0.11.0 - 2023-12-26
### Breaking changes
- Remove `PassResult::take_output`.
- `Metadata::map_filtered` now returns an iterator instead of a copy of the map.

### Fixed
- Implement `Clone` for `PassResult`.

## 0.10.0 - 2023-12-17
### Breaking changes
- Reworked intermediate references. Index is gone, now you reference the step or
Expand Down
2 changes: 1 addition & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "cooklang"
version = "0.10.0"
version = "0.11.0"
edition = "2021"
authors = ["Zheoni <zheoni@outlook.es>"]
description = "Cooklang parser with opt-in extensions"
Expand Down
7 changes: 2 additions & 5 deletions bindings/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,9 +17,7 @@ pub fn parse_recipe(input: String) -> CooklangRecipe {
let converter = Converter::empty();

let mut parser = PullParser::new(&input, extensions);
let parsed = parse_events(&mut parser, &input, extensions, &converter, None)
.take_output()
.unwrap();
let parsed = parse_events(&mut parser, &input, extensions, &converter, None).unwrap_output();

into_simple_recipe(&parsed)
}
Expand All @@ -40,8 +38,7 @@ pub fn parse_metadata(input: String) -> CooklangMetadata {
None,
)
.map(|c| c.metadata.map)
.take_output()
.unwrap();
.unwrap_output();

// converting IndexMap into HashMap
let _ = &(parsed).iter().for_each(|(key, value)| {
Expand Down
6 changes: 3 additions & 3 deletions src/aisle/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@ use parser::{AisleConfParser, Rule};

/// Represents a aisle configuration file
///
/// This type also implements [Serialize] and [Deserialize], so if you don't
/// This type also implements [`Serialize`] and [`Deserialize`], so if you don't
/// like the cooklang shopping list format you can swap it with any [`serde`]
/// format.
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize, Default)]
Expand All @@ -47,7 +47,7 @@ pub struct Category<'a> {
pub ingredients: Vec<Ingredient<'a>>,
}

/// An ingredient belonging to a [Category]
/// An ingredient belonging to a [`Category`]
#[derive(Debug, Clone, PartialEq, Eq, Serialize, Deserialize)]
pub struct Ingredient<'a> {
/// List of names of the ingredient
Expand Down Expand Up @@ -149,7 +149,7 @@ pub fn write(conf: &AisleConf, mut write: impl std::io::Write) -> std::io::Resul
Ok(())
}

/// Error generated by [parse].
/// Error generated by [`parse`].
#[derive(Debug, Error, PartialEq, Eq)]
pub enum AisleConfError {
#[error("Error parsing input: {message}")]
Expand Down
18 changes: 11 additions & 7 deletions src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,7 +332,7 @@ impl std::fmt::Display for SourceReport {
impl std::error::Error for SourceReport {}

/// Output from the different passes of the parsing process
#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct PassResult<T> {
output: Option<T>,
report: SourceReport,
Expand All @@ -348,7 +348,7 @@ impl<T> PassResult<T> {
self.output.is_some()
}

/// Check if the result has errors.
/// Get the report
pub fn report(&self) -> &SourceReport {
&self.report
}
Expand All @@ -366,6 +366,15 @@ impl<T> PassResult<T> {
self.output.as_ref()
}

/// Get the output only if it's valid
pub fn valid_output(&self) -> Option<&T> {
if self.is_valid() {
self.output()
} else {
None
}
}

/// Transform into a common Rust [`Result`]
///
/// If the result is valid, the [`Ok`] variant holds the ouput and a
Expand All @@ -384,11 +393,6 @@ impl<T> PassResult<T> {
self.report
}

/// Take the output, leaving `None` in the result
pub fn take_output(&mut self) -> Option<T> {
self.output.take()
}

/// Transform into the ouput discarding errors/warnings
pub fn into_output(self) -> Option<T> {
self.output
Expand Down
13 changes: 7 additions & 6 deletions src/metadata.rs
Original file line number Diff line number Diff line change
Expand Up @@ -172,12 +172,13 @@ impl Metadata {
Ok(())
}

/// Returns a copy of [`Self::map`] but with all *special* metadata values
/// removed
pub fn map_filtered(&self) -> IndexMap<String, String> {
let mut new_map = self.map.clone();
new_map.retain(|key, _| SpecialKey::from_str(key).is_err());
new_map
/// Iterates over [`Self::map`] but with all *special* metadata values
/// skipped
pub fn map_filtered(&self) -> impl Iterator<Item = (&str, &str)> {
self.map
.iter()
.filter(|(key, _)| SpecialKey::from_str(key).is_err())
.map(|(key, value)| (key.as_str(), value.as_str()))
}
}

Expand Down
28 changes: 11 additions & 17 deletions tests/general_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -79,7 +79,7 @@ fn step_number(src: &str) -> Vec<Vec<Option<u32>>> {
Extensions::all() ^ Extensions::MULTILINE_STEPS,
Default::default(),
);
let r = parser.parse(src).take_output().unwrap();
let r = parser.parse(src).unwrap_output();
let numbers: Vec<Vec<Option<u32>>> = r
.sections
.into_iter()
Expand Down Expand Up @@ -111,14 +111,14 @@ fn empty_not_empty() {

// should be the same with multiline and without
let parser = CooklangParser::new(Extensions::all(), Default::default());
let r = parser.parse(input).take_output().unwrap();
let r = parser.parse(input).unwrap_output();
assert!(r.sections.is_empty());

let parser = CooklangParser::new(
Extensions::all() ^ Extensions::MULTILINE_STEPS,
Default::default(),
);
let r = parser.parse(input).take_output().unwrap();
let r = parser.parse(input).unwrap_output();
assert!(r.sections.is_empty());
}

Expand All @@ -139,14 +139,14 @@ fn empty_steps() {

// should be the same with multiline and without
let parser = CooklangParser::new(Extensions::all(), Default::default());
let r = parser.parse(input).take_output().unwrap();
let r = parser.parse(input).unwrap_output();
assert!(r.sections[0].content.is_empty());

let parser = CooklangParser::new(
Extensions::all() ^ Extensions::MULTILINE_STEPS,
Default::default(),
);
let r = parser.parse(input).take_output().unwrap();
let r = parser.parse(input).unwrap_output();
assert!(r.sections[0].content.is_empty());
}

Expand All @@ -160,7 +160,7 @@ fn whitespace_line_block_separator() {

// should be the same with multiline and without
let parser = CooklangParser::new(Extensions::all(), Default::default());
let r = parser.parse(input).take_output().unwrap();
let r = parser.parse(input).unwrap_output();
assert_eq!(r.sections[0].content.len(), 2);
}

Expand All @@ -173,7 +173,7 @@ fn single_line_no_separator() {
= section
"#};
let parser = CooklangParser::new(Extensions::all(), Default::default());
let r = parser.parse(input).take_output().unwrap();
let r = parser.parse(input).unwrap_output();
assert_eq!(r.sections.len(), 2);
assert_eq!(r.sections[0].content.len(), 2);
assert_eq!(r.sections[1].content.len(), 0);
Expand All @@ -184,7 +184,7 @@ fn single_line_no_separator() {
fn multiple_temperatures() {
let input = "text 2ºC more text 150 F end text";
let parser = CooklangParser::new(Extensions::all(), Default::default());
let r = parser.parse(input).take_output().unwrap();
let r = parser.parse(input).unwrap_output();
assert_eq!(r.inline_quantities.len(), 2);
assert_eq!(r.inline_quantities[0].value, 2.0.into());
assert_eq!(r.inline_quantities[0].unit_text(), Some("ºC"));
Expand Down Expand Up @@ -220,7 +220,7 @@ fn no_steps_component_mode() {
= section
step
"#};
let r = cooklang::parse(input).take_output().unwrap();
let r = cooklang::parse(input).unwrap_output();
assert_eq!(r.sections.len(), 1);
assert_eq!(r.sections[0].name.as_deref(), Some("section"));
assert!(matches!(
Expand All @@ -233,19 +233,13 @@ fn no_steps_component_mode() {
fn text_steps_extension() {
let input = "> text";

let r = CooklangParser::extended()
.parse(input)
.take_output()
.unwrap();
let r = CooklangParser::extended().parse(input).unwrap_output();
assert!(matches!(
r.sections[0].content.as_slice(),
[Content::Text(_)]
));

let r = CooklangParser::canonical()
.parse(input)
.take_output()
.unwrap();
let r = CooklangParser::canonical().parse(input).unwrap_output();
assert!(matches!(
r.sections[0].content.as_slice(),
[Content::Step(_)]
Expand Down

0 comments on commit 17b9ab9

Please sign in to comment.