Skip to content

Commit

Permalink
Rename and reorganize tauri commands
Browse files Browse the repository at this point in the history
  • Loading branch information
sebastinez committed Sep 26, 2024
1 parent 665c5a9 commit ddf44ce
Show file tree
Hide file tree
Showing 8 changed files with 119 additions and 109 deletions.
4 changes: 2 additions & 2 deletions src-tauri/src/commands.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
pub mod auth;
pub mod cobs;
pub mod cob;
pub mod profile;
pub mod repos;
pub mod repo;
pub mod thread;
51 changes: 51 additions & 0 deletions src-tauri/src/commands/cob.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,51 @@
pub mod issue;
pub mod patch;

mod query {
use serde::{Deserialize, Serialize};

use radicle::issue;
use radicle::patch;

#[derive(Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum IssueStatus {
Closed,
#[default]
Open,
All,
}

impl IssueStatus {
pub fn matches(&self, issue: &issue::State) -> bool {
match self {
Self::Open => matches!(issue, issue::State::Open),
Self::Closed => matches!(issue, issue::State::Closed { .. }),
Self::All => true,
}
}
}

#[derive(Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum PatchStatus {
#[default]
Open,
Draft,
Archived,
Merged,
All,
}

impl PatchStatus {
pub fn matches(&self, patch: &patch::State) -> bool {
match self {
Self::Open => matches!(patch, patch::State::Open { .. }),
Self::Draft => matches!(patch, patch::State::Draft),
Self::Archived => matches!(patch, patch::State::Archived),
Self::Merged => matches!(patch, patch::State::Merged { .. }),
Self::All => true,
}
}
}
}
50 changes: 50 additions & 0 deletions src-tauri/src/commands/cob/issue.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
use radicle::git::Oid;
use radicle::identity::RepoId;
use radicle::issue::cache::Issues;

use crate::cob::query;
use crate::error::Error;
use crate::types::cobs;
use crate::AppState;

#[tauri::command]
pub fn list_issues(
ctx: tauri::State<AppState>,
rid: RepoId,
status: query::IssueStatus,
) -> Result<Vec<cobs::Issue>, Error> {
let (repo, _) = ctx.repo(rid)?;
let issues = ctx.profile.issues(&repo)?;
let mut issues: Vec<_> = issues
.list()?
.filter_map(|r| {
let (id, issue) = r.ok()?;
(status.matches(issue.state())).then_some((id, issue))
})
.collect::<Vec<_>>();

issues.sort_by(|(_, a), (_, b)| b.timestamp().cmp(&a.timestamp()));
let aliases = &ctx.profile.aliases();
let issues = issues
.into_iter()
.map(|(id, issue)| cobs::Issue::new(id, issue, aliases))
.collect::<Vec<_>>();

Ok::<_, Error>(issues)
}

#[tauri::command]
pub fn issue_by_id(
ctx: tauri::State<AppState>,
rid: RepoId,
id: Oid,
) -> Result<Option<cobs::Issue>, Error> {
let (repo, _) = ctx.repo(rid)?;
let issues = ctx.profile.issues(&repo)?;
let issue = issues.get(&id.into())?;

let aliases = &ctx.profile.aliases();
let issue = issue.map(|issue| cobs::Issue::new(id.into(), issue, aliases));

Ok::<_, Error>(issue)
}
Original file line number Diff line number Diff line change
Expand Up @@ -3,55 +3,13 @@ use std::str::FromStr;
use radicle::cob::ObjectId;
use radicle::git::Oid;
use radicle::identity::RepoId;
use radicle::issue::cache::Issues;
use radicle::patch::cache::Patches;

use crate::cob::query;
use crate::error::Error;
use crate::types::cobs;
use crate::AppState;

#[tauri::command]
pub fn list_issues(
ctx: tauri::State<AppState>,
rid: RepoId,
status: query::IssueStatus,
) -> Result<Vec<cobs::Issue>, Error> {
let (repo, _) = ctx.repo(rid)?;
let issues = ctx.profile.issues(&repo)?;
let mut issues: Vec<_> = issues
.list()?
.filter_map(|r| {
let (id, issue) = r.ok()?;
(status.matches(issue.state())).then_some((id, issue))
})
.collect::<Vec<_>>();

issues.sort_by(|(_, a), (_, b)| b.timestamp().cmp(&a.timestamp()));
let aliases = &ctx.profile.aliases();
let issues = issues
.into_iter()
.map(|(id, issue)| cobs::Issue::new(id, issue, aliases))
.collect::<Vec<_>>();

Ok::<_, Error>(issues)
}

