Skip to content

Commit

Permalink
Renames TextKind to TextFormat, adds with_format method (#773)
Browse files Browse the repository at this point in the history
  • Loading branch information
zslayton authored May 23, 2024
1 parent b6a5804 commit 8a7b3a4
Show file tree
Hide file tree
Showing 10 changed files with 103 additions and 32 deletions.
2 changes: 1 addition & 1 deletion src/lazy/encoder/text/v1_0/value_writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -326,7 +326,7 @@ impl<'a, W: Write> TextSExpWriter_1_0<'a, W> {
parent_type,
ContainerType::SExp,
"(",
" ",
"",
trailing_delimiter,
)?;
Ok(Self { container_writer })
Expand Down
10 changes: 5 additions & 5 deletions src/lazy/encoder/text/v1_0/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,7 @@ use crate::text::whitespace_config::{
};
use crate::types::ParentType;
use crate::write_config::WriteConfigKind;
use crate::{IonResult, TextKind, WriteConfig};
use crate::{IonResult, TextFormat, WriteConfig};

/// A raw text Ion 1.0 writer.
pub struct LazyRawTextWriter_1_0<W: Write> {
Expand Down Expand Up @@ -73,7 +73,7 @@ impl<W: Write> MakeValueWriter for LazyRawTextWriter_1_0<W> {
impl<W: Write> LazyRawWriter<W> for LazyRawTextWriter_1_0<W> {
fn new(output: W) -> IonResult<Self> {
Self::build(
WriteConfig::<TextEncoding_1_0>::new(TextKind::default()),
WriteConfig::<TextEncoding_1_0>::new(TextFormat::default()),
output,
)
}
Expand All @@ -83,9 +83,9 @@ impl<W: Write> LazyRawWriter<W> for LazyRawTextWriter_1_0<W> {
match &config.kind {
WriteConfigKind::Text(text_config) => {
let whitespace_config = match text_config.text_kind {
TextKind::Compact => &COMPACT_WHITESPACE_CONFIG,
TextKind::Lines => &LINES_WHITESPACE_CONFIG,
TextKind::Pretty => &PRETTY_WHITESPACE_CONFIG,
TextFormat::Compact => &COMPACT_WHITESPACE_CONFIG,
TextFormat::Lines => &LINES_WHITESPACE_CONFIG,
TextFormat::Pretty => &PRETTY_WHITESPACE_CONFIG,
};
Ok(LazyRawTextWriter_1_0 {
output,
Expand Down
4 changes: 2 additions & 2 deletions src/lazy/encoder/text/v1_1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ use crate::lazy::encoding::TextEncoding_1_1;
use std::io::Write;

impl LazyEncoder for TextEncoding_1_1 {
const SUPPORTS_TEXT_TOKENS: bool = false;
const SUPPORTS_TEXT_TOKENS: bool = true;
const DEFAULT_SYMBOL_CREATION_POLICY: SymbolCreationPolicy =
SymbolCreationPolicy::RequireSymbolId;
SymbolCreationPolicy::WriteProvidedToken;
type Writer<W: Write> = LazyRawTextWriter_1_1<W>;
}
10 changes: 5 additions & 5 deletions src/lazy/encoder/text/v1_1/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ use crate::text::whitespace_config::{
COMPACT_WHITESPACE_CONFIG, LINES_WHITESPACE_CONFIG, PRETTY_WHITESPACE_CONFIG,
};
use crate::write_config::WriteConfigKind;
use crate::{IonResult, TextKind, WriteConfig};
use crate::{IonResult, TextFormat, WriteConfig};

// Text Ion 1.1 is a syntactic superset of Ion 1.0. The types comprising this writer implementation
// delegates nearly all of their functionality to the 1.0 text writer.
Expand Down Expand Up @@ -45,7 +45,7 @@ impl<W: Write> LazyRawWriter<W> for LazyRawTextWriter_1_1<W> {
Self: Sized,
{
Self::build(
WriteConfig::<TextEncoding_1_1>::new(TextKind::default()),
WriteConfig::<TextEncoding_1_1>::new(TextFormat::default()),
output,
)
}
Expand All @@ -57,9 +57,9 @@ impl<W: Write> LazyRawWriter<W> for LazyRawTextWriter_1_1<W> {
match &config.kind {
WriteConfigKind::Text(text_config) => {
let whitespace_config = match text_config.text_kind {
TextKind::Compact => &COMPACT_WHITESPACE_CONFIG,
TextKind::Lines => &LINES_WHITESPACE_CONFIG,
TextKind::Pretty => &PRETTY_WHITESPACE_CONFIG,
TextFormat::Compact => &COMPACT_WHITESPACE_CONFIG,
TextFormat::Lines => &LINES_WHITESPACE_CONFIG,
TextFormat::Pretty => &PRETTY_WHITESPACE_CONFIG,
};
write!(
output,
Expand Down
79 changes: 75 additions & 4 deletions src/lazy/encoding.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,8 @@ use crate::lazy::text::value::{
LazyRawTextValue, LazyRawTextValue_1_0, LazyRawTextValue_1_1, LazyRawTextVersionMarker_1_0,
LazyRawTextVersionMarker_1_1, RawTextAnnotationsIterator,
};
use crate::{IonResult, TextKind, WriteConfig};

use crate::{IonResult, TextFormat, WriteConfig};

/// Marker trait for types that represent an Ion encoding.
pub trait Encoding: LazyEncoder + LazyDecoder {
Expand Down Expand Up @@ -99,10 +100,22 @@ impl<'top> BinaryEncoding<'top> for BinaryEncoding_1_1 {}
#[derive(Copy, Clone, Debug, Default)]
pub struct TextEncoding_1_0;

impl TextEncoding_1_0 {
fn with_format(self, format: TextFormat) -> WriteConfig<Self> {
WriteConfig::<Self>::new(format)
}
}

/// The Ion 1.1 text encoding.
#[derive(Copy, Clone, Debug, Default)]
pub struct TextEncoding_1_1;

impl TextEncoding_1_1 {
fn with_format(self, format: TextFormat) -> WriteConfig<Self> {
WriteConfig::<Self>::new(format)
}
}

impl Encoding for BinaryEncoding_1_0 {
type Output = Vec<u8>;

Expand All @@ -129,7 +142,7 @@ impl Encoding for TextEncoding_1_0 {
"text Ion v1.0"
}
fn default_write_config() -> WriteConfig<Self> {
WriteConfig::<Self>::new(<TextKind as Default>::default())
WriteConfig::<Self>::new(<TextFormat as Default>::default())
}
}
impl Encoding for TextEncoding_1_1 {
Expand All @@ -138,7 +151,7 @@ impl Encoding for TextEncoding_1_1 {
"text Ion v1.1"
}
fn default_write_config() -> WriteConfig<Self> {
WriteConfig::<Self>::new(<TextKind as Default>::default())
WriteConfig::<Self>::new(<TextFormat as Default>::default())
}
}

Expand Down Expand Up @@ -226,10 +239,68 @@ impl LazyDecoder for BinaryEncoding_1_1 {
//
// If we do not confine the implementation to types with a marker trait, rustc complains that
// someone may someday use `ExpandedValueSource` as a `LazyDecoder::Value`, and then the
// the implementation will conflict with the core `impl<T> From<T> for T` implementation.
// implementation will conflict with the core `impl<T> From<T> for T` implementation.
pub trait RawValueLiteral {}

impl<'top, E: TextEncoding<'top>> RawValueLiteral for LazyRawTextValue<'top, E> {}
impl<'top> RawValueLiteral for LazyRawBinaryValue_1_0<'top> {}
impl<'top> RawValueLiteral for LazyRawBinaryValue_1_1<'top> {}
impl<'top> RawValueLiteral for LazyRawAnyValue<'top> {}

#[cfg(test)]
mod tests {
use rstest::rstest;

use crate::lazy::encoding::TextEncoding;
use crate::{
ion_list, ion_seq, ion_sexp, ion_struct, v1_0, v1_1, IonResult, Sequence, TextFormat,
WriteConfig,
};

#[rstest]
#[case::pretty_v1_0(
v1_0::Text.with_format(TextFormat::Pretty),
"{\n foo: 1,\n bar: 2,\n}\n[\n 1,\n 2,\n]\n(\n 1\n 2\n)\n"
)]
#[case::pretty_v1_1(
v1_1::Text.with_format(TextFormat::Pretty),
"$ion_1_1\n{\n foo: 1,\n bar: 2,\n}\n[\n 1,\n 2,\n]\n(\n 1\n 2\n)\n"
)]
#[case::compact_v1_0(
v1_0::Text.with_format(TextFormat::Compact),
"{foo: 1, bar: 2, } [1, 2, ] (1 2 ) "
)]
#[case::compact_v1_1(
v1_1::Text.with_format(TextFormat::Compact),
"$ion_1_1 {foo: 1, bar: 2, } [1, 2, ] (1 2 ) "
)]
#[case::lines_v1_0(
v1_0::Text.with_format(TextFormat::Lines),
"{foo: 1, bar: 2, }\n[1, 2, ]\n(1 2 )\n"
)]
#[case::lines_v1_1(
v1_1::Text.with_format(TextFormat::Lines),
"$ion_1_1\n{foo: 1, bar: 2, }\n[1, 2, ]\n(1 2 )\n"
)]
fn encode_formatted_text<'a, E: TextEncoding<'a>>(
#[case] config: impl Into<WriteConfig<E>>,
#[case] expected: &str,
) -> IonResult<()> {
let sequence: Sequence = ion_seq![
ion_struct! {
"foo" : 1,
"bar" : 2,
},
ion_list![1, 2],
ion_sexp! (1 2),
];

// The goal of this test is to confirm that the value was serialized using the requested text format.
// This string equality checks are unfortunately specific/fragile and can be modified if/when
// changes are made to the text formatting.

let text = sequence.encode_as(config)?;
assert_eq!(text, expected);
Ok(())
}
}
4 changes: 2 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -363,7 +363,7 @@ pub mod v1_1 {
/// Whether or not the text spacing is generous/human-friendly or something more compact.
#[derive(Copy, Clone, Debug, Eq, PartialEq, Default)]
#[non_exhaustive]
pub enum TextKind {
pub enum TextFormat {
Compact,
Lines,
#[default]
Expand All @@ -374,7 +374,7 @@ pub enum TextKind {
#[derive(Copy, Clone, Debug, Eq, PartialEq)]
#[non_exhaustive]
pub enum Format {
Text(TextKind),
Text(TextFormat),
Binary,
// TODO: Json(TextKind)
}
6 changes: 3 additions & 3 deletions src/serde/ser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -15,7 +15,7 @@ use crate::serde::timestamp::TUNNELED_TIMESTAMP_TYPE_NAME;
use crate::symbol_ref::AsSymbolRef;
use crate::write_config::WriteConfig;
use crate::Value::Null;
use crate::{Decimal, IonError, IonResult, IonType, TextKind, Timestamp};
use crate::{Decimal, IonError, IonResult, IonType, TextFormat, Timestamp};

pub fn write_to<T: Serialize, E: Encoding, O: Write>(
value: &T,
Expand All @@ -41,7 +41,7 @@ pub fn to_pretty<T>(value: &T) -> IonResult<String>
where
T: Serialize,
{
let config = WriteConfig::<TextEncoding_1_0>::new(TextKind::Pretty);
let config = WriteConfig::<TextEncoding_1_0>::new(TextFormat::Pretty);
let bytes = write_with_config(value, config)?;
match String::from_utf8(bytes) {
Ok(data) => Ok(data),
Expand All @@ -54,7 +54,7 @@ pub fn to_string<T>(value: &T) -> IonResult<String>
where
T: Serialize,
{
let config = WriteConfig::<TextEncoding_1_0>::new(TextKind::Compact);
let config = WriteConfig::<TextEncoding_1_0>::new(TextFormat::Compact);
let bytes = write_with_config(value, config)?;
match String::from_utf8(bytes) {
Ok(data) => Ok(data),
Expand Down
12 changes: 6 additions & 6 deletions src/write_config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@ use crate::lazy::encoding::{
BinaryEncoding_1_0, BinaryEncoding_1_1, Encoding, OutputFromBytes, TextEncoding_1_0,
TextEncoding_1_1,
};
use crate::{IonResult, TextKind};
use crate::{IonResult, TextFormat};

/// Writer configuration to provide format and Ion version details to writer through encoding
/// This will be used to create a writer without specifying which writer methods to use
Expand Down Expand Up @@ -80,7 +80,7 @@ impl<E: Encoding> WriteConfig<E> {
}

impl WriteConfig<TextEncoding_1_0> {
pub fn new(text_kind: TextKind) -> Self {
pub fn new(text_kind: TextFormat) -> Self {
Self {
kind: WriteConfigKind::Text(TextWriteConfig { text_kind }),
phantom_data: Default::default(),
Expand All @@ -89,7 +89,7 @@ impl WriteConfig<TextEncoding_1_0> {
}

impl WriteConfig<TextEncoding_1_1> {
pub fn new(text_kind: TextKind) -> Self {
pub fn new(text_kind: TextFormat) -> Self {
Self {
kind: WriteConfigKind::Text(TextWriteConfig { text_kind }),
phantom_data: Default::default(),
Expand Down Expand Up @@ -117,13 +117,13 @@ impl WriteConfig<BinaryEncoding_1_1> {

impl Default for WriteConfig<TextEncoding_1_0> {
fn default() -> Self {
Self::new(TextKind::Compact)
Self::new(TextFormat::Compact)
}
}

impl Default for WriteConfig<TextEncoding_1_1> {
fn default() -> Self {
Self::new(TextKind::Compact)
Self::new(TextFormat::Compact)
}
}

Expand All @@ -149,7 +149,7 @@ pub(crate) enum WriteConfigKind {
/// Text writer configuration with text kind to be used to create a writer
#[derive(Clone, Debug)]
pub(crate) struct TextWriteConfig {
pub(crate) text_kind: TextKind,
pub(crate) text_kind: TextFormat,
}

/// Binary writer configuration to be used to create a writer
Expand Down
6 changes: 3 additions & 3 deletions tests/ion_tests_1_1.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ impl ElementApi for LazyReaderElementApi {
mod good_round_trip {
use super::*;
use ion_rs::Format::Text;
use ion_rs::TextKind;
use ion_rs::TextFormat;
use test_generator::test_resources;

#[test_resources("ion-tests/iontestdata_1_1/good/**/*.ion")]
Expand All @@ -81,8 +81,8 @@ mod good_round_trip {
|| {
LazyReaderElementApi::assert_three_way_round_trip(
file_name,
Text(TextKind::Lines),
Text(TextKind::Lines),
Text(TextFormat::Lines),
Text(TextFormat::Lines),
)
},
);
Expand Down
2 changes: 1 addition & 1 deletion tests/lazy_element_ion_tests.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ use crate::ion_tests::{
};
use ion_rs::lazy::reader::Reader;
use ion_rs::IonResult;
use ion_rs::{Format, TextKind};
use ion_rs::{Format, TextFormat};
use test_generator::test_resources;

struct LazyReaderElementApi;
Expand Down

0 comments on commit 8a7b3a4

Please sign in to comment.