pub const KNOWLEDGE_CACHE_FLOOR: usize = 512;
#[must_use]
pub const fn cache_capacity(source_total: usize) -> usize {
let one_percent = (source_total + 99) / 100;
let floored = if one_percent > KNOWLEDGE_CACHE_FLOOR {
one_percent
} else {
KNOWLEDGE_CACHE_FLOOR
};
if floored < source_total {
floored
} else {
source_total
}
}
#[must_use]
pub const fn within_cache_capacity(cached: usize, source_total: usize) -> bool {
cached <= cache_capacity(source_total)
}
#[derive(Clone, Copy, Debug, PartialEq, Eq)]
pub enum KnowledgeSource {
RosettaCode,
Wikifunctions,
HelloWorldCollection,
StackOverflow,
}
impl KnowledgeSource {
#[must_use]
pub const fn slug(self) -> &'static str {
match self {
Self::RosettaCode => "rosetta-code",
Self::Wikifunctions => "wikifunctions",
Self::HelloWorldCollection => "hello-world-collection",
Self::StackOverflow => "stack-overflow",
}
}
#[must_use]
pub const fn display_name(self) -> &'static str {
match self {
Self::RosettaCode => "Rosetta Code",
Self::Wikifunctions => "Wikifunctions",
Self::HelloWorldCollection => "Hello World Collection",
Self::StackOverflow => "Stack Overflow",
}
}
#[must_use]
pub const fn base_url(self) -> &'static str {
match self {
Self::RosettaCode => "https://rosettacode.org",
Self::Wikifunctions => "https://www.wikifunctions.org",
Self::HelloWorldCollection => "http://helloworldcollection.de",
Self::StackOverflow => "https://stackoverflow.com",
}
}
#[must_use]
pub const fn approximate_catalog_size(self) -> usize {
match self {
Self::RosettaCode => 1_300,
Self::Wikifunctions => 3_000,
Self::HelloWorldCollection => 600,
Self::StackOverflow => 24_000_000,
}
}
}
#[derive(Clone, Copy, Debug)]
pub struct OracleSnippet {
pub task_slug: &'static str,
pub language_slug: &'static str,
pub language_label: &'static str,
pub source: KnowledgeSource,
pub source_url: &'static str,
pub code: &'static str,
pub expected_output: &'static str,
}
const ORACLE_SNAPSHOTS: &[OracleSnippet] = &[
OracleSnippet {
task_slug: "hello_world",
language_slug: "kotlin",
language_label: "Kotlin",
source: KnowledgeSource::HelloWorldCollection,
source_url: "http://helloworldcollection.de/#Kotlin",
code: "fun main() {\n println(\"Hello, World!\")\n}",
expected_output: "Hello, World!",
},
OracleSnippet {
task_slug: "hello_world",
language_slug: "swift",
language_label: "Swift",
source: KnowledgeSource::HelloWorldCollection,
source_url: "http://helloworldcollection.de/#Swift",
code: "print(\"Hello, World!\")",
expected_output: "Hello, World!",
},
OracleSnippet {
task_slug: "hello_world",
language_slug: "php",
language_label: "PHP",
source: KnowledgeSource::HelloWorldCollection,
source_url: "http://helloworldcollection.de/#PHP",
code: "<?php\necho \"Hello, World!\\n\";",
expected_output: "Hello, World!",
},
OracleSnippet {
task_slug: "hello_world",
language_slug: "bash",
language_label: "Bash",
source: KnowledgeSource::HelloWorldCollection,
source_url: "http://helloworldcollection.de/#Bash",
code: "echo \"Hello, World!\"",
expected_output: "Hello, World!",
},
OracleSnippet {
task_slug: "hello_world",
language_slug: "lua",
language_label: "Lua",
source: KnowledgeSource::HelloWorldCollection,
source_url: "http://helloworldcollection.de/#Lua",
code: "print(\"Hello, World!\")",
expected_output: "Hello, World!",
},
OracleSnippet {
task_slug: "hello_world",
language_slug: "haskell",
language_label: "Haskell",
source: KnowledgeSource::HelloWorldCollection,
source_url: "http://helloworldcollection.de/#Haskell",
code: "main :: IO ()\nmain = putStrLn \"Hello, World!\"",
expected_output: "Hello, World!",
},
OracleSnippet {
task_slug: "factorial",
language_slug: "kotlin",
language_label: "Kotlin",
source: KnowledgeSource::RosettaCode,
source_url: "https://rosettacode.org/wiki/Factorial#Kotlin",
code: "fun factorial(n: Int): Long =\n if (n <= 1) 1L else n * factorial(n - 1)\n\nfun main() {\n println(factorial(5))\n}",
expected_output: "120",
},
];
pub struct CodingOracle;
impl CodingOracle {
#[must_use]
pub const fn snapshots() -> &'static [OracleSnippet] {
ORACLE_SNAPSHOTS
}
#[must_use]
pub fn lookup(task_slug: &str, language: &str) -> Option<&'static OracleSnippet> {
let needle = language.trim().to_ascii_lowercase();
ORACLE_SNAPSHOTS.iter().find(|snippet| {
snippet.task_slug == task_slug
&& (snippet.language_slug == needle
|| snippet.language_label.to_ascii_lowercase() == needle)
})
}
#[must_use]
pub fn knows_language(language: &str) -> bool {
let needle = language.trim().to_ascii_lowercase();
ORACLE_SNAPSHOTS.iter().any(|snippet| {
snippet.language_slug == needle || snippet.language_label.to_ascii_lowercase() == needle
})
}
#[must_use]
pub fn languages() -> Vec<&'static str> {
let mut labels: Vec<&'static str> = Vec::new();
for snippet in ORACLE_SNAPSHOTS {
if !labels.contains(&snippet.language_label) {
labels.push(snippet.language_label);
}
}
labels
}
#[must_use]
pub fn cached_count(source: KnowledgeSource) -> usize {
ORACLE_SNAPSHOTS
.iter()
.filter(|snippet| snippet.source == source)
.count()
}
}