Skip to main content

Error

Struct Error 

Source
pub struct Error { /* private fields */ }
Expand description

A spreadsheet error retaining the affected part, cell, and underlying cause. Details are allocated only on failure, keeping successful Result values small.

Implementations§

Source§

impl Error

Source

pub fn new(kind: ErrorKind, message: impl Into<String>) -> Error

Construct an error without an underlying cause.

Examples found in repository?
examples/temporal_style_fixture.rs (line 37)
32    fn write_row(&mut self, row: &mut Row) -> crabxl::Result<()> {
33        match self {
34            Self::Streaming(writer) => writer.write_row(row),
35            Self::Owned { book, sheet } => {
36                let id = sheet.ok_or_else(|| {
37                    crabxl::Error::new(crabxl::ErrorKind::InvalidState, "Missing fixture sheet")
38                })?;
39                let mut editor = book.sheet_mut(id)?;
40                for cell in row.cells.drain(..) {
41                    editor.set(cell)?;
42                }
43                Ok(())
44            }
45        }
46    }
More examples
Hide additional examples
examples/support/printing_updates.rs (line 8)
4pub fn run(mut update: impl FnMut(&mut WorkbookEditor<File>, usize) -> Result<()>) -> Result<()> {
5    let mut arguments = std::env::args().skip(1);
6    let source = arguments
7        .next()
8        .ok_or_else(|| Error::new(ErrorKind::InvalidData, "Missing source"))?;
9    let target = arguments
10        .next()
11        .ok_or_else(|| Error::new(ErrorKind::InvalidData, "Missing target"))?;
12    let count = arguments
13        .next()
14        .ok_or_else(|| Error::new(ErrorKind::InvalidData, "Missing update count"))?
15        .parse::<usize>()
16        .map_err(|error| Error::caused_by(ErrorKind::InvalidData, "Invalid update count", error))?;
17    if count == 0 {
18        return Err(Error::new(
19            ErrorKind::InvalidData,
20            "Update count must be positive",
21        ));
22    }
23    let mut editor = WorkbookEditor::open(source)?;
24    for index in 0..count {
25        update(&mut editor, index)?;
26    }
27    println!("PRINT_BYTES={}", editor.patch_bytes());
28    let output = File::create(target)
29        .map_err(|error| Error::caused_by(ErrorKind::Io, "Cannot create output", error))?;
30    editor.save(BufWriter::new(output), SaveOptions::default())?;
31    println!("{count}");
32    Ok(())
33}
examples/printing.rs (lines 86-89)
83fn run() -> Result<()> {
84    let args: Vec<_> = std::env::args().collect();
85    if args.len() != 5 {
86        return Err(crabxl::Error::new(
87            crabxl::ErrorKind::InvalidData,
88            "Usage: printing create|copy|edit SOURCE OUTPUT ROWS",
89        ));
90    }
91    if args[1] == "edit" {
92        let mut editor = WorkbookEditor::open(&args[2])?;
93        editor.set_print_settings("Sheet", settings()?)?;
94        println!("PRINT_BYTES={}", editor.patch_bytes());
95        editor.save(output(&args[3])?, SaveOptions::default())?;
96        println!("TEMP_BYTES=0");
97        return Ok(());
98    }
99    if !matches!(args[1].as_str(), "create" | "copy") {
100        return Err(crabxl::Error::new(
101            crabxl::ErrorKind::InvalidData,
102            "Unknown printing operation",
103        ));
104    }
105    let mut reader = if args[1] == "copy" {
106        Some(WorkbookReader::open(&args[2])?)
107    } else {
108        None
109    };
110    let value = match &mut reader {
111        Some(reader) => reader.print_settings("Sheet")?,
112        None => settings()?,
113    };
114    println!("PRINT_BYTES={}", value.memory_bytes());
115    let mut writer = WorkbookWriter::new(WriteOptions::default())?;
116    writer.start_sheet_with_settings("Sheet", None, Some(&value))?;
117    if let Some(reader) = &mut reader {
118        let mut rows = reader.rows("Sheet")?;
119        while let Some(row) = rows.next_row()? {
120            writer.write_row(&row)?;
121        }
122    } else {
123        let count: u32 = args[4].parse().map_err(|error| {
124            crabxl::Error::caused_by(
125                crabxl::ErrorKind::InvalidData,
126                "Invalid printing row count",
127                error,
128            )
129        })?;
130        let mut row = Row::new(RowIndex::new(0)?);
131        for index in 0..count {
132            row.index = RowIndex::new(index)?;
133            row.cells.clear();
134            row.cells.push(Cell {
135                address: crabxl::CellAddress::new(index, 0)?,
136                value: CellValue::Integer(i64::from(index)),
137                style: StyleId::new(0),
138            });
139            writer.write_row(&row)?;
140        }
141    }
142    writer.close_sheet()?;
143    println!("TEMP_BYTES={}", writer.stats().peak_temp_bytes);
144    writer.finish(output(&args[3])?)?;
145    Ok(())
146}
examples/worksheet_views.rs (lines 48-51)
45fn run() -> Result<()> {
46    let args: Vec<_> = std::env::args().collect();
47    if args.len() != 5 {
48        return Err(crabxl::Error::new(
49            crabxl::ErrorKind::InvalidData,
50            "Usage: worksheet_views create|copy|edit SOURCE OUTPUT ROWS",
51        ));
52    }
53    let mut writer = WorkbookWriter::new(WriteOptions::default())?;
54    match args[1].as_str() {
55        "create" => {
56            let rows: u32 = args[4].parse().map_err(|error| {
57                crabxl::Error::caused_by(crabxl::ErrorKind::InvalidData, "Invalid row count", error)
58            })?;
59            let settings = views()?;
60            println!("VIEW_BYTES={}", settings.memory_bytes());
61            writer.start_sheet_with_views("Sheet", &settings)?;
62            for index in 0..rows {
63                writer.write_row(&Row {
64                    index: RowIndex::new(index)?,
65                    cells: vec![Cell {
66                        address: crabxl::CellAddress::new(index, 0)?,
67                        value: CellValue::Integer(i64::from(index)),
68                        style: StyleId::new(0),
69                    }],
70                })?;
71            }
72            writer.close_sheet()?;
73            println!("TEMP_BYTES={}", writer.stats().peak_temp_bytes);
74            writer.finish(BufWriter::new(File::create(&args[3]).map_err(|error| {
75                crabxl::Error::caused_by(crabxl::ErrorKind::Io, "Cannot create view fixture", error)
76            })?))?;
77        }
78        "copy" => {
79            let mut reader = WorkbookReader::open(&args[2])?;
80            let settings = reader.sheet_views("Sheet")?;
81            println!("VIEW_BYTES={}", settings.memory_bytes());
82            writer.start_sheet_with_views("Sheet", &settings)?;
83            let mut rows = reader.rows("Sheet")?;
84            while let Some(row) = rows.next_row()? {
85                writer.write_row(&row)?;
86            }
87            writer.close_sheet()?;
88            println!("TEMP_BYTES={}", writer.stats().peak_temp_bytes);
89            writer.finish(BufWriter::new(File::create(&args[3]).map_err(|error| {
90                crabxl::Error::caused_by(crabxl::ErrorKind::Io, "Cannot create view fixture", error)
91            })?))?;
92        }
93        "edit" => {
94            let mut editor = WorkbookEditor::open(&args[2])?;
95            editor.set_sheet_views("Sheet", views()?)?;
96            println!("VIEW_BYTES={}", editor.patch_bytes());
97            editor.save(
98                BufWriter::new(File::create(&args[3]).map_err(|error| {
99                    crabxl::Error::caused_by(
100                        crabxl::ErrorKind::Io,
101                        "Cannot create view fixture",
102                        error,
103                    )
104                })?),
105                SaveOptions::default(),
106            )?;
107            println!("TEMP_BYTES=0");
108        }
109        _ => {
110            return Err(crabxl::Error::new(
111                crabxl::ErrorKind::InvalidData,
112                "Unknown view operation",
113            ));
114        }
115    }
116    Ok(())
117}
Source

