Skip to content

Commit

Permalink
Update tests and documentation.
Browse files Browse the repository at this point in the history
  • Loading branch information
TheVeryDarkness committed Sep 3, 2024
1 parent 0e6348a commit 51c9a35
Show file tree
Hide file tree
Showing 8 changed files with 138 additions and 44 deletions.
21 changes: 21 additions & 0 deletions examples/doc_show.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
use iof::show;

fn main() {
// Write a single integer to output.
show!(42);

// Write a single string to output.
show!("Hello, World!");

// Write a vector of integers to output.
show!([1, 2, 3]);

// Write a matrix of integers to output.
show!([[1, 2, 3], [4, 5, 6]]);

// Write a matrix of characters to output.
show!([['.', '@', '/'], ['#', '$', '$']]);

// Write a tuple to output.
show!((1, 2, 3));
}
8 changes: 8 additions & 0 deletions examples/doc_show.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
42
Hello, World!
1 2 3
1 2 3
4 5 6
.@/
#$$
1 2 3
6 changes: 6 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,12 @@
//!
//! *You may have noticed that the `show!` macro is similar to the [`print`](https://docs.python.org/zh-cn/3/library/functions.html#print) function in Python.*
//!
//! Code below writes the output to [standard output](std::io::Stdout):
//!
//! ```rust
#![doc = include_str!("../examples/doc_show.rs")]
//! ```
//!
//! # Input
//!
//! ## [ReadInto]
Expand Down
1 change: 1 addition & 0 deletions src/read/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -6,3 +6,4 @@ pub(super) mod read_from;
pub(super) mod read_into;
pub(super) mod read_one_from;
pub(super) mod read_one_into;
mod tuple;
44 changes: 0 additions & 44 deletions src/read/read_from.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::read_one_from::ReadOneFrom;
use crate::{array::array_try_from_fn, mat::Mat, BufReadExt, ReadError};
use std::fmt::{self, Display};

/// The error type for [ReadFrom].
pub type ReadFromError<T> = ReadError<<T as ReadFrom>::ParseError>;
Expand Down Expand Up @@ -86,46 +85,3 @@ impl<T: ReadOneFrom> ReadFrom for Vec<T> {
T::try_read_some_in_line_from(stream)
}
}

macro_rules! impl_read_into_for_tuple {
($e:ident $($t:ident)*) => {
#[derive(Debug)]
pub enum $e<$($t, )* > {
$($t($t), )*
}
impl<$($t: std::error::Error, )* > Display for $e<$($t, )* > {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { $( Self::$t(err) => Display::fmt(err, f), )* }
}
}
impl<$($t: std::error::Error, )* > std::error::Error for $e<$($t, )* > {}
impl<$($t: ReadFrom, )*> ReadFrom for ( $($t, )* ) {
type ParseError = $e<$(<$t as ReadFrom>::ParseError, )*>;
fn try_read_from<S: BufReadExt>(stream: &mut S) -> Result<($($t, )*), ReadFromError<Self>> {
Ok((
$(
<$t as ReadFrom>::try_read_from(stream).map_err(|err| match err {
ReadFromError::<$t>::IOError(e) => ReadFromError::<Self>::IOError(e),
ReadFromError::<$t>::EOF => ReadFromError::<Self>::EOF,
ReadFromError::<$t>::EOL => ReadFromError::<Self>::EOL,
ReadFromError::<$t>::FromStrError(e, s, n) => ReadFromError::<Self>::FromStrError($e::$t(e), s, n),
})?,
)*
))
}
}
};
}

impl_read_into_for_tuple!(Tuple1Error T1);
impl_read_into_for_tuple!(Tuple2Error T1 T2);
impl_read_into_for_tuple!(Tuple3Error T1 T2 T3);
impl_read_into_for_tuple!(Tuple4Error T1 T2 T3 T4);
impl_read_into_for_tuple!(Tuple5Error T1 T2 T3 T4 T5);
impl_read_into_for_tuple!(Tuple6Error T1 T2 T3 T4 T5 T6);
impl_read_into_for_tuple!(Tuple7Error T1 T2 T3 T4 T5 T6 T7);
impl_read_into_for_tuple!(Tuple8Error T1 T2 T3 T4 T5 T6 T7 T8);
impl_read_into_for_tuple!(Tuple9Error T1 T2 T3 T4 T5 T6 T7 T8 T9);
impl_read_into_for_tuple!(Tuple10Error T1 T2 T3 T4 T5 T6 T7 T8 T9 T10);
impl_read_into_for_tuple!(Tuple11Error T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11);
impl_read_into_for_tuple!(Tuple12Error T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12);
55 changes: 55 additions & 0 deletions src/read/tuple.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,55 @@
use crate::{BufReadExt, ReadFrom, ReadFromError};
use std::fmt::{self, Display};

