-
Notifications
You must be signed in to change notification settings - Fork 31
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
refactor: Refactor error in DataType #36
Changes from 4 commits
f7ce03a
715ffb3
2167dbd
3edf01e
d10fc4c
1791515
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -15,7 +15,7 @@ | |
// specific language governing permissions and limitations | ||
// under the License. | ||
|
||
use crate::error::Error; | ||
use crate::error::*; | ||
use crate::spec::DataField; | ||
use bitflags::bitflags; | ||
use serde::{Deserialize, Serialize}; | ||
|
@@ -225,13 +225,16 @@ impl BinaryType { | |
|
||
pub const DEFAULT_LENGTH: usize = 1; | ||
|
||
pub fn new(length: usize) -> Result<Self, &'static str> { | ||
pub fn new(length: usize) -> Result<Self, DataTypeError> { | ||
Self::with_nullable(true, length) | ||
} | ||
|
||
pub fn with_nullable(nullable: bool, length: usize) -> Result<Self, &'static str> { | ||
pub fn with_nullable(nullable: bool, length: usize) -> Result<Self, DataTypeError> { | ||
if length < Self::MIN_LENGTH { | ||
return Err("Binary string length must be at least 1."); | ||
return DataTypeInvalidSnafu { | ||
message: "Binary string length must be at least 1.".to_string(), | ||
} | ||
.fail(); | ||
} | ||
Ok(Self { nullable, length }) | ||
} | ||
|
@@ -320,13 +323,16 @@ impl CharType { | |
|
||
pub const MAX_LENGTH: usize = 255; | ||
|
||
pub fn new(length: usize) -> Result<Self, &'static str> { | ||
pub fn new(length: usize) -> Result<Self, DataTypeError> { | ||
Self::with_nullable(true, length) | ||
} | ||
|
||
pub fn with_nullable(nullable: bool, length: usize) -> Result<Self, &'static str> { | ||
pub fn with_nullable(nullable: bool, length: usize) -> Result<Self, DataTypeError> { | ||
if !(Self::MIN_LENGTH..=Self::MAX_LENGTH).contains(&length) { | ||
return Err("Character string length must be between 1 and 255 (both inclusive)."); | ||
return DataTypeInvalidSnafu { | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Can we simplify this by The same as below There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more.
This simplifies things a bit, but the readability of the code is reduced. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ok, make sense |
||
message: "Char string length must be between 1 and 255.".to_string(), | ||
} | ||
.fail(); | ||
} | ||
Ok(CharType { nullable, length }) | ||
} | ||
|
@@ -420,25 +426,35 @@ impl DecimalType { | |
|
||
pub const DEFAULT_SCALE: u32 = 0; | ||
|
||
pub fn new(precision: u32, scale: u32) -> Result<Self, String> { | ||
pub fn new(precision: u32, scale: u32) -> Result<Self, DataTypeError> { | ||
Self::with_nullable(true, precision, scale) | ||
} | ||
|
||
pub fn with_nullable(nullable: bool, precision: u32, scale: u32) -> Result<Self, String> { | ||
pub fn with_nullable( | ||
nullable: bool, | ||
precision: u32, | ||
scale: u32, | ||
) -> Result<Self, DataTypeError> { | ||
if !(Self::MIN_PRECISION..=Self::MAX_PRECISION).contains(&precision) { | ||
return Err(format!( | ||
"Decimal precision must be between {} and {} (both inclusive).", | ||
Self::MIN_PRECISION, | ||
Self::MAX_PRECISION | ||
)); | ||
return DataTypeInvalidSnafu { | ||
message: format!( | ||
"Decimal precision must be between {} and {} (both inclusive).", | ||
Self::MIN_PRECISION, | ||
Self::MAX_PRECISION | ||
), | ||
} | ||
.fail(); | ||
} | ||
|
||
if !(Self::MIN_SCALE..=precision).contains(&scale) { | ||
return Err(format!( | ||
"Decimal scale must be between {} and the precision {} (both inclusive).", | ||
Self::MIN_SCALE, | ||
precision | ||
)); | ||
return DataTypeInvalidSnafu { | ||
message: format!( | ||
"Decimal scale must be between {} and {} (both inclusive).", | ||
Self::MIN_SCALE, | ||
precision | ||
), | ||
} | ||
.fail(); | ||
} | ||
|
||
Ok(DecimalType { | ||
|
@@ -616,17 +632,20 @@ impl LocalZonedTimestampType { | |
|
||
pub const DEFAULT_PRECISION: u32 = TimestampType::DEFAULT_PRECISION; | ||
|
||
pub fn new(precision: u32) -> Result<Self, String> { | ||
pub fn new(precision: u32) -> Result<Self, DataTypeError> { | ||
Self::with_nullable(true, precision) | ||
} | ||
|
||
pub fn with_nullable(nullable: bool, precision: u32) -> Result<Self, String> { | ||
pub fn with_nullable(nullable: bool, precision: u32) -> Result<Self, DataTypeError> { | ||
if !(Self::MIN_PRECISION..=Self::MAX_PRECISION).contains(&precision) { | ||
return Err(format!( | ||
"Timestamp precision must be between {} and {} (both inclusive).", | ||
Self::MIN_PRECISION, | ||
Self::MAX_PRECISION | ||
)); | ||
return DataTypeInvalidSnafu { | ||
message: format!( | ||
"LocalZonedTimestamp precision must be between {} and {} (both inclusive).", | ||
Self::MIN_PRECISION, | ||
Self::MAX_PRECISION | ||
), | ||
} | ||
.fail(); | ||
} | ||
|
||
Ok(LocalZonedTimestampType { | ||
|
@@ -725,17 +744,20 @@ impl TimeType { | |
|
||
pub const DEFAULT_PRECISION: u32 = 0; | ||
|
||
pub fn new(precision: u32) -> Result<Self, String> { | ||
pub fn new(precision: u32) -> Result<Self, DataTypeError> { | ||
Self::with_nullable(true, precision) | ||
} | ||
|
||
pub fn with_nullable(nullable: bool, precision: u32) -> Result<Self, String> { | ||
pub fn with_nullable(nullable: bool, precision: u32) -> Result<Self, DataTypeError> { | ||
if !(Self::MIN_PRECISION..=Self::MAX_PRECISION).contains(&precision) { | ||
return Err(format!( | ||
"Time precision must be between {} and {} (both inclusive).", | ||
Self::MIN_PRECISION, | ||
Self::MAX_PRECISION | ||
)); | ||
return DataTypeInvalidSnafu { | ||
message: format!( | ||
"Time precision must be between {} and {} (both inclusive).", | ||
Self::MIN_PRECISION, | ||
Self::MAX_PRECISION | ||
), | ||
} | ||
.fail(); | ||
} | ||
|
||
Ok(TimeType { | ||
|
@@ -787,17 +809,20 @@ impl TimestampType { | |
|
||
pub const DEFAULT_PRECISION: u32 = 6; | ||
|
||
pub fn new(precision: u32) -> Result<Self, String> { | ||
pub fn new(precision: u32) -> Result<Self, DataTypeError> { | ||
Self::with_nullable(true, precision) | ||
} | ||
|
||
pub fn with_nullable(nullable: bool, precision: u32) -> Result<Self, String> { | ||
pub fn with_nullable(nullable: bool, precision: u32) -> Result<Self, DataTypeError> { | ||
if !(Self::MIN_PRECISION..=Self::MAX_PRECISION).contains(&precision) { | ||
return Err(format!( | ||
"Timestamp precision must be between {} and {} (both inclusive).", | ||
Self::MIN_PRECISION, | ||
Self::MAX_PRECISION | ||
)); | ||
return DataTypeInvalidSnafu { | ||
message: format!( | ||
"Timestamp precision must be between {} and {} (both inclusive).", | ||
Self::MIN_PRECISION, | ||
Self::MAX_PRECISION | ||
), | ||
} | ||
.fail(); | ||
} | ||
|
||
Ok(TimestampType { | ||
|
@@ -892,13 +917,16 @@ impl VarBinaryType { | |
|
||
pub const DEFAULT_LENGTH: u32 = 1; | ||
|
||
pub fn new(length: u32) -> Result<Self, String> { | ||
pub fn new(length: u32) -> Result<Self, DataTypeError> { | ||
Self::try_new(true, length) | ||
} | ||
|
||
pub fn try_new(nullable: bool, length: u32) -> Result<Self, String> { | ||
pub fn try_new(nullable: bool, length: u32) -> Result<Self, DataTypeError> { | ||
if length < Self::MIN_LENGTH { | ||
return Err("Binary string length must be at least 1.".to_string()); | ||
return DataTypeInvalidSnafu { | ||
message: "VarBinary string length must be at least 1.".to_string(), | ||
} | ||
.fail(); | ||
} | ||
|
||
Ok(VarBinaryType { nullable, length }) | ||
|
@@ -947,17 +975,20 @@ impl VarCharType { | |
|
||
pub const DEFAULT_LENGTH: u32 = 1; | ||
|
||
pub fn new(length: u32) -> Result<Self, String> { | ||
pub fn new(length: u32) -> Result<Self, DataTypeError> { | ||
Self::with_nullable(true, length) | ||
} | ||
|
||
pub fn with_nullable(nullable: bool, length: u32) -> Result<Self, String> { | ||
pub fn with_nullable(nullable: bool, length: u32) -> Result<Self, DataTypeError> { | ||
if !(Self::MIN_LENGTH..=Self::MAX_LENGTH).contains(&length) { | ||
return Err(format!( | ||
"Character string length must be between {} and {} (both inclusive).", | ||
Self::MIN_LENGTH, | ||
Self::MAX_LENGTH | ||
)); | ||
return DataTypeInvalidSnafu { | ||
message: format!( | ||
"VarChar string length must be between {} and {} (both inclusive).", | ||
Self::MIN_LENGTH, | ||
Self::MAX_LENGTH | ||
), | ||
} | ||
.fail(); | ||
} | ||
|
||
Ok(VarCharType { nullable, length }) | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Hi, we usually expose only one error in each crate to keep them user-friendly. I will suggest to add
DataTypeInvalid
as a new enum inError
directly. In this way, we can usepaimon::Error
everywhere.