pub fn caused_by( kind: ErrorKind, message: impl Into<String>, source: impl Error + Send + Sync + 'static, ) -> Error

Preserve a concrete cause without exposing format-specific types in core.

Examples found in repository?
examples/printing.rs (lines 76-80)
74fn output(path: &str) -> Result<BufWriter<File>> {
75    File::create(path).map(BufWriter::new).map_err(|error| {
76        crabxl::Error::caused_by(
77            crabxl::ErrorKind::Io,
78            "Cannot create printing fixture",
79            error,
80        )
81    })
82}
83fn run() -> Result<()> {
84    let args: Vec<_> = std::env::args().collect();
85    if args.len() != 5 {
86        return Err(crabxl::Error::new(
87            crabxl::ErrorKind::InvalidData,
88            "Usage: printing create|copy|edit SOURCE OUTPUT ROWS",
89        ));
90    }
91    if args[1] == "edit" {
92        let mut editor = WorkbookEditor::open(&args[2])?;
93        editor.set_print_settings("Sheet", settings()?)?;
94        println!("PRINT_BYTES={}", editor.patch_bytes());
95        editor.save(output(&args[3])?, SaveOptions::default())?;
96        println!("TEMP_BYTES=0");
97        return Ok(());
98    }
99    if !matches!(args[1].as_str(), "create" | "copy") {
100        return Err(crabxl::Error::new(
101            crabxl::ErrorKind::InvalidData,
102            "Unknown printing operation",
103        ));
104    }
105    let mut reader = if args[1] == "copy" {
106        Some(WorkbookReader::open(&args[2])?)
107    } else {
108        None
109    };
110    let value = match &mut reader {
111        Some(reader) => reader.print_settings("Sheet")?,
112        None => settings()?,
113    };
114    println!("PRINT_BYTES={}", value.memory_bytes());
115    let mut writer = WorkbookWriter::new(WriteOptions::default())?;
116    writer.start_sheet_with_settings("Sheet", None, Some(&value))?;
117    if let Some(reader) = &mut reader {
118        let mut rows = reader.rows("Sheet")?;
119        while let Some(row) = rows.next_row()? {
120            writer.write_row(&row)?;
121        }
122    } else {
123        let count: u32 = args[4].parse().map_err(|error| {
124            crabxl::Error::caused_by(
125                crabxl::ErrorKind::InvalidData,
126                "Invalid printing row count",
127                error,
128            )
129        })?;
130        let mut row = Row::new(RowIndex::new(0)?);
131        for index in 0..count {
132            row.index = RowIndex::new(index)?;
133            row.cells.clear();
134            row.cells.push(Cell {
135                address: crabxl::CellAddress::new(index, 0)?,
136                value: CellValue::Integer(i64::from(index)),
137                style: StyleId::new(0),
138            });
139            writer.write_row(&row)?;
140        }
141    }
142    writer.close_sheet()?;
143    println!("TEMP_BYTES={}", writer.stats().peak_temp_bytes);
144    writer.finish(output(&args[3])?)?;
145    Ok(())
146}
More examples
Hide additional examples
examples/temporal_style_fixture.rs (line 61)
47    fn finish(self, path: String) -> crabxl::Result<()> {
48        let writer = match self {
49            Self::Streaming(mut writer) => {
50                writer.close_sheet()?;
51                writer
52            }
53            Self::Owned { book, .. } => {
54                eprintln!("BANK_BYTES {}", book.charged_bytes());
55                WorkbookWriter::from_workbook(WriteOptions::default(), book)?
56            }
57        };
58        eprintln!("STYLE_BYTES {}", writer.style_memory_bytes());
59        eprintln!("TEMP_BYTES {}", writer.stats().peak_temp_bytes);
60        writer.finish(std::fs::File::create(path).map_err(|error| {
61            crabxl::Error::caused_by(crabxl::ErrorKind::Io, "Cannot create fixture", error)
62        })?)?;
63        Ok(())
64    }
examples/support/printing_updates.rs (line 16)
4pub fn run(mut update: impl FnMut(&mut WorkbookEditor<File>, usize) -> Result<()>) -> Result<()> {
5    let mut arguments = std::env::args().skip(1);
6    let source = arguments
7        .next()
8        .ok_or_else(|| Error::new(ErrorKind::InvalidData, "Missing source"))?;
9    let target = arguments
10        .next()
11        .ok_or_else(|| Error::new(ErrorKind::InvalidData, "Missing target"))?;
12    let count = arguments
13        .next()
14        .ok_or_else(|| Error::new(ErrorKind::InvalidData, "Missing update count"))?
15        .parse::<usize>()
16        .map_err(|error| Error::caused_by(ErrorKind::InvalidData, "Invalid update count", error))?;
17    if count == 0 {
18        return Err(Error::new(
19            ErrorKind::InvalidData,
20            "Update count must be positive",
21        ));
22    }
23    let mut editor = WorkbookEditor::open(source)?;
24    for index in 0..count {
25        update(&mut editor, index)?;
26    }
27    println!("PRINT_BYTES={}", editor.patch_bytes());
28    let output = File::create(target)
29        .map_err(|error| Error::caused_by(ErrorKind::Io, "Cannot create output", error))?;
30    editor.save(BufWriter::new(output), SaveOptions::default())?;
31    println!("{count}");
32    Ok(())
33}
examples/worksheet_views.rs (line 57)
45fn run() -> Result<()> {
46    let args: Vec<_> = std::env::args().collect();
47    if args.len() != 5 {
48        return Err(crabxl::Error::new(
49            crabxl::ErrorKind::InvalidData,
50            "Usage: worksheet_views create|copy|edit SOURCE OUTPUT ROWS",
51        ));
52    }
53    let mut writer = WorkbookWriter::new(WriteOptions::default())?;
54    match args[1].as_str() {
55        "create" => {
56            let rows: u32 = args[4].parse().map_err(|error| {
57                crabxl::Error::caused_by(crabxl::ErrorKind::InvalidData, "Invalid row count", error)
58            })?;
59            let settings = views()?;
60            println!("VIEW_BYTES={}", settings.memory_bytes());
61            writer.start_sheet_with_views("Sheet", &settings)?;
62            for index in 0..rows {
63                writer.write_row(&Row {
64                    index: RowIndex::new(index)?,
65                    cells: vec![Cell {
66                        address: crabxl::CellAddress::new(index, 0)?,
67                        value: CellValue::Integer(i64::from(index)),
68                        style: StyleId::new(0),
69                    }],
70                })?;
71            }
72            writer.close_sheet()?;
73            println!("TEMP_BYTES={}", writer.stats().peak_temp_bytes);
74            writer.finish(BufWriter::new(File::create(&args[3]).map_err(|error| {
75                crabxl::Error::caused_by(crabxl::ErrorKind::Io, "Cannot create view fixture", error)
76            })?))?;
77        }
78        "copy" => {
79            let mut reader = WorkbookReader::open(&args[2])?;
80            let settings = reader.sheet_views("Sheet")?;
81            println!("VIEW_BYTES={}", settings.memory_bytes());
82            writer.start_sheet_with_views("Sheet", &settings)?;
83            let mut rows = reader.rows("Sheet")?;
84            while let Some(row) = rows.next_row()? {
85                writer.write_row(&row)?;
86            }
87            writer.close_sheet()?;
88            println!("TEMP_BYTES={}", writer.stats().peak_temp_bytes);
89            writer.finish(BufWriter::new(File::create(&args[3]).map_err(|error| {
90                crabxl::Error::caused_by(crabxl::ErrorKind::Io, "Cannot create view fixture", error)
91            })?))?;
92        }
93        "edit" => {
94            let mut editor = WorkbookEditor::open(&args[2])?;
95            editor.set_sheet_views("Sheet", views()?)?;
96            println!("VIEW_BYTES={}", editor.patch_bytes());
97            editor.save(
98                BufWriter::new(File::create(&args[3]).map_err(|error| {
99                    crabxl::Error::caused_by(
100                        crabxl::ErrorKind::Io,
101                        "Cannot create view fixture",
102                        error,
103                    )
104                })?),
105                SaveOptions::default(),
106            )?;
107            println!("TEMP_BYTES=0");
108        }
109        _ => {
110            return Err(crabxl::Error::new(
111                crabxl::ErrorKind::InvalidData,
112                "Unknown view operation",
113            ));
114        }
115    }
116    Ok(())
117}
Source

pub fn with_part(self, part: impl Into<String>) -> Error

Attach the affected archive part.

Source

pub fn with_cell(self, cell: CellAddress) -> Error

Attach the affected cell coordinate.

Source

pub fn kind(&self) -> ErrorKind

Return the matchable error category.

Source

pub fn part(&self) -> Option<&str>

Return the affected archive part, when known.

Source

pub fn cell(&self) -> Option<CellAddress>

Return the affected cell, when known.

Trait Implementations§

Source§

impl Debug for Error

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Display for Error

Source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result<(), Error>

Formats the value using the given formatter. Read more
Source§

impl Error for Error

Source§

fn source(&self) -> Option<&(dyn Error + 'static)>

Returns the lower-level source of this error, if any. Read more
1.0.0 · Source§

fn description(&self) -> &str

👎Deprecated since 1.42.0:

use the Display impl or to_string()

1.0.0 · Source§

fn cause(&self) -> Option<&dyn Error>

👎Deprecated since 1.33.0:

replaced by Error::source, which can support downcasting

Source§

fn provide<'a>(&'a self, request: &mut Request<'a>)

🔬This is a nightly-only experimental API. (error_generic_member_access)
Provides type-based access to context intended for error reports. Read more

Auto Trait Implementations§

§

impl !RefUnwindSafe for Error

§

impl !UnwindSafe for Error

§

impl Freeze for Error

§

impl Send for Error

§

impl Sync for Error

§

impl Unpin for Error

§

impl UnsafeUnpin for Error

Blanket Implementations§

Source§

impl<T> Any for T
where T: 'static + ?Sized,

Source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
Source§

impl<T> Borrow<T> for T
where T: ?Sized,

Source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
Source§

impl<T> BorrowMut<T> for T
where T: ?Sized,

Source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
Source§

impl<T> From<T> for T

Source§

fn from(t: T) -> T

Returns the argument unchanged.

Source§

impl<T, U> Into<U> for T
where U: From<T>,

Source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

Source§

impl<T> ToString for T
where T: Display + ?Sized,

Source§

fn to_string(&self) -> String

Converts the given value to a String. Read more
Source§

impl<T, U> TryFrom<U> for T
where U: Into<T>,

Source§

type Error = !

The type returned in the event of a conversion error.
Source§

fn try_from(value: U) -> Result<T, !>

Performs the conversion.
Source§

impl<T, U> TryInto<U> for T
where U: TryFrom<T>,

Source§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
Source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.