1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
use serde::Serialize;

use lexpr::print;

use crate::error::Result;
use crate::value::to_value;

use std::io;

/// Serialize an instance of type `T` into an S-expression string, using the
/// default printer options.
///
/// ```
/// use serde_lexpr::to_string;
///
/// assert_eq!(to_string(&("foo", 1)).unwrap(), r#"#("foo" 1)"#.to_string())
/// ```
pub fn to_string<T>(value: &T) -> Result<String>
where
    T: Serialize,
{
    Ok(lexpr::to_string(&to_value(value)?)?)
}

/// Serialize an instance of type `T` into an S-expression string.
///
/// ```
/// use serde_lexpr::{print, to_string_custom};
///
/// assert_eq!(to_string_custom(&("foo", 1), print::Options::elisp()).unwrap(), r#"["foo" 1]"#.to_string())
/// ```
pub fn to_string_custom<T>(value: &T, options: print::Options) -> Result<String>
where
    T: Serialize,
{
    Ok(lexpr::to_string_custom(&to_value(value)?, options)?)
}

/// Serialize an instance of type `T` into an S-expression byte vector, using the
/// default printer options.
///
/// ```
/// use serde_lexpr::to_vec;
///
/// let expected: Vec<u8> = r#"#("foo" 1)"#.into();
/// assert_eq!(to_vec(&("foo", 1)).unwrap(), expected);
/// ```
pub fn to_vec<T>(value: &T) -> Result<Vec<u8>>
where
    T: Serialize,
{
    Ok(lexpr::to_vec(&to_value(value)?)?)
}

/// Serialize an instance of type `T` into an S-expression byte vector.
///
/// ```
/// use serde_lexpr::{print, to_vec_custom};
///
/// let expected: Vec<u8> = r#"["foo" 1]"#.into();
/// assert_eq!(to_vec_custom(&("foo", 1), print::Options::elisp()).unwrap(), expected);
/// ```
pub fn to_vec_custom<T>(value: &T, options: print::Options) -> Result<Vec<u8>>
where
    T: Serialize,
{
    Ok(lexpr::to_vec_custom(&to_value(value)?, options)?)
}

/// Serialize an instance of type `T` into an S-expression byte vector, using the
/// default printer options.
///
/// ```
/// use serde_lexpr::to_writer;
///
/// let mut output = Vec::new();
/// to_writer(&mut output, &("foo", 1)).unwrap();
/// assert_eq!(output, r#"#("foo" 1)"#.as_bytes());
/// ```
pub fn to_writer<T, W>(writer: W, value: &T) -> Result<()>
where
    T: Serialize,
    W: io::Write,
{
    Ok(lexpr::to_writer(writer, &to_value(value)?)?)
}

/// Serialize an instance of type `T` into an S-expression byte vector.
///
/// ```
/// use serde_lexpr::{print, to_writer_custom};
///
/// let mut output = Vec::new();
/// to_writer_custom(&mut output, &("foo", 1), print::Options::elisp()).unwrap();
/// assert_eq!(output, r#"["foo" 1]"#.as_bytes());
/// ```
pub fn to_writer_custom<T, W>(writer: W, value: &T, options: print::Options) -> Result<()>
where
    T: Serialize,
    W: io::Write,
{
    Ok(lexpr::to_writer_custom(writer, &to_value(value)?, options)?)
}