Skip to content

Commit

Permalink
Merge pull request #239 from Virv12/fix-st-deps
Browse files Browse the repository at this point in the history
fix TaskYAML serialization
  • Loading branch information
Virv12 authored Apr 6, 2024
2 parents bd77316 + dd1a94f commit 03e9b44
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 53 deletions.
56 changes: 18 additions & 38 deletions task-maker-format/src/ioi/format/italian_yaml/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ use std::sync::Arc;

use anyhow::{anyhow, bail, Context, Error};
use itertools::Itertools;
use serde::{Deserialize, Deserializer, Serialize, Serializer};
use serde::{Deserialize, Serialize, Serializer};
use unic::normal::StrNormalForm;
use unic::ucd::category::GeneralCategory;

Expand Down Expand Up @@ -315,6 +315,21 @@ pub const VALID_SUBTASK_NAME_CHARACTER_CATEGORIES: &[GeneralCategory] = &[
GeneralCategory::OtherSymbol,
];

#[allow(clippy::trivially_copy_pass_by_ref)]
fn cms_serialize_score_type<S>(
val: &Option<TestcaseScoreAggregator>,
ser: S,
) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
match val {
Some(TestcaseScoreAggregator::Sum) => ser.serialize_str("Sum"),
Some(TestcaseScoreAggregator::Min) => ser.serialize_str("GroupMin"),
None => ser.serialize_none(),
}
}

/// Deserialized data from the task.yaml of a IOI format task.
#[derive(Debug, Serialize, Deserialize)]
struct TaskYAML {
Expand All @@ -325,6 +340,7 @@ struct TaskYAML {
#[serde(alias = "nome")]
pub title: String,
/// The score type to use for this task.
#[serde(serialize_with = "cms_serialize_score_type")]
pub score_type: Option<TestcaseScoreAggregator>,
/// The parameters of the score type.
#[serde(skip_serializing_if = "Option::is_none")]
Expand All @@ -341,9 +357,7 @@ struct TaskYAML {
pub memory_limit: Option<u64>,

/// Whether this is an output only task. Defaults to false.
#[serde(default = "bool::default")]
#[serde(serialize_with = "python_bool_serializer")]
#[serde(deserialize_with = "python_bool_deserializer")]
#[serde(default)]
pub output_only: bool,
/// The input file for the solutions, usually 'input.txt' or '' (stdin). Defaults to `''`.
#[serde(default = "default_infile")]
Expand Down Expand Up @@ -833,40 +847,6 @@ fn parse_communication_task_data(
})))
}

/// Serializer of a boolean using the python syntax:
/// - `true` -> `True`
/// - `false` -> `False`
#[allow(clippy::trivially_copy_pass_by_ref)]
fn python_bool_serializer<S>(val: &bool, ser: S) -> Result<S::Ok, S::Error>
where
S: Serializer,
{
if *val {
ser.serialize_str("True")
} else {
ser.serialize_str("False")
}
}

/// Deserializer of a boolean using the python syntax:
/// - `True` -> `true`
/// - `False` -> `false`
/// - other -> error
fn python_bool_deserializer<'de, D>(deser: D) -> Result<bool, D::Error>
where
D: Deserializer<'de>,
{
use serde::de::Error;
let val = String::deserialize(deser)?;
if val == "True" {
Ok(true)
} else if val == "False" {
Ok(false)
} else {
Err(Error::custom("invalid bool, either True or False"))
}
}

/// The default value for the `infile` field of task.yaml.
fn default_infile() -> String {
"input.txt".into()
Expand Down
16 changes: 1 addition & 15 deletions task-maker-format/src/ioi/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,10 +18,9 @@
use std::collections::HashMap;
use std::fmt::Debug;
use std::path::{Path, PathBuf};
use std::str::FromStr;
use std::sync::{Arc, Mutex};

use anyhow::{bail, Context, Error};
use anyhow::{Context, Error};
use itertools::Itertools;
use serde::{Deserialize, Serialize};
use typescript_definitions::TypeScriptify;
Expand Down Expand Up @@ -547,19 +546,6 @@ impl TestcaseInfo {
}
}

impl FromStr for TestcaseScoreAggregator {
type Err = Error;

fn from_str(s: &str) -> Result<Self, Self::Err> {
match s {
"min" | "GroupMin" => Ok(TestcaseScoreAggregator::Min),
"sum" | "Sum" => Ok(TestcaseScoreAggregator::Sum),
"GroupMul" | "GroupThreshold" => bail!("{s} is not supported yet"),
_ => bail!("Invalid testcase score aggregator: {}", s),
}
}
}

impl ScoreManager {
/// Make a new `ScoreManager` based on the subtasks and testcases of the specified task.
pub fn new(
Expand Down

0 comments on commit 03e9b44

Please sign in to comment.