Skip to content

Commit

Permalink
Add tag and clippy (#73)
Browse files Browse the repository at this point in the history
* add tag

* manual clippy

* manual clippy

* remove unused TYPE_NUMBER

* manual clippy

* manual clippy

* manual clippy

* cargo fmt
  • Loading branch information
Eason0729 authored Aug 24, 2024
1 parent d0efd20 commit a9b13dd
Show file tree
Hide file tree
Showing 40 changed files with 496 additions and 647 deletions.
13 changes: 7 additions & 6 deletions backend/src/controller/judger/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,9 +27,9 @@ pub enum Error {
#[error("judger temporarily unavailable")]
JudgerResourceExhausted,
#[error("`{0}`")]
JudgerError(Status),
Judger(Status),
#[error("`{0}`")]
JudgerProto(&'static str),
JudgerProtoChanged(&'static str),
#[error("payload.`{0}` is not a vaild argument")]
BadArgument(&'static str),
#[error("language not found")]
Expand All @@ -50,7 +50,7 @@ impl From<Status> for Error {
fn from(value: Status) -> Self {
match value.code() {
tonic::Code::ResourceExhausted => Error::JudgerResourceExhausted,
_ => Error::JudgerError(value),
_ => Error::Judger(value),
}
}
}
Expand All @@ -61,8 +61,8 @@ impl From<Error> for Status {
Error::JudgerResourceExhausted => Status::resource_exhausted("no available judger"),
Error::BadArgument(x) => Status::invalid_argument(format!("bad argument: {}", x)),
Error::LangNotFound => Status::not_found("languaue not found"),
Error::JudgerError(x) => report_internal!(info, "`{}`", x),
Error::JudgerProto(x) => report_internal!(info, "`{}`", x),
Error::Judger(x) => report_internal!(info, "`{}`", x),
Error::JudgerProtoChanged(x) => report_internal!(info, "`{}`", x),
Error::Database(x) => report_internal!(warn, "{}", x),
Error::TransportLayer(x) => report_internal!(info, "{}", x),
Error::RateLimit => Status::resource_exhausted("resource limit imposed by backend"),
Expand Down Expand Up @@ -92,6 +92,7 @@ impl From<Code> for SubmitStatus {
}
}

#[allow(dead_code)]
pub struct PlaygroundPayload {
pub input: Vec<u8>,
pub code: Vec<u8>,
Expand Down Expand Up @@ -138,7 +139,7 @@ impl Judger {
.next()
.in_current_span()
.await
.ok_or(Error::JudgerProto("Expected as many case as inputs"))??;
.ok_or(Error::JudgerProtoChanged("Expected as many case as inputs"))??;
total_memory += res.memory;
total_time += res.time;
total_score += score;
Expand Down
4 changes: 2 additions & 2 deletions backend/src/endpoint/announcement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,7 +26,7 @@ impl<'a> From<WithAuth<'a, Model>> for AnnouncementFullInfo {
}
}

impl<'a> WithAuthTrait for Model {}
impl WithAuthTrait for Model {}

impl From<Model> for AnnouncementInfo {
fn from(value: Model) -> Self {
Expand Down Expand Up @@ -276,7 +276,7 @@ impl Announcement for ArcServer {

contest.ok_or(Error::NotInDB)?;
let mut model = model.ok_or(Error::NotInDB)?.into_active_model();
if let ActiveValue::Set(Some(v)) = model.contest_id {
if let ActiveValue::Set(Some(_)) = model.contest_id {
return Err(Error::AlreadyExist("announcement already linked"));
}

Expand Down
2 changes: 1 addition & 1 deletion backend/src/endpoint/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ impl<'a> From<WithAuth<'a, Model>> for ChatInfo {
}
}

impl<'a> WithAuthTrait for Model {}
impl WithAuthTrait for Model {}

#[tonic::async_trait]
impl Chat for ArcServer {
Expand Down
2 changes: 1 addition & 1 deletion backend/src/endpoint/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@ mod user;

use crate::NonZeroU32;
use grpc::backend::{Id, Order, *};
use sea_orm::*;
use sea_orm::{Value, *};
use std::ops::Deref;
use tonic::*;
use tracing::*;
Expand Down
34 changes: 26 additions & 8 deletions backend/src/endpoint/problem.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,16 @@
use super::*;
use grpc::backend::problem_server::*;
use sea_orm::sea_query::Expr;
use std::sync::Arc;

use crate::entity::{contest, problem::Paginator, problem::*, testcase};
use crate::entity::{contest, problem::Paginator, problem::*};

impl<'a> From<WithAuth<'a, Model>> for ProblemFullInfo {
fn from(value: WithAuth<'a, Model>) -> Self {
let model = value.1;
let writable = Entity::writable(&model, value.0);
ProblemFullInfo {
content: model.content.clone(),
tags: model.tags.clone(),
tags: vec![],
difficulty: model.difficulty,
public: model.public,
time: model.time as u64,
Expand Down Expand Up @@ -64,6 +64,8 @@ impl Problem for ArcServer {
let start_from_end = create.order == Order::Descend as i32;
if let Some(text) = query.text {
Paginator::new_text(text, start_from_end)
} else if !query.tags.is_empty() {
Paginator::new_tag(query.tags.into_iter(), start_from_end)
} else if let Some(sort) = query.sort_by {
Paginator::new_sort(sort.try_into().unwrap_or_default(), start_from_end)
} else if let Some(parent) = query.contest_id {
Expand Down Expand Up @@ -129,16 +131,31 @@ impl Problem for ArcServer {
model.user_id = ActiveValue::Set(user_id);

fill_active_model!(
model, req.info, title, difficulty, time, memory, tags, content, match_rule, order
model, req.info, title, difficulty, time, memory, content, match_rule, order
);

let txn = self
.db
.begin_with_config(Some(IsolationLevel::ReadCommitted), None)
.await?;

let model = model
.save(self.db.deref())
.save(&txn)
.instrument(info_span!("save").or_current())
.await
.map_err(Into::<Error>::into)?;

let id = *model.id.as_ref();
let txn = Arc::new(txn);

let id = model.id.unwrap();

insert_tag(txn.clone(), req.info.tags.into_iter(), id).await?;

Arc::try_unwrap(txn)
.unwrap()
.commit()
.await
.map_err(|_| Error::Retry)?;

info!(count.problem = 1, id = id);

Expand Down Expand Up @@ -170,8 +187,9 @@ impl Problem for ArcServer {
.into_active_model();

fill_exist_active_model!(
model, req.info, title, difficulty, time, memory, tags, content, match_rule, order
model, req.info, title, difficulty, time, memory, content, match_rule, order
);
// FIXME: fill tag

model
.update(self.db.deref())
Expand Down Expand Up @@ -236,7 +254,7 @@ impl Problem for ArcServer {
let contest: contest::IdModel = contest.ok_or(Error::NotInDB)?;

let mut model = model.ok_or(Error::NotInDB)?.into_active_model();
if let ActiveValue::Set(Some(v)) = model.contest_id {
if let ActiveValue::Set(Some(_)) = model.contest_id {
return Err(Error::AlreadyExist("problem already linked"));
}

Expand Down
2 changes: 1 addition & 1 deletion backend/src/endpoint/testcase.rs
Original file line number Diff line number Diff line change
Expand Up @@ -198,7 +198,7 @@ impl Testcase for ArcServer {

let mut model = model.ok_or(Error::NotInDB)?.into_active_model();
// FIXME: use is_set(), be sure to know what Option<T> in sea_orm said
if let ActiveValue::Set(Some(v)) = model.problem_id {
if let ActiveValue::Set(Some(_)) = model.problem_id {
return Err(Error::AlreadyExist("testcase already linked"));
}

Expand Down
2 changes: 0 additions & 2 deletions backend/src/endpoint/token.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,8 +8,6 @@ use grpc::backend::token_server::*;
use crate::entity::token::{Paginator, *};
use crate::{entity::user, util::rate_limit::RateLimit};

const TOKEN_LIMIT: u64 = 16;

impl From<Model> for String {
fn from(value: Model) -> Self {
base64::Engine::encode(
Expand Down
8 changes: 0 additions & 8 deletions backend/src/entity/announcement.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,8 +117,6 @@ impl PagerData for PagerTrait {
impl Source for PagerTrait {
const ID: <Self::Entity as EntityTrait>::Column = Column::Id;
type Entity = Entity;
const TYPE_NUMBER: u8 = 4;

async fn filter(
auth: &Auth,
_data: &Self::Data,
Expand All @@ -140,8 +138,6 @@ impl PagerData for TextPagerTrait {
impl Source for TextPagerTrait {
const ID: <Self::Entity as EntityTrait>::Column = Column::Id;
type Entity = Entity;
const TYPE_NUMBER: u8 = 4;

async fn filter(
auth: &Auth,
data: &Self::Data,
Expand All @@ -163,8 +159,6 @@ impl PagerData for ParentPagerTrait {
impl Source for ParentPagerTrait {
const ID: <Self::Entity as EntityTrait>::Column = Column::Id;
type Entity = Entity;
const TYPE_NUMBER: u8 = 8;

async fn filter(
auth: &Auth,
data: &Self::Data,
Expand Down Expand Up @@ -202,8 +196,6 @@ impl PagerData for ColPagerTrait {
impl Source for ColPagerTrait {
const ID: <Self::Entity as EntityTrait>::Column = Column::Id;
type Entity = Entity;
const TYPE_NUMBER: u8 = 8;

async fn filter(
auth: &Auth,
_data: &Self::Data,
Expand Down
2 changes: 0 additions & 2 deletions backend/src/entity/chat.rs
Original file line number Diff line number Diff line change
Expand Up @@ -87,8 +87,6 @@ impl PagerData for ParentPagerTrait {
impl Source for ParentPagerTrait {
const ID: <Self::Entity as EntityTrait>::Column = Column::Id;
type Entity = Entity;
const TYPE_NUMBER: u8 = 8;

async fn filter(
auth: &Auth,
data: &Self::Data,
Expand Down
4 changes: 0 additions & 4 deletions backend/src/entity/contest.rs
Original file line number Diff line number Diff line change
Expand Up @@ -220,8 +220,6 @@ impl PagerData for TextPagerTrait {
impl Source for TextPagerTrait {
const ID: <Self::Entity as EntityTrait>::Column = Column::Id;
type Entity = Entity;
const TYPE_NUMBER: u8 = 4;

async fn filter(
auth: &Auth,
data: &Self::Data,
Expand All @@ -243,8 +241,6 @@ impl PagerData for ColPagerTrait {
impl Source for ColPagerTrait {
const ID: <Self::Entity as EntityTrait>::Column = Column::Id;
type Entity = Entity;
const TYPE_NUMBER: u8 = 8;

async fn filter(
auth: &Auth,
_data: &Self::Data,
Expand Down
6 changes: 0 additions & 6 deletions backend/src/entity/education.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,6 @@ impl PagerData for PagerTrait {
impl Source for PagerTrait {
const ID: <Self::Entity as EntityTrait>::Column = Column::Id;
type Entity = Entity;
const TYPE_NUMBER: u8 = 4;

async fn filter(
auth: &Auth,
_data: &Self::Data,
Expand All @@ -127,8 +125,6 @@ impl PagerData for TextPagerTrait {
impl Source for TextPagerTrait {
const ID: <Self::Entity as EntityTrait>::Column = Column::Id;
type Entity = Entity;
const TYPE_NUMBER: u8 = 4;

async fn filter(
auth: &Auth,
data: &Self::Data,
Expand All @@ -150,8 +146,6 @@ impl PagerData for ParentPagerTrait {
impl Source for ParentPagerTrait {
const ID: <Self::Entity as EntityTrait>::Column = Column::Id;
type Entity = Entity;
const TYPE_NUMBER: u8 = 8;

async fn filter(
auth: &Auth,
data: &Self::Data,
Expand Down
Loading

0 comments on commit a9b13dd

Please sign in to comment.