Constructing an empty Thing should error ()

This commit is contained in:
Micha de Vries 2024-05-29 14:23:54 +02:00 committed by GitHub
parent 5cbe400e4c
commit f6fb0527f3
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
2 changed files with 45 additions and 7 deletions
core/src

View file

@ -1000,6 +1000,12 @@ pub enum Error {
#[error("This record access method does not allow signin")]
AccessRecordNoSignin,
/// Found a table name for the record but this is not a valid table
#[error("Found {value} for the Record ID but this is not a valid table name")]
TbInvalid {
value: String,
},
}
impl From<Error> for String {

View file

@ -95,8 +95,19 @@ pub fn table((val,): (Value,)) -> Result<Value, Error> {
}
pub fn thing((arg1, arg2): (Value, Option<Value>)) -> Result<Value, Error> {
Ok(if let Some(arg2) = arg2 {
Value::Thing(Thing {
match (arg1, arg2) {
// Empty table name
(Value::Strand(arg1), _) if arg1.is_empty() => Err(Error::TbInvalid {
value: arg1.as_string(),
}),
// Empty ID part
(_, Some(Value::Strand(arg2))) if arg2.is_empty() => Err(Error::IdInvalid {
value: arg2.as_string(),
}),
// Handle second argument
(arg1, Some(arg2)) => Ok(Value::Thing(Thing {
tb: arg1.as_string(),
id: match arg2 {
Value::Thing(v) => v.id,
@ -105,9 +116,10 @@ pub fn thing((arg1, arg2): (Value, Option<Value>)) -> Result<Value, Error> {
Value::Number(v) => v.into(),
v => v.as_string().into(),
},
})
} else {
match arg1 {
})),
// No second argument passed
(arg1, _) => Ok(match arg1 {
Value::Thing(v) => Ok(v),
Value::Strand(v) => Thing::try_from(v.as_str()).map_err(move |_| Error::ConvertTo {
from: Value::Strand(v),
@ -118,8 +130,8 @@ pub fn thing((arg1, arg2): (Value, Option<Value>)) -> Result<Value, Error> {
into: "record".into(),
}),
}?
.into()
})
.into()),
}
}
pub fn range(args: Vec<Value>) -> Result<Value, Error> {
@ -313,6 +325,7 @@ pub mod is {
#[cfg(test)]
mod tests {
use crate::err::Error;
use crate::sql::value::Value;
#[test]
@ -323,4 +336,23 @@ mod tests {
let value = super::is::array(("test".into(),)).unwrap();
assert_eq!(value, Value::Bool(false));
}
#[test]
fn no_empty_thing() {
let value = super::thing(("".into(), None));
let _expected = Error::TbInvalid {
value: "".into(),
};
if !matches!(value, Err(_expected)) {
panic!("An empty thing tb part should result in an error");
}
let value = super::thing(("table".into(), Some("".into())));
let _expected = Error::IdInvalid {
value: "".into(),
};
if !matches!(value, Err(_expected)) {
panic!("An empty thing id part should result in an error");
}
}
}