Imports name this capability’s declared dependencies, which fune builds next to it in your project; each one links to its page.
use super::funejson::Value; ← the fune runtime: the JSON value the test vectors use; fune build keeps it only where a signature takes one
fn is_space(c: char) -> bool {
matches!(c, ' ' | '\t' | '\n' | '\r' | '\x0c' | '\x0b')
}
// RFC 6838's restricted-name characters.
fn is_mime_name(value: &str) -> bool {
!value.is_empty() && value.chars().all(|c| c.is_ascii_alphanumeric() || "!#$&^_.+-".contains(c))
}
/// The accept attribute's tokens, trimmed and lower-cased, empty ones dropped.
///
/// # Panics
/// Panics on a token that is not an extension, a MIME type or a type/* wildcard.
pub fn accept_tokens(accept: &str) -> Vec<String> {
let mut tokens = Vec::new();
for raw in accept.split(',') {
let trimmed = raw.trim_matches(is_space);
let token = trimmed.to_ascii_lowercase();
if token.is_empty() {
continue;
}
let ok = if let Some(ext) = token.strip_prefix('.') {
!ext.is_empty() && !ext.contains('/') && !ext.chars().any(is_space)
} else {
match token.find('/') {
Some(slash) if slash > 0 => {
let (kind, subtype) = (&token[..slash], &token[slash + 1..]);
is_mime_name(kind) && (subtype == "*" || is_mime_name(subtype))
}
_ => false,
}
};
if !ok {
panic!(
"accept token \"{}\" is not an extension (.pdf), a MIME type (application/pdf) or a wildcard (image/*)",
trimmed
);
}
tokens.push(token);
}
tokens
}
/// What the accepted types are called in an error message: "a PDF", "an
/// image", "a PDF, JPG or PNG".
pub fn describe_accept(tokens: &[String]) -> String {
let mut words: Vec<String> = Vec::new();
for token in tokens {
let word = if let Some(ext) = token.strip_prefix('.') {
ext.to_ascii_uppercase()
} else if let Some(kind) = token.strip_suffix("/*") {
kind.to_string()
} else {
token[token.find('/').map(|i| i + 1).unwrap_or(0)..].to_ascii_uppercase()
};
if !words.contains(&word) {
words.push(word);
}
}
let listed = if words.len() == 1 {
words[0].clone()
} else {
format!("{} or {}", words[..words.len() - 1].join(", "), words[words.len() - 1])
};
let first = words[0].chars().next().unwrap_or(' ');
// A lower-case word is said as a word; an initialism letter by letter.
let an = if first.is_ascii_lowercase() { "aeiou".contains(first) } else { "AEFHILMNORSX8".contains(first) };
format!("{} {}", if an { "an" } else { "a" }, listed)
}
pub fn matches_accept(tokens: &[String], file: &FileInfo) -> bool {
if tokens.is_empty() {
return true;
}
let name = file.name.to_ascii_lowercase();
let kind = file.mime_type.to_ascii_lowercase();
tokens.iter().any(|token| {
if token.starts_with('.') {
name.ends_with(token.as_str())
} else if let Some(prefix) = token.strip_suffix('*') {
kind.starts_with(prefix)
} else {
kind == *token
}
})
}
/// Whether a browser's file chooser would offer this file for an accept
/// attribute: by extension, exact MIME type or type/* wildcard, ignoring case.
/// An empty accept takes anything.
///
/// # Panics
/// Panics on a malformed accept token.
pub fn accepts_file(accept: &str, file: &FileInfo) -> bool {
matches_accept(&accept_tokens(accept), file)
}
pub fn file_info_from_value(v: &Value) -> FileInfo {
let size = match v.get("size") {
Value::Int(n) => *n,
other => panic!("file size must be a whole number of bytes, got {:?}", other),
};
FileInfo { name: v.get("name").as_str().to_string(), size, mime_type: v.get("mimeType").as_str().to_string() }
}
pub fn fune_vector(args: &[Value]) -> Value {
Value::Bool(accepts_file(args[0].as_str(), &file_info_from_value(&args[1])))
}