macro_rules! impl_read_into_for_tuple {
($e:ident) => {
use std::convert::Infallible as $e;
impl ReadFrom for () {
type ParseError = $e;
fn try_read_from<S: BufReadExt>(_stream: &mut S) -> Result<(), ReadFromError<Self>> {
Ok(())
}
}
};
($e:ident $($t:ident)+) => {
#[derive(Debug)]
pub enum $e<$($t, )+ > {
$($t($t), )+
}
impl<$($t: std::error::Error, )+ > Display for $e<$($t, )+ > {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
match self { $( Self::$t(err) => Display::fmt(err, f), )+ }
}
}
impl<$($t: std::error::Error, )+ > std::error::Error for $e<$($t, )+ > {}
impl<$($t: ReadFrom, )+> ReadFrom for ( $($t, )+ ) {
type ParseError = $e<$(<$t as ReadFrom>::ParseError, )+>;
fn try_read_from<S: BufReadExt>(stream: &mut S) -> Result<($($t, )+), ReadFromError<Self>> {
Ok((
$(
<$t as ReadFrom>::try_read_from(stream).map_err(|err| match err {
ReadFromError::<$t>::IOError(e) => ReadFromError::<Self>::IOError(e),
ReadFromError::<$t>::EOF => ReadFromError::<Self>::EOF,
ReadFromError::<$t>::EOL => ReadFromError::<Self>::EOL,
ReadFromError::<$t>::FromStrError(e, s, n) => ReadFromError::<Self>::FromStrError($e::$t(e), s, n),
})?,
)+
))
}
}
};
}

impl_read_into_for_tuple!(Tuple0Error);
impl_read_into_for_tuple!(Tuple1Error T1);
impl_read_into_for_tuple!(Tuple2Error T1 T2);
impl_read_into_for_tuple!(Tuple3Error T1 T2 T3);
impl_read_into_for_tuple!(Tuple4Error T1 T2 T3 T4);
impl_read_into_for_tuple!(Tuple5Error T1 T2 T3 T4 T5);
impl_read_into_for_tuple!(Tuple6Error T1 T2 T3 T4 T5 T6);
impl_read_into_for_tuple!(Tuple7Error T1 T2 T3 T4 T5 T6 T7);
impl_read_into_for_tuple!(Tuple8Error T1 T2 T3 T4 T5 T6 T7 T8);
impl_read_into_for_tuple!(Tuple9Error T1 T2 T3 T4 T5 T6 T7 T8 T9);
impl_read_into_for_tuple!(Tuple10Error T1 T2 T3 T4 T5 T6 T7 T8 T9 T10);
impl_read_into_for_tuple!(Tuple11Error T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11);
impl_read_into_for_tuple!(Tuple12Error T1 T2 T3 T4 T5 T6 T7 T8 T9 T10 T11 T12);
41 changes: 41 additions & 0 deletions src/write/impls.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,3 +25,44 @@ impl WriteOneInto for char {
s.write_all(&[*self as u8])
}
}

macro_rules! impl_write_one_into_for_tuple {
($n0:ident $t0:ident $(, $n:ident $t:ident)* $(,)?) => {
impl<$t0: WriteOneInto, $($t: WriteOneInto),*> WriteOneInto for ($t0, $($t,)*) {
const SEP_ITEM: &'static str = " ";

fn try_write_one_into<S: io::Write>(&self, s: &mut S) -> io::Result<()> {
let ($n0, $($n, )*) = self;
$n0.try_write_one_into(s)?;
$(
s.write_all(Self::SEP_ITEM.as_bytes())?;
$n.try_write_one_into(s)?;
)*
Ok(())
}
}
};
() => {
impl WriteOneInto for () {
const SEP_ITEM: &'static str = " ";

fn try_write_one_into<S: io::Write>(&self, _s: &mut S) -> io::Result<()> {
Ok(())
}
}
};
}

impl_write_one_into_for_tuple!();
impl_write_one_into_for_tuple!(t1 T1);
impl_write_one_into_for_tuple!(t1 T1, t2 T2);
impl_write_one_into_for_tuple!(t1 T1, t2 T2, t3 T3);
impl_write_one_into_for_tuple!(t1 T1, t2 T2, t3 T3, t4 T4);
impl_write_one_into_for_tuple!(t1 T1, t2 T2, t3 T3, t4 T4, t5 T5);
impl_write_one_into_for_tuple!(t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6);
impl_write_one_into_for_tuple!(t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7);
impl_write_one_into_for_tuple!(t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7, t8 T8);
impl_write_one_into_for_tuple!(t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7, t8 T8, t9 T9);
impl_write_one_into_for_tuple!(t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7, t8 T8, t9 T9, t10 T10);
impl_write_one_into_for_tuple!(t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7, t8 T8, t9 T9, t10 T10, t11 T11);
impl_write_one_into_for_tuple!(t1 T1, t2 T2, t3 T3, t4 T4, t5 T5, t6 T6, t7 T7, t8 T8, t9 T9, t10 T10, t11 T11, t12 T12);
6 changes: 6 additions & 0 deletions tests/test_bin.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,3 +94,9 @@ fn doc_get_line_some() {
"",
);
}

#[test]
#[cfg_attr(miri, ignore)]
fn doc_show() {
test_example("doc_show", "", include_str!("../examples/doc_show.txt"));
}

0 comments on commit 51c9a35

Please sign in to comment.