diff --git a/src/lazy/encoder/binary/v1_0/mod.rs b/src/lazy/encoder/binary/v1_0/mod.rs index 8b49f2b33..d562517c3 100644 --- a/src/lazy/encoder/binary/v1_0/mod.rs +++ b/src/lazy/encoder/binary/v1_0/mod.rs @@ -11,7 +11,7 @@ pub mod writer; impl LazyEncoder for BinaryEncoding_1_0 { const SUPPORTS_TEXT_TOKENS: bool = false; const DEFAULT_SYMBOL_CREATION_POLICY: SymbolCreationPolicy = - SymbolCreationPolicy::CreateSymbolIdNow; + SymbolCreationPolicy::RequireSymbolId; type Writer = LazyRawBinaryWriter_1_0; diff --git a/src/lazy/encoder/binary/v1_1/mod.rs b/src/lazy/encoder/binary/v1_1/mod.rs index 1064278cd..b6367f52b 100644 --- a/src/lazy/encoder/binary/v1_1/mod.rs +++ b/src/lazy/encoder/binary/v1_1/mod.rs @@ -16,7 +16,7 @@ pub mod writer; impl LazyEncoder for BinaryEncoding_1_1 { const SUPPORTS_TEXT_TOKENS: bool = true; const DEFAULT_SYMBOL_CREATION_POLICY: SymbolCreationPolicy = - SymbolCreationPolicy::CreateSymbolIdNow; + SymbolCreationPolicy::RequireSymbolId; type Writer = LazyRawBinaryWriter_1_1; diff --git a/src/lazy/encoder/mod.rs b/src/lazy/encoder/mod.rs index 3107cf541..0919f0dee 100644 --- a/src/lazy/encoder/mod.rs +++ b/src/lazy/encoder/mod.rs @@ -41,12 +41,13 @@ pub trait LazyEncoder: 'static + Encoding + Sized + Debug + Clone + Copy { pub enum SymbolCreationPolicy { // Prefer a compact encoding; create symbol table entries for all field names, annotations, // and symbol values. For text Ion, this will result in less human-friendly output. - CreateSymbolIdNow, - // When the encoding supports it, do not add nwe symbols to the symbol table. Instead, write - // out the text of the symbol at the usage site. - PreferInlineText, - // TODO: Other policies, such as: - // * Waiting until the next flush() operation to add new symbol definitions in bulk. + RequireSymbolId, + // When the encoding supports it, write whatever token (symbol ID or text) that the user provided. + // Do not create new symbol table entries. + WriteProvidedToken, + // TODO: Other potential policies, such as: + // * Require text (if a SID doesn't map to text, it's an error) + // * Wait until the next `flush()` operation to add new symbol definitions in bulk. // * Using a symbol detection mechanism to intern recurring symbols after `N` usages. } diff --git a/src/lazy/encoder/text/mod.rs b/src/lazy/encoder/text/mod.rs index 2d73edccc..d8370be74 100644 --- a/src/lazy/encoder/text/mod.rs +++ b/src/lazy/encoder/text/mod.rs @@ -109,7 +109,7 @@ impl LazyRawWriter for LazyRawTextWriter_1_0 { impl LazyEncoder for TextEncoding_1_0 { const SUPPORTS_TEXT_TOKENS: bool = true; const DEFAULT_SYMBOL_CREATION_POLICY: SymbolCreationPolicy = - SymbolCreationPolicy::PreferInlineText; + SymbolCreationPolicy::WriteProvidedToken; type Writer = LazyRawTextWriter_1_0; diff --git a/src/lazy/encoder/writer.rs b/src/lazy/encoder/writer.rs index 3934e2083..ec0abbf76 100644 --- a/src/lazy/encoder/writer.rs +++ b/src/lazy/encoder/writer.rs @@ -178,7 +178,7 @@ impl<'value, V: ValueWriter> AnnotatableWriter for ApplicationValueWriter<'value where Self: 'a, { - if self.encoding.symbol_creation_policy == SymbolCreationPolicy::PreferInlineText { + if self.encoding.symbol_creation_policy == SymbolCreationPolicy::WriteProvidedToken { // Store the tokens as they are. Text will be written as text, symbol IDs will be written // as symbol IDs. TODO: Lookup SIDs to see if they have text? return Ok(ApplicationValueWriter { @@ -255,7 +255,7 @@ impl<'value, V: ValueWriter> ValueWriter for ApplicationValueWriter<'value, V> { // If the writer can write it as inline text, do so. if self.encoding.supports_text_tokens - && self.encoding.symbol_creation_policy == SymbolCreationPolicy::PreferInlineText + && self.encoding.symbol_creation_policy == SymbolCreationPolicy::WriteProvidedToken { return self.raw_value_writer.write_symbol(text.as_ref()); } @@ -350,7 +350,7 @@ impl<'value, V: ValueWriter> FieldEncoder for ApplicationStructWriter<'value, V> // If the writer can write it as inline text, do so. if self.encoding.supports_text_tokens - && self.encoding.symbol_creation_policy == SymbolCreationPolicy::PreferInlineText + && self.encoding.symbol_creation_policy == SymbolCreationPolicy::WriteProvidedToken { return self.raw_struct_writer.encode_field_name(text.as_ref()); } @@ -495,46 +495,3 @@ impl ElementWriter for ApplicationWriter { Ok(()) } } - -#[cfg(test)] -mod tests { - use super::*; - use crate::lazy::encoder::annotate::Annotatable; - use crate::lazy::encoding::BinaryEncoding_1_0; - use crate::symbol_ref::AsSymbolRef; - - #[test] - fn write_element() -> IonResult<()> { - let mut writer = ApplicationWriter::::new(vec![])?; - writer.write_elements(&[Element::from(1)])?; - writer.flush()?; - let output = writer.close()?; - println!("{:?}", Element::read_all(output)); - Ok(()) - } - - #[test] - fn try_it() -> IonResult<()> { - let mut writer = ApplicationWriter::::new(vec![])?; - writer.write(5)?; - writer.write_symbol("foo")?; - writer.write( - &["bar".as_symbol_ref(), "baz".as_symbol_ref()].annotated_with(["spuz", "fwee"]), - )?; - let mut struct_ = writer - .value_writer() - .with_annotations("spud")? - .struct_writer()?; - struct_ - .write("quux", 1)? - .write("quuz", 2.annotated_with("buzz"))?; - struct_.close()?; - writer.write(false.annotated_with(["gary", "waldo"]))?; - let output = writer.close()?; - println!("{:0x?}", output.as_slice()); - Element::read_all(output.as_slice())? - .iter() - .for_each(|e| println!("{e}")); - Ok(()) - } -}