use std::fmt;
use serde::{Deserialize, Serialize};
use unitype_core::canon::TypeId;
use unitype_core::{BigO, Ident, ImplProfile, Metadata, QualifiedName};
#[derive(Clone, Debug, PartialEq, Eq, Hash, PartialOrd, Ord, Serialize, Deserialize)]
#[serde(transparent)]
pub struct ImplId(pub String);
impl ImplId {
pub fn new(id: impl Into<String>) -> Self {
Self(id.into())
}
pub fn as_str(&self) -> &str {
&self.0
}
}
impl fmt::Display for ImplId {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
f.write_str(&self.0)
}
}
impl From<&str> for ImplId {
fn from(s: &str) -> Self {
Self::new(s)
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Implementation {
pub id: ImplId,
pub proves: QualifiedName,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub sig_hash: Option<TypeId>,
pub environment: Ident,
pub entry: String,
pub cost: ImplProfile,
#[serde(default, skip_serializing_if = "Option::is_none")]
pub notes: Option<String>,
#[serde(default, skip_serializing_if = "Metadata::is_empty")]
pub meta: Metadata,
}
impl Implementation {
pub fn new(
id: impl Into<ImplId>,
proves: QualifiedName,
environment: Ident,
entry: impl Into<String>,
cost: ImplProfile,
) -> Self {
Self {
id: id.into(),
proves,
sig_hash: None,
environment,
entry: entry.into(),
cost,
notes: None,
meta: Metadata::default(),
}
}
pub fn cost_key(&self) -> (u8, u8, &str) {
(
self.cost.time.sort_key(),
self.cost.space.sort_key(),
self.id.as_str(),
)
}
pub fn memory_key(&self) -> (u8, u8, &str) {
(
self.cost.space.sort_key(),
self.cost.time.sort_key(),
self.id.as_str(),
)
}
}
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Manifest {
#[serde(default = "default_version")]
pub version: String,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub environments: Vec<crate::environment::Environment>,
#[serde(default, skip_serializing_if = "Vec::is_empty")]
pub implementations: Vec<Implementation>,
}
pub const MANIFEST_VERSION: &str = "1.0";
fn default_version() -> String {
MANIFEST_VERSION.to_string()
}
impl Default for Manifest {
fn default() -> Self {
Self {
version: default_version(),
environments: Vec::new(),
implementations: Vec::new(),
}
}
}
pub fn trivial_cost() -> ImplProfile {
ImplProfile {
time: BigO::O1,
space: BigO::O1,
notes: None,
}
}
#[cfg(test)]
mod tests {
use super::*;
fn impl_with(id: &str, time: BigO, space: BigO) -> Implementation {
Implementation::new(
id,
"demo::sort".parse().unwrap(),
Ident::new("here").unwrap(),
id,
ImplProfile::new(time, space),
)
}
#[test]
fn the_cheaper_proof_sorts_first() {
let merge = impl_with("merge", BigO::ONLogN, BigO::ON);
let bubble = impl_with("bubble", BigO::ON2, BigO::O1);
assert!(
merge.cost_key() < bubble.cost_key(),
"asked for speed, mergesort wins"
);
assert!(
bubble.memory_key() < merge.memory_key(),
"asked for memory, bubble sort wins"
);
}
#[test]
fn a_proof_that_will_not_say_what_it_costs_sorts_last() {
let stated = impl_with("stated", BigO::OExp, BigO::OExp);
let vague = impl_with("vague", BigO::Custom("depends".into()), BigO::O1);
assert!(
stated.cost_key() < vague.cost_key(),
"a stated cost, however bad, beats no answer"
);
}
#[test]
fn equally_good_proofs_are_ordered_the_same_way_every_time() {
let a = impl_with("alpha", BigO::ON, BigO::ON);
let b = impl_with("beta", BigO::ON, BigO::ON);
assert!(a.cost_key() < b.cost_key());
}
#[test]
fn a_manifest_round_trips_through_json() {
let manifest = Manifest {
version: MANIFEST_VERSION.to_string(),
environments: vec![crate::environment::Environment::subprocess(
"py",
"python",
"python3",
&["-u"],
)],
implementations: vec![impl_with("bubble", BigO::ON2, BigO::O1)],
};
let text = serde_json::to_string(&manifest).unwrap();
assert_eq!(serde_json::from_str::<Manifest>(&text).unwrap(), manifest);
}
#[test]
fn a_manifest_reads_the_way_someone_would_write_one() {
let manifest: Manifest = serde_json::from_str(
r#"{
"environments": [
{"id":"py","language":"python",
"reach":{"k":"subprocess","command":"python3","args":["impls.py"]}}],
"implementations": [
{"id":"demo.sort.py-bubble","proves":"demo::sort","environment":"py",
"entry":"bubble_sort",
"cost":{"time":"o_n2","space":"o1","notes":"in place"}}]
}"#,
)
.expect("reads");
assert_eq!(manifest.version, MANIFEST_VERSION);
assert_eq!(manifest.implementations[0].cost.time, BigO::ON2);
assert_eq!(manifest.environments[0].reach.kind(), "subprocess");
}
}