extern crate libxlsxwriter_sys;
pub mod chart;
mod error;
pub mod format;
pub mod prelude;
pub mod workbook;
pub mod worksheet;
use std::{ffi::CString, os::raw::c_char, pin::Pin};
use chart::*;
use error::XlsxErrorSource;
use format::*;
use worksheet::*;
pub use format::Format;
pub use workbook::Workbook;
pub use worksheet::Worksheet;
#[derive(Debug)]
pub struct XlsxError {
pub(crate) source: XlsxErrorSource,
}
fn convert_bool(value: bool) -> u8 {
let result = if value {
libxlsxwriter_sys::lxw_boolean_LXW_TRUE
} else {
libxlsxwriter_sys::lxw_boolean_LXW_FALSE
};
result as u8
}
fn convert_validation_bool(value: bool) -> u8 {
let result = if value {
libxlsxwriter_sys::lxw_validation_boolean_LXW_VALIDATION_ON
} else {
libxlsxwriter_sys::lxw_validation_boolean_LXW_VALIDATION_OFF
};
result as u8
}
#[derive(Debug, Clone, PartialEq, Default)]
pub(crate) struct CStringHelper {
strings: Vec<Pin<Box<CString>>>,
}
impl CStringHelper {
pub fn new() -> CStringHelper {
CStringHelper {
strings: Vec::new(),
}
}
pub fn add(&mut self, s: &str) -> Result<*const c_char, XlsxError> {
let s = Box::pin(CString::new(s)?);
let p = s.as_ptr();
self.strings.push(s);
Ok(p)
}
pub fn add_opt(&mut self, s: Option<&str>) -> Result<*const c_char, XlsxError> {
if let Some(s) = s {
self.add(s)
} else {
Ok(std::ptr::null())
}
}
}
pub(crate) fn try_to_vec<I, T>(it: I) -> Result<Vec<T>, XlsxError>
where
I: std::iter::Iterator<Item = Result<T, XlsxError>>,
{
let mut r = Vec::new();
for one in it {
r.push(one?);
}
Ok(r)
}
#[derive(Debug, Clone, PartialEq, PartialOrd)]
pub enum StringOrFloat {
String(String),
Float(f64),
}
impl Default for StringOrFloat {
fn default() -> Self {
StringOrFloat::Float(0.)
}
}
impl StringOrFloat {
#[must_use]
pub fn to_string(self) -> Option<String> {
match self {
StringOrFloat::String(x) => Some(x),
StringOrFloat::Float(_) => None,
}
}
#[must_use]
pub fn to_str(&self) -> Option<&str> {
match self {
StringOrFloat::String(x) => Some(x.as_str()),
StringOrFloat::Float(_) => None,
}
}
#[must_use]
pub fn to_f64(&self) -> Option<f64> {
match self {
StringOrFloat::String(_) => None,
StringOrFloat::Float(x) => Some(*x),
}
}
}
impl From<&str> for StringOrFloat {
fn from(val: &str) -> Self {
StringOrFloat::String(val.to_string())
}
}
impl From<String> for StringOrFloat {
fn from(val: String) -> Self {
StringOrFloat::String(val)
}
}
impl From<f64> for StringOrFloat {
fn from(val: f64) -> Self {
StringOrFloat::Float(val)
}
}
#[cfg(test)]
mod test;