Skip to content

Commit

Permalink
[Enhancement] Add ToRon Trait (#272)
Browse files Browse the repository at this point in the history
* Added ::= src/traits/to_ron.rs:  creation

* Added ::= ToRon

* Update lib.rs

* Update comment_changes.rs

* Added ::= tests/to_ron.rs:  creation

* Create summary of recent changes

---------

Co-authored-by: GitHub Actions <41898282+github-actions[bot]@users.noreply.github.com>
  • Loading branch information
kevinmatthes and github-actions[bot] committed May 31, 2023
1 parent 74140ca commit cbe520f
Show file tree
Hide file tree
Showing 6 changed files with 90 additions and 7 deletions.
9 changes: 9 additions & 0 deletions changelog.d/20230531_025720_GitHub_Actions_to-ron-trait.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
Added
.....

- tests/to_ron.rs: creation

- ToRon

- src/traits/to_ron.rs: creation

8 changes: 2 additions & 6 deletions src/changelog/comment_changes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@
| |
\******************************************************************************/

use crate::{AppendAsLine, PatternWriter};
use crate::{AppendAsLine, PatternWriter, ToRon};
use git2::Repository;
use std::collections::HashMap;
use sysexits::{ExitCode, Result};
Expand Down Expand Up @@ -362,11 +362,7 @@ impl Logic {

fn report(&mut self) -> Result<()> {
let content = if self.cli.extension == "ron" {
ron::ser::to_string_pretty(
&crate::Fragment::new(&self.hyperlinks, &self.changes),
ron::ser::PrettyConfig::default().indentor(" ".to_string()),
)
.map_or(Err(ExitCode::DataErr), Ok)?
crate::Fragment::new(&self.hyperlinks, &self.changes).to_ron(2)?
} else {
self.generate()
};
Expand Down
4 changes: 3 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -312,7 +312,9 @@ pub use crate::{
Reader as PatternReader, Writer as PatternWriter,
},
running::Running,
traits::{AppendAsLine, ColourMessage, ConvertBuffer, Prefer, ToStderr},
traits::{
AppendAsLine, ColourMessage, ConvertBuffer, Prefer, ToRon, ToStderr,
},
version::Version,
};

Expand Down
2 changes: 2 additions & 0 deletions src/traits/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,12 +21,14 @@ mod append_as_line;
mod colour_message;
mod convert_buffer;
mod prefer;
mod to_ron;
mod to_stderr;

pub use append_as_line::AppendAsLine;
pub use colour_message::ColourMessage;
pub use convert_buffer::ConvertBuffer;
pub use prefer::Prefer;
pub use to_ron::ToRon;
pub use to_stderr::ToStderr;

/******************************************************************************/
42 changes: 42 additions & 0 deletions src/traits/to_ron.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,42 @@
/*********************** GNU General Public License 3.0 ***********************\
| |
| Copyright (C) 2023 Kevin Matthes |
| |
| This program is free software: you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by |
| the Free Software Foundation, either version 3 of the License, or |
| (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License |
| along with this program. If not, see <https://www.gnu.org/licenses/>. |
| |
\******************************************************************************/

use sysexits::Result;

/// Convert this instance into a RON string.
pub trait ToRon: serde::Serialize {
/// Convert an instance implementing [`serde::Serialize`] to valid RON.
///
/// # Errors
///
/// - [`sysexits::ExitCode::DataErr`]
fn to_ron(&self, indentation_width: usize) -> Result<String>;
}

impl<T: serde::Serialize> ToRon for T {
fn to_ron(&self, indentation_width: usize) -> Result<String> {
ron::ser::to_string_pretty(
self,
ron::ser::PrettyConfig::default().indentor(" ".repeat(indentation_width)),
)
.map_or(Err(sysexits::ExitCode::DataErr), Ok)
}
}

/******************************************************************************/
32 changes: 32 additions & 0 deletions tests/to_ron.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,32 @@
/*********************** GNU General Public License 3.0 ***********************\
| |
| Copyright (C) 2023 Kevin Matthes |
| |
| This program is free software: you can redistribute it and/or modify |
| it under the terms of the GNU General Public License as published by |
| the Free Software Foundation, either version 3 of the License, or |
| (at your option) any later version. |
| |
| This program is distributed in the hope that it will be useful, |
| but WITHOUT ANY WARRANTY; without even the implied warranty of |
| MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| GNU General Public License for more details. |
| |
| You should have received a copy of the GNU General Public License |
| along with this program. If not, see <https://www.gnu.org/licenses/>. |
| |
\******************************************************************************/

use aeruginous::ToRon;

#[test]
fn i32_number_struct() {
#[derive(serde::Serialize)]
struct Number {
n: i32,
}

assert_eq!(&Number { n: 42 }.to_ron(2).unwrap(), "(\n n: 42,\n)");
}

/******************************************************************************/

0 comments on commit cbe520f

Please sign in to comment.