Skip to content

Commit

Permalink
add ticketid NewType
Browse files Browse the repository at this point in the history
  • Loading branch information
va1ha11a committed Jul 1, 2024
1 parent f3887e4 commit a0a2128
Show file tree
Hide file tree
Showing 3 changed files with 36 additions and 9 deletions.
2 changes: 1 addition & 1 deletion repopt/src/actions.rs
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ pub(super) fn show_ticket(id: String) -> Result<()> {
println!("Showing a ticket with id: {id}");
let in_repo_db = in_repo_db::collect_in_repo_db();
let in_repo_db = in_repo_db?;
let ticket = in_repo_db.get_ticket(&id);
let ticket = in_repo_db.get_ticket(id.try_into()?);
if let Some(ticket) = ticket {
println!("{:#?}", ticket);
} else {
Expand Down
4 changes: 2 additions & 2 deletions repopt/src/in_repo_db.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
use crate::in_repo_db_structs::{InRepoDB, Project, Ticket};
use crate::in_repo_db_structs::{InRepoDB, Project, Ticket, TicketId};
use crate::toml_utils;

use std::collections::HashMap;
Expand All @@ -24,7 +24,7 @@ pub(super) fn collect_in_repo_db() -> Result<InRepoDB> {
Ok(InRepoDB::new(projects, tickets))
}

fn collect_tickets(ticket_path: PathBuf) -> Result<HashMap<String, Ticket>> {
fn collect_tickets(ticket_path: PathBuf) -> Result<HashMap<TicketId, Ticket>> {
let tickets: HashMap<_, _> = toml_utils::get_toml_files_in_dir(&ticket_path)?
.into_iter()
.map(|ticket_file| -> Result<_> {
Expand Down
39 changes: 33 additions & 6 deletions repopt/src/in_repo_db_structs.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,10 @@
use clap::ValueEnum;
use derive_more::Display;
use serde::Deserialize;
use serde::{Deserialize, Serialize};
use serde_json::Value;
use std::collections::HashMap;
type Error = Box<dyn std::error::Error>; // replace this with set error types for production code.
type Result<T> = std::result::Result<T, Error>;

#[allow(dead_code)]
#[derive(Deserialize, Debug)]
Expand Down Expand Up @@ -43,10 +45,35 @@ pub(crate) enum TicketType {
Other,
}

#[derive(Debug, Deserialize, Serialize, PartialEq, Eq, Clone, Hash)]
pub(super) struct TicketId(String);

// Implement TryFrom<String> for TicketId
impl TryFrom<String> for TicketId {
type Error = Error;
fn try_from(value: String) -> Result<Self> {
match value.parse() {
Ok(id) => Ok(TicketId(id)),
Err(_) => Err(Error::from("Invalid Ticket ID Format")),
}
}
}

impl TryFrom<&str> for TicketId {
type Error = Error;
fn try_from(value: &str) -> Result<Self> {
match value.parse() {
Ok(id) => Ok(TicketId(id)),
Err(_) => Err(Error::from("Invalid Ticket ID Format")),
}
}
}

#[derive(TypedBuilder)]
#[allow(dead_code)]
#[derive(Deserialize, Debug)]
pub(crate) struct Ticket {
pub(crate) id: String,
pub(crate) id: TicketId,
title: String,
status: TicketStatus,
#[serde(rename = "type")]
Expand All @@ -72,21 +99,21 @@ impl Ticket {
#[derive(Deserialize, Debug)]
pub(crate) struct InRepoDB {
projects: HashMap<String, Project>,
tickets: HashMap<String, Ticket>,
tickets: HashMap<TicketId, Ticket>,
}

#[allow(dead_code)]
impl InRepoDB {
pub fn new(projects: HashMap<String, Project>, tickets: HashMap<String, Ticket>) -> Self {
pub fn new(projects: HashMap<String, Project>, tickets: HashMap<TicketId, Ticket>) -> Self {
InRepoDB { projects, tickets }
}

pub fn get_project(&self, id: &str) -> Option<&Project> {
self.projects.get(id)
}

pub fn get_ticket(&self, id: &str) -> Option<&Ticket> {
self.tickets.get(id)
pub fn get_ticket(&self, id: TicketId) -> Option<&Ticket> {
self.tickets.get(&id)
}

pub fn iter_tickets(&self) -> impl Iterator<Item = &Ticket> {
Expand Down

0 comments on commit a0a2128

Please sign in to comment.