浏览代码

Document errors

Johannes Hofmann 7 年前
父节点
当前提交
54145beb47
共有 3 个文件被更改,包括 26 次插入9 次删除
  1. 1
    2
      src/block.rs
  2. 24
    6
      src/error.rs
  3. 1
    1
      src/lib.rs

+ 1
- 2
src/block.rs 查看文件

370
 
370
 
371
 pub(crate) fn str_from_stringtable(block: &osmformat::PrimitiveBlock, index: usize) -> Result<&str> {
371
 pub(crate) fn str_from_stringtable(block: &osmformat::PrimitiveBlock, index: usize) -> Result<&str> {
372
     if let Some(vec) = block.get_stringtable().get_s().get(index) {
372
     if let Some(vec) = block.get_stringtable().get_s().get(index) {
373
-        //TODO at location to ErrorKind::Utf8
374
         std::str::from_utf8(vec)
373
         std::str::from_utf8(vec)
375
-            .map_err(|e| new_error(ErrorKind::Utf8(e)))
374
+            .map_err(|e| new_error(ErrorKind::StringtableUtf8{err: e, index}))
376
     } else {
375
     } else {
377
         Err(new_error(ErrorKind::StringtableIndexOutOfBounds{index}))
376
         Err(new_error(ErrorKind::StringtableIndexOutOfBounds{index}))
378
     }
377
     }

+ 24
- 6
src/error.rs 查看文件

43
 /// The specific type of an error.
43
 /// The specific type of an error.
44
 #[derive(Debug)]
44
 #[derive(Debug)]
45
 pub enum ErrorKind {
45
 pub enum ErrorKind {
46
+    /// An error for I/O operations.
46
     Io(io::Error),
47
     Io(io::Error),
48
+    /// An error that occurs when decoding a protobuf message.
47
     Protobuf{err: ProtobufError, location: &'static str},
49
     Protobuf{err: ProtobufError, location: &'static str},
48
-    Utf8(Utf8Error),
50
+    /// The stringtable contains an entry at `index` that could not be decoded to a valid UTF-8
51
+    /// string.
52
+    StringtableUtf8{err: Utf8Error, index: usize},
53
+    /// An element contains an out-of-bounds index to the stringtable.
49
     StringtableIndexOutOfBounds{index: usize},
54
     StringtableIndexOutOfBounds{index: usize},
55
+    /// An error that occurs when decoding `Blob`s.
50
     Blob(BlobError),
56
     Blob(BlobError),
51
 
57
 
52
     //TODO add UnexpectedPrimitiveBlock
58
     //TODO add UnexpectedPrimitiveBlock
63
 /// An error that occurs when decoding a blob.
69
 /// An error that occurs when decoding a blob.
64
 #[derive(Debug)]
70
 #[derive(Debug)]
65
 pub enum BlobError {
71
 pub enum BlobError {
72
+    /// Header size could not be decoded to a u32.
66
     InvalidHeaderSize,
73
     InvalidHeaderSize,
67
-    HeaderTooBig{size: u64},
68
-    MessageTooBig{size: u64},
74
+    /// Blob header is bigger than [`MAX_BLOB_HEADER_SIZE`](blob/MAX_BLOB_HEADER_SIZE.v.html).
75
+    HeaderTooBig{
76
+        /// Blob header size in bytes.
77
+        size: u64
78
+    },
79
+    /// Blob content is bigger than [`MAX_BLOB_MESSAGE_SIZE`](blob/MAX_BLOB_MESSAGE_SIZE.v.html).
80
+    MessageTooBig{
81
+        /// Blob content size in bytes.
82
+        size: u64
83
+    },
84
+    /// The blob is empty because the `raw` and `zlib-data` fields are missing.
69
     Empty,
85
     Empty,
70
     /// Hints that destructuring should not be exhaustive.
86
     /// Hints that destructuring should not be exhaustive.
71
     #[doc(hidden)]
87
     #[doc(hidden)]
90
         match *self.0 {
106
         match *self.0 {
91
             ErrorKind::Io(ref err) => err.description(),
107
             ErrorKind::Io(ref err) => err.description(),
92
             ErrorKind::Protobuf{ref err, ..} => err.description(),
108
             ErrorKind::Protobuf{ref err, ..} => err.description(),
93
-            ErrorKind::Utf8(ref err) => err.description(),
109
+            ErrorKind::StringtableUtf8{ref err, ..} => err.description(),
94
             ErrorKind::StringtableIndexOutOfBounds{..} => "stringtable index out of bounds",
110
             ErrorKind::StringtableIndexOutOfBounds{..} => "stringtable index out of bounds",
95
             ErrorKind::Blob(BlobError::InvalidHeaderSize) => "blob header size could not be decoded",
111
             ErrorKind::Blob(BlobError::InvalidHeaderSize) => "blob header size could not be decoded",
96
             ErrorKind::Blob(BlobError::HeaderTooBig{..}) => "blob header is too big",
112
             ErrorKind::Blob(BlobError::HeaderTooBig{..}) => "blob header is too big",
104
         match *self.0 {
120
         match *self.0 {
105
             ErrorKind::Io(ref err) => Some(err),
121
             ErrorKind::Io(ref err) => Some(err),
106
             ErrorKind::Protobuf{ref err, ..} => Some(err),
122
             ErrorKind::Protobuf{ref err, ..} => Some(err),
107
-            ErrorKind::Utf8(ref err) => Some(err),
123
+            ErrorKind::StringtableUtf8{ref err, ..} => Some(err),
108
             ErrorKind::StringtableIndexOutOfBounds{..} => None,
124
             ErrorKind::StringtableIndexOutOfBounds{..} => None,
109
             ErrorKind::Blob(BlobError::InvalidHeaderSize) => None,
125
             ErrorKind::Blob(BlobError::InvalidHeaderSize) => None,
110
             ErrorKind::Blob(BlobError::HeaderTooBig{..}) => None,
126
             ErrorKind::Blob(BlobError::HeaderTooBig{..}) => None,
122
             ErrorKind::Protobuf{ref err, location} => {
138
             ErrorKind::Protobuf{ref err, location} => {
123
                 write!(f, "protobuf error at '{}': {}", location, err)
139
                 write!(f, "protobuf error at '{}': {}", location, err)
124
             },
140
             },
125
-            ErrorKind::Utf8(ref err) => err.fmt(f),
141
+            ErrorKind::StringtableUtf8{ref err, index} => {
142
+                write!(f, "invalid UTF-8 at string table index {}: {}", index, err)
143
+            }
126
             ErrorKind::StringtableIndexOutOfBounds { index } => {
144
             ErrorKind::StringtableIndexOutOfBounds { index } => {
127
                 write!(f, "stringtable index out of bounds: {}", index)
145
                 write!(f, "stringtable index out of bounds: {}", index)
128
             },
146
             },

+ 1
- 1
src/lib.rs 查看文件

87
 pub use block::*;
87
 pub use block::*;
88
 pub use dense::*;
88
 pub use dense::*;
89
 pub use elements::*;
89
 pub use elements::*;
90
-pub use error::{Error, ErrorKind, Result};
90
+pub use error::{BlobError, Error, ErrorKind, Result};
91
 pub use mmap_blob::*;
91
 pub use mmap_blob::*;
92
 pub use reader::*;
92
 pub use reader::*;
93
 
93