Response to code review
This commit is contained in:
parent
65d64bbacf
commit
db948f1f67
|
@ -37,7 +37,7 @@ pub enum GolgiError {
|
||||||
/// JSON serialization or deserialization error.
|
/// JSON serialization or deserialization error.
|
||||||
SerdeJson(JsonError),
|
SerdeJson(JsonError),
|
||||||
/// Error decoding typed ssb message from content.
|
/// Error decoding typed ssb message from content.
|
||||||
ContentTypeDecode(String),
|
ContentType(String),
|
||||||
/// Error decoding UTF8 string from bytes
|
/// Error decoding UTF8 string from bytes
|
||||||
Utf8Parse {
|
Utf8Parse {
|
||||||
/// The underlying parse error.
|
/// The underlying parse error.
|
||||||
|
@ -56,7 +56,7 @@ impl std::error::Error for GolgiError {
|
||||||
GolgiError::Rpc(ref err) => Some(err),
|
GolgiError::Rpc(ref err) => Some(err),
|
||||||
GolgiError::Sbot(_) => None,
|
GolgiError::Sbot(_) => None,
|
||||||
GolgiError::SerdeJson(ref err) => Some(err),
|
GolgiError::SerdeJson(ref err) => Some(err),
|
||||||
GolgiError::ContentTypeDecode(_) => None,
|
GolgiError::ContentType(_) => None,
|
||||||
GolgiError::Utf8Parse{ ref source} => Some(source),
|
GolgiError::Utf8Parse{ ref source} => Some(source),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -77,7 +77,7 @@ impl std::fmt::Display for GolgiError {
|
||||||
GolgiError::Sbot(ref err) => write!(f, "Sbot returned an error response: {}", err),
|
GolgiError::Sbot(ref err) => write!(f, "Sbot returned an error response: {}", err),
|
||||||
GolgiError::SerdeJson(_) => write!(f, "Failed to serialize JSON slice"),
|
GolgiError::SerdeJson(_) => write!(f, "Failed to serialize JSON slice"),
|
||||||
//GolgiError::WhoAmI(ref err) => write!(f, "{}", err),
|
//GolgiError::WhoAmI(ref err) => write!(f, "{}", err),
|
||||||
GolgiError::ContentTypeDecode(ref err) => write!(
|
GolgiError::ContentType(ref err) => write!(
|
||||||
f,
|
f,
|
||||||
"Failed to decode typed message from ssb message content: {}",
|
"Failed to decode typed message from ssb message content: {}",
|
||||||
err
|
err
|
||||||
|
|
|
@ -36,32 +36,26 @@ pub enum SsbMessageContentType {
|
||||||
Vote,
|
Vote,
|
||||||
Post,
|
Post,
|
||||||
Contact,
|
Contact,
|
||||||
None
|
Unrecognized
|
||||||
}
|
}
|
||||||
|
|
||||||
impl SsbMessageValue {
|
impl SsbMessageValue {
|
||||||
|
|
||||||
/// Gets the type field of the message content as an enum, if found.
|
/// Gets the type field of the message content as an enum, if found.
|
||||||
/// if no type field is found, it returns a SsbMessageContentType::None
|
/// if no type field is found or the type field is not a string, it returns an Err(GolgiError::ContentType)
|
||||||
/// if a type field is found with an invalid format it returns a GolgiError::ContentTypeDecode
|
/// if a type field is found but with an unknown string it returns an Ok(SsbMessageContentType::Unrecognized)
|
||||||
pub fn get_message_type(&self) -> Result<SsbMessageContentType, GolgiError> {
|
pub fn get_message_type(&self) -> Result<SsbMessageContentType, GolgiError> {
|
||||||
let msg_type = self
|
let msg_type = self
|
||||||
.content
|
.content
|
||||||
.get("type");
|
.get("type")
|
||||||
let enum_type = match msg_type {
|
.ok_or(GolgiError::ContentType("type field not found".to_string()))?;
|
||||||
Some(mtype) => {
|
let mtype_str: &str = msg_type.as_str().ok_or(GolgiError::ContentType("type field value is not a string as expected".to_string()))?;
|
||||||
let mtype_str: &str = mtype.as_str().ok_or(GolgiError::ContentTypeDecode("type field with invalid format".to_string()))?;
|
let enum_type = match mtype_str {
|
||||||
match mtype_str {
|
"about" => SsbMessageContentType::About,
|
||||||
"about" => SsbMessageContentType::About,
|
"post" => SsbMessageContentType::Post,
|
||||||
"post" => SsbMessageContentType::Post,
|
"vote" => SsbMessageContentType::Vote,
|
||||||
"vote" => SsbMessageContentType::Vote,
|
"contact" => SsbMessageContentType::Contact,
|
||||||
"contact" => SsbMessageContentType::Contact,
|
_ => SsbMessageContentType::Unrecognized,
|
||||||
_ => return Err(GolgiError::ContentTypeDecode("type field with unknown value".to_string()))
|
|
||||||
}
|
|
||||||
},
|
|
||||||
None => {
|
|
||||||
SsbMessageContentType::None
|
|
||||||
}
|
|
||||||
};
|
};
|
||||||
Ok(enum_type)
|
Ok(enum_type)
|
||||||
}
|
}
|
||||||
|
|
43
src/sbot.rs
43
src/sbot.rs
|
@ -195,31 +195,24 @@ impl Sbot {
|
||||||
pub async fn get_latest_about_message(&mut self, ssb_id: &str, key: &str) -> Result<Option<String>, GolgiError> {
|
pub async fn get_latest_about_message(&mut self, ssb_id: &str, key: &str) -> Result<Option<String>, GolgiError> {
|
||||||
// vector of about messages with most recent at the front of the vector
|
// vector of about messages with most recent at the front of the vector
|
||||||
let about_messages = self.get_about_messages(ssb_id).await?;
|
let about_messages = self.get_about_messages(ssb_id).await?;
|
||||||
// iterate through the vector looking for an about message with the given key
|
// iterate through the vector looking for most recent about message with the given key
|
||||||
// the first one we find is the most recent
|
let latest_about = about_messages
|
||||||
for msg in about_messages {
|
.iter()
|
||||||
let value = msg.content.get(key);
|
// find the first msg that contains the field `key`
|
||||||
match value {
|
.find(|msg| msg.content.get(key).is_some())
|
||||||
Some(val) => {
|
// map the found msg (`Some(SsbMessageValue)`) to a `Some(Some(&Value))`
|
||||||
let val_as_str_option = val.as_str();
|
.map(|msg| msg.content.get(key))
|
||||||
match val_as_str_option {
|
// flatten `Some(Some(&Value))` into `Some(&Value)`
|
||||||
Some(val_as_str) => {
|
.flatten()
|
||||||
return Ok(Some(val_as_str.to_string()))
|
// map `Some(&Value)` to `Some(Some(&str))`
|
||||||
},
|
.map(|msg_val| msg_val.as_str())
|
||||||
None => {
|
// flatten `Some(Some(&str))` to `Some(&str)`
|
||||||
// if it is an improperly formatted field (not a string)
|
.flatten()
|
||||||
// then just continue
|
// map `Some(&str))` to `Some(String)`
|
||||||
continue
|
.map(|msg_str| msg_str.to_string());
|
||||||
}
|
|
||||||
}
|
// return value is either `Ok(Some(String))` or `Ok(None)`
|
||||||
},
|
Ok(latest_about)
|
||||||
None => {
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
// if no about message with given key was found, then return None
|
|
||||||
Ok(None)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn get_name(&mut self, ssb_id: &str) -> Result<Option<String>, GolgiError> {
|
pub async fn get_name(&mut self, ssb_id: &str) -> Result<Option<String>, GolgiError> {
|
||||||
|
|
|
@ -140,6 +140,8 @@ where
|
||||||
Ok(messages)
|
Ok(messages)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
/// Takes in an rpc request number, and a handling function,
|
/// Takes in an rpc request number, and a handling function,
|
||||||
/// and calls the handling function on all responses which match the request number,
|
/// and calls the handling function on all responses which match the request number,
|
||||||
/// and prints out the result of the handling function.
|
/// and prints out the result of the handling function.
|
||||||
|
|
Loading…
Reference in New Issue