Skip to content

Commit

Permalink
ion_sexp! returns SExp instead of Element
Browse files Browse the repository at this point in the history
  • Loading branch information
Zack Slayton committed Mar 14, 2023
1 parent 84de28b commit 09bbc67
Show file tree
Hide file tree
Showing 4 changed files with 20 additions and 31 deletions.
13 changes: 2 additions & 11 deletions src/text/text_formatter.rs
Original file line number Diff line number Diff line change
Expand Up @@ -449,8 +449,7 @@ impl<'a, W: std::fmt::Write> IonValueFormatter<'a, W> {
#[cfg(test)]
mod formatter_test {
use crate::text::text_formatter::IonValueFormatter;
use crate::value::owned::SExp;
use crate::{ion_list, ion_struct, Integer, IonResult, IonType, Timestamp};
use crate::{ion_list, ion_sexp, ion_struct, Integer, IonResult, IonType, Timestamp};
use num_bigint::BigInt;

fn formatter<F>(mut f: F, expected: &str)
Expand Down Expand Up @@ -556,15 +555,7 @@ mod formatter_test {
#[test]
fn test_format_sexp() -> IonResult<()> {
formatter(
|ivf| {
ivf.format_sexp(
&SExp::builder()
.push("hello")
.push(5)
.push(true)
.build_sexp(),
)
},
|ivf| ivf.format_sexp(&ion_sexp!("hello" 5 true)),
"(\"hello\" 5 true)",
);
Ok(())
Expand Down
34 changes: 16 additions & 18 deletions src/value/builders.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,7 +64,8 @@ impl ListBuilder {
/// .push(1)
/// .push(true)
/// .push("foo")
/// .build();
/// .build()
/// .into();
/// let expected = ion!(r#"(1 true "foo")"#);
/// assert_eq!(actual, expected);
/// ```
Expand Down Expand Up @@ -103,14 +104,9 @@ impl SExpBuilder {
}

/// Builds a [`SExp`] with the previously specified elements.
pub fn build_sexp(self) -> SExp {
pub fn build(self) -> SExp {
SExp::new(self.values)
}

/// Builds an s-expression [`Element`] with the previously specified child elements.
pub fn build(self) -> Element {
self.build_sexp().into()
}
}

/// Constructs [Struct] values incrementally.
Expand Down Expand Up @@ -246,7 +242,7 @@ impl From<ListBuilder> for Element {

impl From<SExpBuilder> for Element {
fn from(s_expr_builder: SExpBuilder) -> Self {
s_expr_builder.build()
s_expr_builder.build().into()
}
}

Expand Down Expand Up @@ -311,8 +307,9 @@ macro_rules! ion_list {
///
/// ```
/// use ion_rs::{ion, ion_sexp};
/// use ion_rs::value::owned::Element;
/// // Construct an s-expression Element from Rust values
/// let actual = ion_sexp!("foo" 7 false ion_sexp!(1.5f64 8.25f64));
/// let actual: Element = ion_sexp!("foo" 7 false ion_sexp!(1.5f64 8.25f64)).into();
/// // Construct an Element from serialized Ion data
/// let expected = ion!(r#"("foo" 7 false (1.5e0 8.25e0))"#);
/// // Compare the two Elements
Expand All @@ -330,13 +327,13 @@ macro_rules! ion_list {
/// let string_element: Element = "foo".into();
/// let bool_element: Element = true.into();
///
/// let actual = ion_sexp!(
/// let actual: Element = ion_sexp!(
/// string_element
/// bool_element
/// 10i64.with_annotations(["bar"]) // .with_annotations() constructs an Element
/// Element::clob("hello")
/// Element::symbol("world")
/// );
/// ).into();
/// // Construct an Element from serialized Ion data
/// let expected = ion!(r#"("foo" true bar::10 {{"hello"}} world)"#);
/// // Compare the two Elements
Expand Down Expand Up @@ -477,21 +474,21 @@ mod tests {
.push(true)
.push("foo")
.push(Symbol::owned("bar"))
.build();
.build()
.into();
let expected = ion!(r#"(1 true "foo" bar)"#);
assert_eq!(actual, expected);
}

#[test]
fn sexp_clone_builder() {
let original_sexp = ion_sexp!(1 true "foo" Symbol::owned("bar"));
let new_sexp = original_sexp
.as_sexp()
.unwrap()
let new_sexp: Element = original_sexp
.clone_builder()
.remove(1)
.push(88)
.build();
.build()
.into();
let expected_sexp = ion!(r#"(1 "foo" bar 88)"#);
assert_eq!(new_sexp, expected_sexp);
}
Expand All @@ -505,14 +502,15 @@ mod tests {
.remove(1)
.remove(1)
.push(Symbol::owned("bar"))
.build();
.build()
.into();
let expected = ion!("(1 bar)");
assert_eq!(actual, expected);
}

#[test]
fn make_sexp_with_macro() {
let actual: Element = ion_sexp!(1 true "foo" Symbol::owned("bar"));
let actual: Element = ion_sexp!(1 true "foo" Symbol::owned("bar")).into();
let expected = ion!(r#"(1 true "foo" bar)"#);
assert_eq!(actual, expected);
}
Expand Down
2 changes: 1 addition & 1 deletion src/value/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -447,7 +447,7 @@ mod tests {

fn sexp_case() -> Case {
Case {
elem: ion_sexp!(true false),
elem: ion_sexp!(true false).into(),
ion_type: IonType::SExpression,
ops: vec![AsSExp, AsSequence],
op_assert: Box::new(|e: &Element| {
Expand Down
2 changes: 1 addition & 1 deletion src/value/reader.rs
Original file line number Diff line number Diff line change
Expand Up @@ -241,7 +241,7 @@ mod reader_tests {
(e f g)
"#,
vec![
ion_sexp!(Symbol::owned("e") Symbol::owned("f") Symbol::owned("g"))
ion_sexp!(Symbol::owned("e") Symbol::owned("f") Symbol::owned("g")).into()
]
)]
#[case::structs(
Expand Down

0 comments on commit 09bbc67

Please sign in to comment.