criware_utf_core/lib.rs
1//! The core mechanisms offered by `criware-utf`.
2//!
3//! Please do not use this on its own. Use the full `criware-utf` crate.
4//!
5
6use thiserror::Error;
7
8mod reader;
9mod schema;
10mod table;
11mod value;
12mod writer;
13
14pub use crate::reader::Reader;
15pub use crate::schema::{ColumnStorageFormat, Schema, SchemaColumn};
16pub use crate::table::Table;
17pub use crate::value::{Primitive, Value, ValueKind, utf_size_of};
18pub use crate::writer::{WriteContext, Writer};
19
20/// Error returned when reading or writing a table fails
21///
22#[derive(Debug, Error)]
23pub enum Error {
24 ///
25 /// If a data blob is not the correct size
26 ///
27 /// This is only used in the implementation of [`Value`] for `[u8; N]`
28 ///
29 #[error("wrong size")]
30 BlobWrongSize,
31 ///
32 /// If a string or data blob is unable to be read from a table
33 ///
34 /// This means the table is malformed
35 ///
36 #[error("string/blob not found")]
37 DataNotFound,
38 ///
39 /// If the entire content of the table is unable to be read from a stream
40 ///
41 #[error("reached end of file early (at {0})")]
42 EOF(String),
43 ///
44 /// If the flag associated with the column's storage method is invalid
45 /// (table is malformed)
46 ///
47 #[error("invalid column storage flag: 0x{0:02}")]
48 InvalidColumnStorage(u8),
49 ///
50 /// If the flag associated with the column's data type is invalid
51 /// (table is malformed)
52 ///
53 #[error("invalid column type flag: 0x{0:02}")]
54 InvalidColumnType(u8),
55 ///
56 /// If an I/O error happens
57 ///
58 /// This does not include end-of-file errors. There's a variant for that
59 /// specifically.
60 ///
61 #[error("i/o error: {0}")]
62 IOError(std::io::Error),
63 ///
64 /// Generic error for any malformed data in the header of a table
65 ///
66 #[error("malformed header")]
67 MalformedHeader,
68 ///
69 /// If a string stored in a table is unable to be decoded
70 ///
71 #[error("error when decoding utf8 string: {0}")]
72 StringMalformed(std::str::Utf8Error),
73 ///
74 /// Occurs when writing
75 ///
76 /// For a rowed optional value, the value in each row must ALL either be
77 /// `Some` or `None`. If this condition is violated, this error is
78 /// returned.
79 ///
80 #[error("optional column conflict: \"{0}\" (values must be all Some or all None)")]
81 OptionalColumnConflict(&'static str),
82 ///
83 /// If a conversion from a primitive to another value (or vice versa) fails
84 ///
85 #[error("failed to convert {0} to {1}: {2}")]
86 ValueConversion(&'static str, &'static str, Box<dyn std::error::Error>),
87 ///
88 /// If the name of a column is not what was expected
89 ///
90 /// This indicates the table doesn't follow the expected schema. The table
91 /// may still be perfectly valid.
92 ///
93 #[error("wrong column name: \"{0}\" (expected \"{1}\"")]
94 WrongColumnName(String, &'static str),
95 ///
96 /// If the type of data stored in a column is not what was expected.
97 ///
98 /// This indicates the table doesn't follow the expected schema. The table
99 /// may still be perfectly valid.
100 ///
101 #[error("wrong column type flag: 0x{0:02} (expected 0x{1:02})")]
102 WrongColumnType(u8, u8),
103 ///
104 /// If the method a column stores data is not what was expected
105 ///
106 /// This indicates the table doesn't follow the expected schema. The table
107 /// may still be perfectly valid.
108 ///
109 #[error("wrong column storage flag: 0x{0:02} (expected {1})")]
110 WrongColumnStorage(u8, &'static str),
111 ///
112 /// Generic error for a table not following a schema. The table may still
113 /// be valid.
114 ///
115 #[error("wrong table schema")]
116 WrongTableSchema,
117}
118
119/// A typedef of the result returned by much of the crate.
120///
121pub type Result<T> = std::result::Result<T, Error>;