cargo clippy & fmt

This commit is contained in:
Mike Dilger 2024-07-11 17:12:46 +12:00
parent e57167dd05
commit 1fde129c05
4 changed files with 48 additions and 52 deletions

View File

@ -120,7 +120,6 @@ async fn main() -> Result<(), Error> {
target: "Client", target: "Client",
"{}: TLS accept: {}", hashed_peer, e "{}: TLS accept: {}", hashed_peer, e
); );
return;
} }
} }
}, },

View File

@ -193,9 +193,9 @@ async fn handle_http_request(
ref ioerror, ref ioerror,
)) => { )) => {
match ioerror.kind() { match ioerror.kind() {
std::io::ErrorKind::ConnectionReset | std::io::ErrorKind::ConnectionReset
std::io::ErrorKind::ConnectionAborted | | std::io::ErrorKind::ConnectionAborted
std::io::ErrorKind::UnexpectedEof => { | std::io::ErrorKind::UnexpectedEof => {
// no biggie. // no biggie.
msg = "Reset"; msg = "Reset";
} }

View File

@ -46,8 +46,7 @@ pub async fn check_auth(request: Request<Incoming>) -> Result<Value, Error> {
let event_bytes = BASE64_STANDARD.decode(base64event)?; let event_bytes = BASE64_STANDARD.decode(base64event)?;
// Authorization header base64 must decode to a nostr Event // Authorization header base64 must decode to a nostr Event
let mut buffer = Vec::with_capacity(base64event.len()); let mut buffer = vec![0; base64event.len()];
buffer.resize(base64event.len(), 0);
let (_size, event) = Event::from_json(&event_bytes, &mut buffer)?; let (_size, event) = Event::from_json(&event_bytes, &mut buffer)?;
// Nostr event must be valid // Nostr event must be valid

View File

@ -67,7 +67,7 @@ pub async fn handle(
StatusCode::INTERNAL_SERVER_ERROR, StatusCode::INTERNAL_SERVER_ERROR,
), ),
}; };
return respond(result, status); respond(result, status)
} }
} }
} }
@ -87,8 +87,7 @@ pub fn handle_inner(command: Value) -> Result<Option<Value>, Error> {
}; };
match &*method { match &*method {
"supportedmethods" => { "supportedmethods" => Ok(Some(json!({
return Ok(Some(json!({
"result": [ "result": [
"allowevent", "allowevent",
"allowpubkey", "allowpubkey",
@ -100,19 +99,18 @@ pub fn handle_inner(command: Value) -> Result<Option<Value>, Error> {
"listbannedpubkeys", "listbannedpubkeys",
"supportedmethods" "supportedmethods"
] ]
}))); }))),
}
// Pubkeys // Pubkeys
"banpubkey" => { "banpubkey" => {
let pk = get_pubkey_param(obj)?; let pk = get_pubkey_param(obj)?;
crate::mark_pubkey_approval(GLOBALS.store.get().unwrap(), pk, false)?; crate::mark_pubkey_approval(GLOBALS.store.get().unwrap(), pk, false)?;
return Ok(None); Ok(None)
} }
"allowpubkey" => { "allowpubkey" => {
let pk = get_pubkey_param(obj)?; let pk = get_pubkey_param(obj)?;
crate::mark_pubkey_approval(GLOBALS.store.get().unwrap(), pk, true)?; crate::mark_pubkey_approval(GLOBALS.store.get().unwrap(), pk, true)?;
return Ok(None); Ok(None)
} }
"listbannedpubkeys" => { "listbannedpubkeys" => {
let approvals = crate::dump_pubkey_approvals(GLOBALS.store.get().unwrap())?; let approvals = crate::dump_pubkey_approvals(GLOBALS.store.get().unwrap())?;
@ -126,9 +124,9 @@ pub fn handle_inner(command: Value) -> Result<Option<Value>, Error> {
} }
}) })
.collect(); .collect();
return Ok(Some(json!({ Ok(Some(json!({
"result": pubkeys "result": pubkeys
}))); })))
} }
"listallowedpubkeys" => { "listallowedpubkeys" => {
let approvals = crate::dump_pubkey_approvals(GLOBALS.store.get().unwrap())?; let approvals = crate::dump_pubkey_approvals(GLOBALS.store.get().unwrap())?;
@ -142,20 +140,20 @@ pub fn handle_inner(command: Value) -> Result<Option<Value>, Error> {
} }
}) })
.collect(); .collect();
return Ok(Some(json!({ Ok(Some(json!({
"result": pubkeys "result": pubkeys
}))); })))
} }
// Events // Events
"banevent" => { "banevent" => {
let id = get_id_param(obj)?; let id = get_id_param(obj)?;
crate::mark_event_approval(GLOBALS.store.get().unwrap(), id, false)?; crate::mark_event_approval(GLOBALS.store.get().unwrap(), id, false)?;
return Ok(None); Ok(None)
} }
"allowevent" => { "allowevent" => {
let id = get_id_param(obj)?; let id = get_id_param(obj)?;
crate::mark_event_approval(GLOBALS.store.get().unwrap(), id, true)?; crate::mark_event_approval(GLOBALS.store.get().unwrap(), id, true)?;
return Ok(None); Ok(None)
} }
"listbannedevents" => { "listbannedevents" => {
let approvals = crate::dump_event_approvals(GLOBALS.store.get().unwrap())?; let approvals = crate::dump_event_approvals(GLOBALS.store.get().unwrap())?;
@ -169,9 +167,9 @@ pub fn handle_inner(command: Value) -> Result<Option<Value>, Error> {
} }
}) })
.collect(); .collect();
return Ok(Some(json!({ Ok(Some(json!({
"result": ids "result": ids
}))); })))
} }
"listallowedevents" => { "listallowedevents" => {
let approvals = crate::dump_event_approvals(GLOBALS.store.get().unwrap())?; let approvals = crate::dump_event_approvals(GLOBALS.store.get().unwrap())?;
@ -185,30 +183,30 @@ pub fn handle_inner(command: Value) -> Result<Option<Value>, Error> {
} }
}) })
.collect(); .collect();
return Ok(Some(json!({ Ok(Some(json!({
"result": ids "result": ids
}))); })))
} }
"listeventsneedingmoderation" => return Err(ChorusError::NotImplemented.into()), "listeventsneedingmoderation" => Err(ChorusError::NotImplemented.into()),
// Kinds // Kinds
"allowkind" => return Err(ChorusError::NotImplemented.into()), "allowkind" => Err(ChorusError::NotImplemented.into()),
"disallowkind" => return Err(ChorusError::NotImplemented.into()), "disallowkind" => Err(ChorusError::NotImplemented.into()),
"listbannedkinds" => return Err(ChorusError::NotImplemented.into()), "listbannedkinds" => Err(ChorusError::NotImplemented.into()),
"listallowedkinds" => return Err(ChorusError::NotImplemented.into()), "listallowedkinds" => Err(ChorusError::NotImplemented.into()),
// IP addresses // IP addresses
"blockip" => return Err(ChorusError::NotImplemented.into()), "blockip" => Err(ChorusError::NotImplemented.into()),
"unblockip" => return Err(ChorusError::NotImplemented.into()), "unblockip" => Err(ChorusError::NotImplemented.into()),
"listblockedips" => return Err(ChorusError::NotImplemented.into()), "listblockedips" => Err(ChorusError::NotImplemented.into()),
// Config // Config
"changerelayname" => return Err(ChorusError::NotImplemented.into()), "changerelayname" => Err(ChorusError::NotImplemented.into()),
"changerelaydescription" => return Err(ChorusError::NotImplemented.into()), "changerelaydescription" => Err(ChorusError::NotImplemented.into()),
"changerelayicon" => return Err(ChorusError::NotImplemented.into()), "changerelayicon" => Err(ChorusError::NotImplemented.into()),
_ => return Err(ChorusError::NotImplemented.into()), _ => Err(ChorusError::NotImplemented.into()),
} }
} }
@ -218,12 +216,12 @@ fn get_pubkey_param(obj: &Map<String, Value>) -> Result<Pubkey, Error> {
.ok_or(ChorusError::BadRequest("Params field missing").into_err())? .ok_or(ChorusError::BadRequest("Params field missing").into_err())?
.as_array() .as_array()
.ok_or(ChorusError::BadRequest("Params not an array").into_err())? .ok_or(ChorusError::BadRequest("Params not an array").into_err())?
.get(0) .first()
.ok_or(ChorusError::BadRequest("Missing pubkey parameter").into_err())? .ok_or(ChorusError::BadRequest("Missing pubkey parameter").into_err())?
.as_str() .as_str()
.ok_or(ChorusError::BadRequest("Pubkey parameter is wrong type").into_err())?; .ok_or(ChorusError::BadRequest("Pubkey parameter is wrong type").into_err())?;
Ok(Pubkey::read_hex(pubkey_text.as_bytes()) Pubkey::read_hex(pubkey_text.as_bytes())
.map_err(|_| ChorusError::BadRequest("Pubkey could not be parsed").into_err())?) .map_err(|_| ChorusError::BadRequest("Pubkey could not be parsed").into_err())
} }
fn get_id_param(obj: &Map<String, Value>) -> Result<Id, Error> { fn get_id_param(obj: &Map<String, Value>) -> Result<Id, Error> {
@ -232,10 +230,10 @@ fn get_id_param(obj: &Map<String, Value>) -> Result<Id, Error> {
.ok_or(ChorusError::BadRequest("Params field missing").into_err())? .ok_or(ChorusError::BadRequest("Params field missing").into_err())?
.as_array() .as_array()
.ok_or(ChorusError::BadRequest("Params not an array").into_err())? .ok_or(ChorusError::BadRequest("Params not an array").into_err())?
.get(0) .first()
.ok_or(ChorusError::BadRequest("Missing ID parameter").into_err())? .ok_or(ChorusError::BadRequest("Missing ID parameter").into_err())?
.as_str() .as_str()
.ok_or(ChorusError::BadRequest("ID parameter is wrong type").into_err())?; .ok_or(ChorusError::BadRequest("ID parameter is wrong type").into_err())?;
Ok(Id::read_hex(id_text.as_bytes()) Id::read_hex(id_text.as_bytes())
.map_err(|_| ChorusError::BadRequest("ID could not be parsed").into_err())?) .map_err(|_| ChorusError::BadRequest("ID could not be parsed").into_err())
} }