#[tauri::command]
pub fn issues_by_id(
ctx: tauri::State<AppState>,
rid: RepoId,
id: Oid,
) -> Result<Option<cobs::Issue>, Error> {
let (repo, _) = ctx.repo(rid)?;
let issues = ctx.profile.issues(&repo)?;
let issue = issues.get(&id.into())?;

let aliases = &ctx.profile.aliases();
let issue = issue.map(|issue| cobs::Issue::new(id.into(), issue, aliases));

Ok::<_, Error>(issue)
}

#[tauri::command]
pub fn list_patches(
ctx: tauri::State<AppState>,
Expand Down Expand Up @@ -79,7 +37,7 @@ pub fn list_patches(
}

#[tauri::command]
pub fn patches_by_id(
pub fn patch_by_id(
ctx: tauri::State<AppState>,
rid: RepoId,
id: String,
Expand Down Expand Up @@ -118,7 +76,7 @@ pub fn revisions_by_patch(
}

#[tauri::command]
pub fn revisions_by_id(
pub fn revision_by_patch_and_id(
ctx: tauri::State<AppState>,
rid: RepoId,
id: String,
Expand All @@ -137,52 +95,3 @@ pub fn revisions_by_id(
});
Ok::<_, Error>(revision)
}

mod query {
use serde::{Deserialize, Serialize};

use radicle::issue;
use radicle::patch;

#[derive(Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum IssueStatus {
Closed,
#[default]
Open,
All,
}

impl IssueStatus {
pub fn matches(&self, issue: &issue::State) -> bool {
match self {
Self::Open => matches!(issue, issue::State::Open),
Self::Closed => matches!(issue, issue::State::Closed { .. }),
Self::All => true,
}
}
}

#[derive(Default, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub enum PatchStatus {
#[default]
Open,
Draft,
Archived,
Merged,
All,
}

impl PatchStatus {
pub fn matches(&self, patch: &patch::State) -> bool {
match self {
Self::Open => matches!(patch, patch::State::Open { .. }),
Self::Draft => matches!(patch, patch::State::Draft),
Self::Archived => matches!(patch, patch::State::Archived),
Self::Merged => matches!(patch, patch::State::Merged { .. }),
Self::All => true,
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn repo_by_id(
}

#[tauri::command]
pub async fn diff(
pub async fn diff_stats(
ctx: tauri::State<'_, AppState>,
rid: RepoId,
base: String,
Expand Down
20 changes: 10 additions & 10 deletions src-tauri/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ use radicle::storage::git::Repository;
use radicle::storage::{ReadRepository, ReadStorage};
use radicle::Node;

use commands::{auth, cobs, profile, repos, thread};
use commands::{auth, cob, profile, repo, thread};
use types::repo::SupportedPayloads;

struct AppState {
Expand Down Expand Up @@ -138,17 +138,17 @@ pub fn run() {
.plugin(tauri_plugin_window_state::Builder::default().build())
.invoke_handler(tauri::generate_handler![
auth::authenticate,
repos::list_repos,
repos::repo_by_id,
repos::diff,
cobs::list_issues,
cobs::issues_by_id,
cobs::list_patches,
repo::list_repos,
repo::repo_by_id,
repo::diff_stats,
cob::issue::list_issues,
cob::issue::issue_by_id,
cob::patch::list_patches,
cob::patch::patch_by_id,
cob::patch::revisions_by_patch,
cob::patch::revision_by_patch_and_id,
thread::create_issue_comment,
thread::create_patch_comment,
cobs::patches_by_id,
cobs::revisions_by_patch,
cobs::revisions_by_id,
profile::config,
])
.run(tauri::generate_context!())
Expand Down
2 changes: 1 addition & 1 deletion src/components/PatchTeaser.svelte
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@
</div>
</div>
<div class="global-flex">
{#await invoke<Stats>( "diff", { rid, base: patch.base, head: patch.head }, ) then stats}
{#await invoke<Stats>( "diff_stats", { rid, base: patch.base, head: patch.head }, ) then stats}
<DiffStatBadge {stats} />
{/await}
{#each patch.labels as label}
Expand Down
2 changes: 1 addition & 1 deletion src/views/repo/router.ts
Original file line number Diff line number Diff line change
Expand Up @@ -111,7 +111,7 @@ export async function loadIssue(
rid: route.rid,
status: "all",
});
const issue: Issue = await invoke("issues_by_id", {
const issue: Issue = await invoke("issue_by_id", {
rid: route.rid,
id: route.issue,
});
Expand Down

0 comments on commit ddf44ce

Please sign in to comment.