@ -25,12 +25,12 @@ use std::io::{self, Write};
///
///
/// // boolean
/// // boolean
/// let mut buffer = Vec::new();
/// let mut buffer = Vec::new();
/// json_serializer.write_boolean_result (&mut buffer, true)?;
/// json_serializer.serialize_boolean_to_write (&mut buffer, true)?;
/// assert_eq!(buffer, b"{\"head\":{},\"boolean\":true}");
/// assert_eq!(buffer, b"{\"head\":{},\"boolean\":true}");
///
///
/// // solutions
/// // solutions
/// let mut buffer = Vec::new();
/// let mut buffer = Vec::new();
/// let mut writer = json_serializer.solutions_writer (&mut buffer, vec![Variable::new_unchecked("foo"), Variable::new_unchecked("bar")])?;
/// let mut writer = json_serializer.serialize_s olutions_to_ write(&mut buffer, vec![Variable::new_unchecked("foo"), Variable::new_unchecked("bar")])?;
/// writer.write(once((VariableRef::new_unchecked("foo"), LiteralRef::from("test"))))?;
/// writer.write(once((VariableRef::new_unchecked("foo"), LiteralRef::from("test"))))?;
/// writer.finish()?;
/// writer.finish()?;
/// assert_eq!(buffer, b"{\"head\":{\"vars\":[\"foo\",\"bar\"]},\"results\":{\"bindings\":[{\"foo\":{\"type\":\"literal\",\"value\":\"test\"}}]}}");
/// assert_eq!(buffer, b"{\"head\":{\"vars\":[\"foo\",\"bar\"]},\"results\":{\"bindings\":[{\"foo\":{\"type\":\"literal\",\"value\":\"test\"}}]}}");
@ -55,21 +55,30 @@ impl QueryResultsSerializer {
///
///
/// let json_serializer = QueryResultsSerializer::from_format(QueryResultsFormat::Xml);
/// let json_serializer = QueryResultsSerializer::from_format(QueryResultsFormat::Xml);
/// let mut buffer = Vec::new();
/// let mut buffer = Vec::new();
/// json_serializer.write_boolean_result (&mut buffer, true)?;
/// json_serializer.serialize_boolean_to_write (&mut buffer, true)?;
/// assert_eq!(buffer, b"<?xml version=\"1.0\"?><sparql xmlns=\"http://www.w3.org/2005/sparql-results#\"><head></head><boolean>true</boolean></sparql>");
/// assert_eq!(buffer, b"<?xml version=\"1.0\"?><sparql xmlns=\"http://www.w3.org/2005/sparql-results#\"><head></head><boolean>true</boolean></sparql>");
/// # std::io::Result::Ok(())
/// # std::io::Result::Ok(())
/// ```
/// ```
pub fn write_boolean_result < W : Write > ( & self , writer : W , value : bool ) -> io ::Result < W > {
pub fn serialize_boolean_to_write < W : Write > ( & self , write : W , value : bool ) -> io ::Result < W > {
match self . format {
match self . format {
QueryResultsFormat ::Xml = > write_boolean_xml_result ( writer , value ) ,
QueryResultsFormat ::Xml = > write_boolean_xml_result ( write , value ) ,
QueryResultsFormat ::Json = > write_boolean_json_result ( writer , value ) ,
QueryResultsFormat ::Json = > write_boolean_json_result ( write , value ) ,
QueryResultsFormat ::Csv = > write_boolean_csv_result ( writer , value ) ,
QueryResultsFormat ::Csv = > write_boolean_csv_result ( write , value ) ,
QueryResultsFormat ::Tsv = > write_boolean_tsv_result ( writer , value ) ,
QueryResultsFormat ::Tsv = > write_boolean_tsv_result ( write , value ) ,
}
}
}
#[ deprecated(note = " Use serialize_boolean_to_write " ) ]
pub fn write_boolean_result < W : Write > ( & self , writer : W , value : bool ) -> io ::Result < W > {
self . serialize_boolean_to_write ( writer , value )
}
}
/// Returns a `SolutionsWriter` allowing writing query solutions into the given [`Write`] implementation.
/// Returns a `SolutionsWriter` allowing writing query solutions into the given [`Write`] implementation.
///
///
/// <div class="warning">Do not forget to run the [`finish`](ToWriteSolutionsWriter::finish()) method to properly write the last bytes of the file.</div>
///
/// <div class="warning">This writer does unbuffered writes. You might want to use [`BufWriter`](io::BufWriter) to avoid that.</div>
///
/// Example in XML (the API is the same for JSON and TSV):
/// Example in XML (the API is the same for JSON and TSV):
/// ```
/// ```
/// use sparesults::{QueryResultsFormat, QueryResultsSerializer};
/// use sparesults::{QueryResultsFormat, QueryResultsSerializer};
@ -78,40 +87,51 @@ impl QueryResultsSerializer {
///
///
/// let json_serializer = QueryResultsSerializer::from_format(QueryResultsFormat::Xml);
/// let json_serializer = QueryResultsSerializer::from_format(QueryResultsFormat::Xml);
/// let mut buffer = Vec::new();
/// let mut buffer = Vec::new();
/// let mut writer = json_serializer.solutions_writer (&mut buffer, vec![Variable::new_unchecked("foo"), Variable::new_unchecked("bar")])?;
/// let mut writer = json_serializer.serialize_s olutions_to_ write(&mut buffer, vec![Variable::new_unchecked("foo"), Variable::new_unchecked("bar")])?;
/// writer.write(once((VariableRef::new_unchecked("foo"), LiteralRef::from("test"))))?;
/// writer.write(once((VariableRef::new_unchecked("foo"), LiteralRef::from("test"))))?;
/// writer.finish()?;
/// writer.finish()?;
/// assert_eq!(buffer, b"<?xml version=\"1.0\"?><sparql xmlns=\"http://www.w3.org/2005/sparql-results#\"><head><variable name=\"foo\"/><variable name=\"bar\"/></head><results><result><binding name=\"foo\"><literal>test</literal></binding></result></results></sparql>");
/// assert_eq!(buffer, b"<?xml version=\"1.0\"?><sparql xmlns=\"http://www.w3.org/2005/sparql-results#\"><head><variable name=\"foo\"/><variable name=\"bar\"/></head><results><result><binding name=\"foo\"><literal>test</literal></binding></result></results></sparql>");
/// # std::io::Result::Ok(())
/// # std::io::Result::Ok(())
/// ```
/// ```
pub fn solutions_writer < W : Write > (
pub fn serialize_s olutions_to_ write < W : Write > (
& self ,
& self ,
writer : W ,
write : W ,
variables : Vec < Variable > ,
variables : Vec < Variable > ,
) -> io ::Result < SolutionsWriter < W > > {
) -> io ::Result < ToWrite SolutionsWriter< W > > {
Ok ( SolutionsWriter {
Ok ( ToWrite SolutionsWriter {
formatter : match self . format {
formatter : match self . format {
QueryResultsFormat ::Xml = > {
QueryResultsFormat ::Xml = > {
SolutionsWriterKind ::Xml ( XmlSolutionsWriter ::start ( writer , & variables ) ? )
ToWrite SolutionsWriterKind ::Xml ( XmlSolutionsWriter ::start ( write , & variables ) ? )
}
}
QueryResultsFormat ::Json = > {
QueryResultsFormat ::Json = > {
SolutionsWriterKind ::Json ( JsonSolutionsWriter ::start ( writer , & variables ) ? )
ToWrite SolutionsWriterKind ::Json ( JsonSolutionsWriter ::start ( write , & variables ) ? )
}
}
QueryResultsFormat ::Csv = > {
QueryResultsFormat ::Csv = > {
SolutionsWriterKind ::Csv ( CsvSolutionsWriter ::start ( writer , variables ) ? )
ToWrite SolutionsWriterKind ::Csv ( CsvSolutionsWriter ::start ( write , variables ) ? )
}
}
QueryResultsFormat ::Tsv = > {
QueryResultsFormat ::Tsv = > {
SolutionsWriterKind ::Tsv ( TsvSolutionsWriter ::start ( writer , variables ) ? )
ToWrite SolutionsWriterKind ::Tsv ( TsvSolutionsWriter ::start ( write , variables ) ? )
}
}
} ,
} ,
} )
} )
}
}
#[ deprecated(note = " Use serialize_solutions_to_write " ) ]
pub fn solutions_writer < W : Write > (
& self ,
writer : W ,
variables : Vec < Variable > ,
) -> io ::Result < ToWriteSolutionsWriter < W > > {
self . serialize_solutions_to_write ( writer , variables )
}
}
}
/// Allows writing query results.
/// Allows writing query results.
/// Could be built using a [`QueryResultsSerializer`].
/// Could be built using a [`QueryResultsSerializer`].
///
///
/// <div class="warning">Do not forget to run the [`finish`](SolutionsWriter::finish()) method to properly write the last bytes of the file.</div>
/// <div class="warning">Do not forget to run the [`finish`](ToWriteSolutionsWriter::finish()) method to properly write the last bytes of the file.</div>
///
/// <div class="warning">This writer does unbuffered writes. You might want to use [`BufWriter`](io::BufWriter) to avoid that.</div>
///
///
/// Example in TSV (the API is the same for JSON and XML):
/// Example in TSV (the API is the same for JSON and XML):
/// ```
/// ```
@ -121,25 +141,25 @@ impl QueryResultsSerializer {
///
///
/// let json_serializer = QueryResultsSerializer::from_format(QueryResultsFormat::Tsv);
/// let json_serializer = QueryResultsSerializer::from_format(QueryResultsFormat::Tsv);
/// let mut buffer = Vec::new();
/// let mut buffer = Vec::new();
/// let mut writer = json_serializer.solutions_writer (&mut buffer, vec![Variable::new_unchecked("foo"), Variable::new_unchecked("bar")])?;
/// let mut writer = json_serializer.serialize_s olutions_to_ write(&mut buffer, vec![Variable::new_unchecked("foo"), Variable::new_unchecked("bar")])?;
/// writer.write(once((VariableRef::new_unchecked("foo"), LiteralRef::from("test"))))?;
/// writer.write(once((VariableRef::new_unchecked("foo"), LiteralRef::from("test"))))?;
/// writer.finish()?;
/// writer.finish()?;
/// assert_eq!(buffer, b"?foo\t?bar\n\"test\"\t\n");
/// assert_eq!(buffer, b"?foo\t?bar\n\"test\"\t\n");
/// # std::io::Result::Ok(())
/// # std::io::Result::Ok(())
/// ```
/// ```
#[ must_use ]
#[ must_use ]
pub struct SolutionsWriter < W : Write > {
pub struct ToWrite SolutionsWriter< W : Write > {
formatter : SolutionsWriterKind < W > ,
formatter : ToWrite SolutionsWriterKind< W > ,
}
}
enum SolutionsWriterKind < W : Write > {
enum ToWrite SolutionsWriterKind< W : Write > {
Xml ( XmlSolutionsWriter < W > ) ,
Xml ( XmlSolutionsWriter < W > ) ,
Json ( JsonSolutionsWriter < W > ) ,
Json ( JsonSolutionsWriter < W > ) ,
Csv ( CsvSolutionsWriter < W > ) ,
Csv ( CsvSolutionsWriter < W > ) ,
Tsv ( TsvSolutionsWriter < W > ) ,
Tsv ( TsvSolutionsWriter < W > ) ,
}
}
impl < W : Write > SolutionsWriter < W > {
impl < W : Write > ToWrite SolutionsWriter< W > {
/// Writes a solution.
/// Writes a solution.
///
///
/// Example in JSON (the API is the same for XML and TSV):
/// Example in JSON (the API is the same for XML and TSV):
@ -150,7 +170,7 @@ impl<W: Write> SolutionsWriter<W> {
///
///
/// let json_serializer = QueryResultsSerializer::from_format(QueryResultsFormat::Json);
/// let json_serializer = QueryResultsSerializer::from_format(QueryResultsFormat::Json);
/// let mut buffer = Vec::new();
/// let mut buffer = Vec::new();
/// let mut writer = json_serializer.solutions_writer (&mut buffer, vec![Variable::new_unchecked("foo"), Variable::new_unchecked("bar")])?;
/// let mut writer = json_serializer.serialize_s olutions_to_ write(&mut buffer, vec![Variable::new_unchecked("foo"), Variable::new_unchecked("bar")])?;
/// writer.write(once((VariableRef::new_unchecked("foo"), LiteralRef::from("test"))))?;
/// writer.write(once((VariableRef::new_unchecked("foo"), LiteralRef::from("test"))))?;
/// writer.write(&QuerySolution::from((vec![Variable::new_unchecked("bar")], vec![Some(Literal::from("test").into())])))?;
/// writer.write(&QuerySolution::from((vec![Variable::new_unchecked("bar")], vec![Some(Literal::from("test").into())])))?;
/// writer.finish()?;
/// writer.finish()?;
@ -163,20 +183,20 @@ impl<W: Write> SolutionsWriter<W> {
) -> io ::Result < ( ) > {
) -> io ::Result < ( ) > {
let solution = solution . into_iter ( ) . map ( | ( v , s ) | ( v . into ( ) , s . into ( ) ) ) ;
let solution = solution . into_iter ( ) . map ( | ( v , s ) | ( v . into ( ) , s . into ( ) ) ) ;
match & mut self . formatter {
match & mut self . formatter {
SolutionsWriterKind ::Xml ( writer ) = > writer . write ( solution ) ,
ToWrite SolutionsWriterKind ::Xml ( writer ) = > writer . write ( solution ) ,
SolutionsWriterKind ::Json ( writer ) = > writer . write ( solution ) ,
ToWrite SolutionsWriterKind ::Json ( writer ) = > writer . write ( solution ) ,
SolutionsWriterKind ::Csv ( writer ) = > writer . write ( solution ) ,
ToWrite SolutionsWriterKind ::Csv ( writer ) = > writer . write ( solution ) ,
SolutionsWriterKind ::Tsv ( writer ) = > writer . write ( solution ) ,
ToWrite SolutionsWriterKind ::Tsv ( writer ) = > writer . write ( solution ) ,
}
}
}
}
/// Writes the last bytes of the file.
/// Writes the last bytes of the file.
pub fn finish ( self ) -> io ::Result < W > {
pub fn finish ( self ) -> io ::Result < W > {
match self . formatter {
match self . formatter {
SolutionsWriterKind ::Xml ( write ) = > write . finish ( ) ,
ToWrite SolutionsWriterKind ::Xml ( write ) = > write . finish ( ) ,
SolutionsWriterKind ::Json ( write ) = > write . finish ( ) ,
ToWrite SolutionsWriterKind ::Json ( write ) = > write . finish ( ) ,
SolutionsWriterKind ::Csv ( write ) = > write . finish ( ) ,
ToWrite SolutionsWriterKind ::Csv ( write ) = > write . finish ( ) ,
SolutionsWriterKind ::Tsv ( write ) = > write . finish ( ) ,
ToWrite SolutionsWriterKind ::Tsv ( write ) = > write . finish ( ) ,
}
}
}
}
}
}