Skip to content

Commit 98c30db

Browse files
committedFeb 26, 2024·
Introduce TryFromOperatorError
TryFrom implementations should return a specific error type, not just ().
1 parent e140d64 commit 98c30db

File tree

4 files changed

+22
-6
lines changed

4 files changed

+22
-6
lines changed
 

‎yash-syntax/CHANGELOG.md

+3
Original file line numberDiff line numberDiff line change
@@ -14,14 +14,17 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
1414
- `impl FromStr for parser::lex::Keyword`
1515
- `parser::lex::Operator::as_str`
1616
- `parser::lex::ParseOperatorError`
17+
- `parser::lex::TryFromOperatorError`
1718

1819
### Changed
1920

2021
- `parser::lex::Keyword::is_clause_delimiter` now `const` and `#[must_use]`
2122
- `parser::lex::Operator::is_clause_delimiter` now `const` and `#[must_use]`
2223
- `<parser::lex::Operator as FromStr>::Err` from `()` to `ParseOperatorError`
2324
- `<syntax::AndOr as FromStr>::Err` from `()` to `ParseOperatorError`
25+
- `<syntax::AndOr as TryFrom<parser::lex::Operator>>::Error` from `()` to `TryFromOperatorError`
2426
- `<syntax::RedirOp as FromStr>::Err` from `()` to `ParseOperatorError`
27+
- `<syntax::RedirOp as TryFrom<parser::lex::Operator>>::Error` from `()` to `TryFromOperatorError`
2528

2629
### Removed
2730

‎yash-syntax/src/parser/lex.rs

+1
Original file line numberDiff line numberDiff line change
@@ -43,6 +43,7 @@ pub use self::keyword::Keyword;
4343
pub use self::keyword::ParseKeywordError;
4444
pub use self::op::is_operator_char;
4545
pub use self::op::Operator;
46+
pub use self::op::TryFromOperatorError;
4647
pub use self::raw_param::is_portable_name_char;
4748
pub use self::raw_param::is_special_parameter_char;
4849
pub use self::token::is_token_delimiter_char;

‎yash-syntax/src/parser/lex/op.rs

+11
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ use crate::syntax::Word;
2626
use std::fmt;
2727
use std::future::Future;
2828
use std::pin::Pin;
29+
use thiserror::Error;
2930

3031
/// Operator token identifier.
3132
#[derive(Clone, Copy, Debug, Eq, PartialEq)]
@@ -376,6 +377,16 @@ impl Lexer<'_> {
376377
}
377378
}
378379

380+
/// Error value indicating an operand conversion failure
381+
#[derive(Clone, Debug, Eq, Error, PartialEq)]
382+
pub struct TryFromOperatorError {}
383+
384+
impl fmt::Display for TryFromOperatorError {
385+
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
386+
f.write_str("inconvertible operator")
387+
}
388+
}
389+
379390
#[cfg(test)]
380391
mod tests {
381392
use super::*;

‎yash-syntax/src/syntax.rs

+7-6
Original file line numberDiff line numberDiff line change
@@ -74,6 +74,7 @@
7474
//! with here-document contents included, you can use ... TODO TBD.
7575
7676
use crate::parser::lex::Operator;
77+
use crate::parser::lex::TryFromOperatorError;
7778
use crate::source::Location;
7879
use itertools::Itertools;
7980
use std::cell::OnceCell;
@@ -785,8 +786,8 @@ pub enum RedirOp {
785786
}
786787

787788
impl TryFrom<Operator> for RedirOp {
788-
type Error = ();
789-
fn try_from(op: Operator) -> Result<RedirOp, ()> {
789+
type Error = TryFromOperatorError;
790+
fn try_from(op: Operator) -> Result<RedirOp, TryFromOperatorError> {
790791
use Operator::*;
791792
use RedirOp::*;
792793
match op {
@@ -799,7 +800,7 @@ impl TryFrom<Operator> for RedirOp {
799800
GreaterAnd => Ok(FdOut),
800801
GreaterGreaterBar => Ok(Pipe),
801802
LessLessLess => Ok(String),
802-
_ => Err(()),
803+
_ => Err(TryFromOperatorError {}),
803804
}
804805
}
805806
}
@@ -1177,12 +1178,12 @@ pub enum AndOr {
11771178
}
11781179

11791180
impl TryFrom<Operator> for AndOr {
1180-
type Error = ();
1181-
fn try_from(op: Operator) -> Result<AndOr, ()> {
1181+
type Error = TryFromOperatorError;
1182+
fn try_from(op: Operator) -> Result<AndOr, TryFromOperatorError> {
11821183
match op {
11831184
Operator::AndAnd => Ok(AndOr::AndThen),
11841185
Operator::BarBar => Ok(AndOr::OrElse),
1185-
_ => Err(()),
1186+
_ => Err(TryFromOperatorError {}),
11861187
}
11871188
}
11881189
}

0 commit comments

Comments
 (0)
Please sign in to comment.