Skip to content

Commit

Permalink
Better SymbolCreationPolicy variant names
Browse files Browse the repository at this point in the history
  • Loading branch information
zslayton committed Apr 18, 2024
1 parent 32a944c commit f42720d
Show file tree
Hide file tree
Showing 5 changed files with 13 additions and 55 deletions.
2 changes: 1 addition & 1 deletion src/lazy/encoder/binary/v1_0/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<W: Write> = LazyRawBinaryWriter_1_0<W>;

Expand Down
2 changes: 1 addition & 1 deletion src/lazy/encoder/binary/v1_1/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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<W: Write> = LazyRawBinaryWriter_1_1<W>;

Expand Down
13 changes: 7 additions & 6 deletions src/lazy/encoder/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
}

Expand Down
2 changes: 1 addition & 1 deletion src/lazy/encoder/text/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -109,7 +109,7 @@ impl<W: Write> LazyRawWriter<W> for LazyRawTextWriter_1_0<W> {
impl LazyEncoder for TextEncoding_1_0 {
const SUPPORTS_TEXT_TOKENS: bool = true;
const DEFAULT_SYMBOL_CREATION_POLICY: SymbolCreationPolicy =
SymbolCreationPolicy::PreferInlineText;
SymbolCreationPolicy::WriteProvidedToken;

type Writer<W: Write> = LazyRawTextWriter_1_0<W>;

Expand Down
49 changes: 3 additions & 46 deletions src/lazy/encoder/writer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -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());
}
Expand Down Expand Up @@ -495,46 +495,3 @@ impl<E: LazyEncoder, O: Write> ElementWriter for ApplicationWriter<E, O> {
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::<BinaryEncoding_1_0, _>::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::<BinaryEncoding_1_0, _>::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(())
}
}

0 comments on commit f42720d

Please sign in to comment.