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
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
use std::{fmt::Display, sync::PoisonError};

use crate::annis::types::LineColumnRange;
use graphannis_core::{
    errors::{ComponentTypeError, GraphAnnisCoreError},
    types::NodeID,
};
use thiserror::Error;

use super::db::relannis::TextProperty;

pub type Result<T> = std::result::Result<T, GraphAnnisError>;

#[derive(Error, Debug, strum_macros::IntoStaticStr)]
#[non_exhaustive]
pub enum GraphAnnisError {
    #[error(transparent)]
    Core(#[from] GraphAnnisCoreError),
    #[error("{0}")]
    AQLSyntaxError(AQLError),
    #[error("{0}")]
    AQLSemanticError(AQLError),
    #[error("impossible search expression detected: {0}")]
    ImpossibleSearch(String),
    #[error("timeout")]
    Timeout,
    #[error("could not load graph {name} from disk")]
    LoadingGraphFailed { name: String },
    #[error("corpus {0} not found")]
    NoSuchCorpus(String),
    #[error("corpus {0} already exists.")]
    CorpusExists(String),
    #[error("could not get internal node ID for node {0}")]
    NoSuchNodeID(String),
    #[error("plan description missing")]
    PlanDescriptionMissing,
    #[error("plan cost missing")]
    PlanCostMissing,
    #[error("no execution node for component {0}")]
    NoExecutionNode(usize),
    #[error("no component for node #{0}")]
    NoComponentForNode(usize),
    #[error("LHS operand not found")]
    LHSOperandNotFound,
    #[error("RHS operand not found")]
    RHSOperandNotFound,
    #[error("Could not peek next element in index join: {0}")]
    PeekInIndexJoin(String),
    #[error(
        "frequency definition must consists of two parts: \
    the referenced node and the annotation name or \"tok\" separated by \":\""
    )]
    InvalidFrequencyDefinition,
    #[error(transparent)]
    CorpusStorage(#[from] CorpusStorageError),
    #[error(transparent)]
    RelAnnisImportError(#[from] RelAnnisError),
    #[error(transparent)]
    Io(#[from] std::io::Error),
    #[error(transparent)]
    TomlDeserializer(#[from] toml::de::Error),
    #[error(transparent)]
    TomlSerializer(#[from] toml::ser::Error),
    #[error(transparent)]
    Zip(#[from] zip::result::ZipError),
    #[error(transparent)]
    StripPathPrefix(#[from] std::path::StripPrefixError),
    #[error(transparent)]
    Csv(#[from] csv::Error),
    #[error(transparent)]
    ParseIntError(#[from] std::num::ParseIntError),
    #[error("Lock poisoning ({0})")]
    LockPoisoning(String),
}

impl<T> From<PoisonError<T>> for GraphAnnisError {
    fn from(e: PoisonError<T>) -> Self {
        Self::LockPoisoning(e.to_string())
    }
}

#[derive(Error, Debug)]
#[non_exhaustive]
pub enum CorpusStorageError {
    #[error("listing directories from {path} failed")]
    ListingDirectories {
        path: String,
        source: std::io::Error,
    },
    #[error("could not get directory entry for {path}")]
    DirectoryEntry {
        path: String,
        source: std::io::Error,
    },
    #[error("could not determine file type for {path}")]
    FileTypeDetection {
        path: String,
        source: std::io::Error,
    },
    #[error("loading corpus-config.toml for corpus {corpus} failed")]
    LoadingCorpusConfig {
        corpus: String,
        source: Box<dyn std::error::Error + Send + Sync>,
    },
    #[error("could not create corpus with name {corpus}")]
    CreateCorpus {
        corpus: String,
        source: GraphAnnisCoreError,
    },
    #[error("this format can only export one corpus but {0} corpora have been given as argument")]
    MultipleCorporaForSingleCorpusFormat(usize),
    #[error("error when removing existing files for corpus {corpus}")]
    RemoveFileForCorpus {
        corpus: String,
        source: std::io::Error,
    },
    #[error("could not lock corpus directory {path}")]
    LockCorpusDirectory {
        path: String,
        source: std::io::Error,
    },
    #[error("the corpus cache entry is not loaded")]
    CorpusCacheEntryNotLoaded,
}

#[derive(Error, Debug)]
#[non_exhaustive]
pub enum RelAnnisError {
    #[error("directory {0} not found")]
    DirectoryNotFound(String),
    #[error("missing column at position {pos} ({name}) in file {file}")]
    MissingColumn {
        pos: usize,
        name: String,
        file: String,
    },
    #[error("unexpected value NULL in column {pos} ({name}) in file {file} at line {}", line.map_or("<unkown>".to_string(), |l| l.to_string()))]
    UnexpectedNull {
        pos: usize,
        name: String,
        file: String,
        line: Option<u64>,
    },
    #[error("toplevel corpus not found")]
    ToplevelCorpusNotFound,
    #[error("corpus with ID {0} not found")]
    CorpusNotFound(u32),
    #[error("node with ID {0} not found")]
    NodeNotFound(NodeID),
    #[error("can't get find any aligned token (left or right) for node with ID {0}")]
    AlignedNotFound(NodeID),
    #[error("can't get left aligned token for node with ID {0}")]
    LeftAlignedNotFound(NodeID),
    #[error("can't get right aligned token for node with ID {0}")]
    RightAlignedNotFound(NodeID),
    #[error("can't get token ID for position {0:?}")]
    NoTokenForPosition(TextProperty),
    #[error("can't get left position of node {0}")]
    NoLeftPositionForNode(NodeID),
    #[error("can't get right position of node {0}")]
    NoRightPositionForNode(NodeID),
    #[error("invalid component type short name '{0}'")]
    InvalidComponentShortName(String),
}

#[derive(Debug, Serialize, Clone)]
pub struct AQLError {
    pub desc: String,
    pub location: Option<LineColumnRange>,
}

impl Display for AQLError {
    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
        if let Some(location) = &self.location {
            write!(f, "[{}] {}", location, self.desc)
        } else {
            write!(f, "{}", self.desc)
        }
    }
}

impl From<GraphAnnisError> for ComponentTypeError {
    fn from(e: GraphAnnisError) -> Self {
        ComponentTypeError(Box::new(e))
    }
}

#[macro_export]
macro_rules! try_as_boxed_iter {
    ($x:expr) => {
        match $x {
            Ok(v) => v,
            Err(e) => {
                return std::boxed::Box::new(std::iter::once(Err(e.into())));
            }
        }
    };
}

#[macro_export]
macro_rules! try_as_option {
    ($x:expr) => {
        match $x {
            Ok(v) => v,
            Err(e) => {
                return Some(Err(e.into()));
            }
        }
    };
}