Skip to content

Commit

Permalink
refactor: reduce usage of slicing in qr module
Browse files Browse the repository at this point in the history
  • Loading branch information
link2xt committed Nov 18, 2024
1 parent b300dbd commit 8b8e175
Showing 1 changed file with 17 additions and 13 deletions.
30 changes: 17 additions & 13 deletions src/qr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -367,9 +367,10 @@ pub fn format_backup(qr: &Qr) -> Result<String> {
/// scheme: `OPENPGP4FPR:FINGERPRINT#a=ADDR&n=NAME&i=INVITENUMBER&s=AUTH`
/// or: `OPENPGP4FPR:FINGERPRINT#a=ADDR&g=GROUPNAME&x=GROUPID&i=INVITENUMBER&s=AUTH`
/// or: `OPENPGP4FPR:FINGERPRINT#a=ADDR`
#[allow(clippy::indexing_slicing)]
async fn decode_openpgp(context: &Context, qr: &str) -> Result<Qr> {
let payload = &qr[OPENPGP4FPR_SCHEME.len()..];
let payload = qr
.strip_prefix(OPENPGP4FPR_SCHEME)
.context("Invalid OPENPGP4FPR scheme")?;

// macOS and iOS sometimes replace the # with %23 (uri encode it), we should be able to parse this wrong format too.
// see issue https://github.com/deltachat/deltachat-core-rust/issues/1969 for more info
Expand Down Expand Up @@ -545,8 +546,8 @@ async fn decode_ideltachat(context: &Context, prefix: &str, qr: &str) -> Result<
/// scheme: `DCACCOUNT:https://example.org/new_email?t=1w_7wDjgjelxeX884x96v3`
fn decode_account(qr: &str) -> Result<Qr> {
let payload = qr
.get(DCACCOUNT_SCHEME.len()..)
.context("invalid DCACCOUNT payload")?;
.strip_prefix(DCACCOUNT_SCHEME)
.context("Invalid DCACCOUNT payload")?;
let url = url::Url::parse(payload).context("Invalid account URL")?;
if url.scheme() == "http" || url.scheme() == "https" {
Ok(Qr::Account {
Expand All @@ -563,8 +564,8 @@ fn decode_account(qr: &str) -> Result<Qr> {
/// scheme: `DCWEBRTC:https://meet.jit.si/$ROOM`
fn decode_webrtc_instance(_context: &Context, qr: &str) -> Result<Qr> {
let payload = qr
.get(DCWEBRTC_SCHEME.len()..)
.context("invalid DCWEBRTC payload")?;
.strip_prefix(DCWEBRTC_SCHEME)
.context("Invalid DCWEBRTC payload")?;

let (_type, url) = Message::parse_webrtc_instance(payload);
let url = url::Url::parse(&url).context("Invalid WebRTC instance")?;
Expand Down Expand Up @@ -637,7 +638,7 @@ fn decode_shadowsocks_proxy(qr: &str) -> Result<Qr> {
fn decode_backup2(qr: &str) -> Result<Qr> {
let payload = qr
.strip_prefix(DCBACKUP2_SCHEME)
.ok_or_else(|| anyhow!("invalid DCBACKUP scheme"))?;
.context("Invalid DCBACKUP2 scheme")?;
let (auth_token, node_addr) = payload
.split_once('&')
.context("Backup QR code has no separator")?;
Expand Down Expand Up @@ -668,9 +669,10 @@ struct CreateAccountErrorResponse {
/// take a qr of the type DC_QR_ACCOUNT, parse it's parameters,
/// download additional information from the contained url and set the parameters.
/// on success, a configure::configure() should be able to log in to the account
#[allow(clippy::indexing_slicing)]
async fn set_account_from_qr(context: &Context, qr: &str) -> Result<()> {
let url_str = &qr[DCACCOUNT_SCHEME.len()..];
let url_str = qr
.strip_prefix(DCACCOUNT_SCHEME)
.context("Invalid DCACCOUNT scheme")?;

if !url_str.starts_with(HTTPS_SCHEME) {
bail!("DCACCOUNT QR codes must use HTTPS scheme");
Expand Down Expand Up @@ -800,9 +802,10 @@ pub async fn set_config_from_qr(context: &Context, qr: &str) -> Result<()> {
/// Extract address for the mailto scheme.
///
/// Scheme: `mailto:addr...?subject=...&body=..`
#[allow(clippy::indexing_slicing)]
async fn decode_mailto(context: &Context, qr: &str) -> Result<Qr> {
let payload = &qr[MAILTO_SCHEME.len()..];
let payload = qr
.strip_prefix(MAILTO_SCHEME)
.context("Invalid mailto: scheme")?;

let (addr, query) = if let Some(query_index) = payload.find('?') {
(&payload[..query_index], &payload[query_index + 1..])
Expand Down Expand Up @@ -855,9 +858,10 @@ async fn decode_mailto(context: &Context, qr: &str) -> Result<Qr> {
/// Extract address for the smtp scheme.
///
/// Scheme: `SMTP:addr...:subject...:body...`
#[allow(clippy::indexing_slicing)]
async fn decode_smtp(context: &Context, qr: &str) -> Result<Qr> {
let payload = &qr[SMTP_SCHEME.len()..];
let payload = qr
.strip_prefix(SMTP_SCHEME)
.context("Invalid SMTP scheme")?;

let addr = if let Some(query_index) = payload.find(':') {
&payload[..query_index]
Expand Down

0 comments on commit 8b8e175

Please sign in to comment.