mirror of
https://github.com/storytold/vectorcraft.git
synced 2026-11-27 13:15:12 +00:00
Plug-ins: unknown keys inside a written style's nested attributes are refused
A paragraph style's hyphenation, justification, tab stops and list, and a character style's stroke dash, now refuse a key they don't read, naming the field, as the style's own keys already did. A misspelled nested key no longer leaves that setting unchanged without an error.
This commit is contained in:
@@ -1323,6 +1323,31 @@ fn bad_writes_are_refused_naming_the_object_and_the_field() {
|
||||
(text, json!({"text": {"runs": [{"text": "Hi", "style": 5}]}}), &InputKind::ALL, "text.runs[0]: `style` must be an object".into()),
|
||||
(text, json!({"text": {"paragraphs": [5, {}]}}), &InputKind::ALL, "text: `paragraphs[0]` must be an object".into()),
|
||||
(text, json!({"text": {"paragraphs": [{"spaceBefor": 1}, {}]}}), &InputKind::ALL, "text.paragraphs[0]: unknown key 'spaceBefor'".into()),
|
||||
(
|
||||
text,
|
||||
json!({"text": {"paragraphs": [{"hyphenation": {"wordLongerThen": 3}}, {}]}}),
|
||||
&InputKind::ALL,
|
||||
"text.paragraphs[0].hyphenation: unknown key 'wordLongerThen'".into(),
|
||||
),
|
||||
(
|
||||
text,
|
||||
json!({"text": {"paragraphs": [{"justification": {"wordSpacng": [80, 100, 133]}}, {}]}}),
|
||||
&InputKind::ALL,
|
||||
"text.paragraphs[0].justification: unknown key 'wordSpacng'".into(),
|
||||
),
|
||||
(
|
||||
text,
|
||||
json!({"text": {"paragraphs": [{}, {"tabs": [{"position": 36}, {"position": 72, "aling": "center"}]}]}}),
|
||||
&InputKind::ALL,
|
||||
"text.paragraphs[1].tabs[1]: unknown key 'aling'".into(),
|
||||
),
|
||||
(text, json!({"text": {"paragraphs": [{"list": {"levl": 2}}, {}]}}), &InputKind::ALL, "text.paragraphs[0].list: unknown key 'levl'".into()),
|
||||
(
|
||||
text,
|
||||
json!({"text": {"runs": styled(&|s| s["stroke_dash"] = json!({"pattern": [2, 2], "ofset": 1}))}}),
|
||||
&InputKind::ALL,
|
||||
"text.runs[0].style.stroke_dash: unknown key 'ofset'".into(),
|
||||
),
|
||||
(
|
||||
text,
|
||||
json!({"text": {"runs": [{"text": "Hi\u{FFFC}", "style": style}]}}),
|
||||
|
||||
@@ -165,6 +165,28 @@ fn strict<T: DeserializeOwned>(v: &Value, parent: &str, key: &str) -> Result<T>
|
||||
serde_json::from_value(v.clone()).map_err(|e| out_err(&at, e))
|
||||
}
|
||||
|
||||
/// The field `key` of `v`, the style at `at`, holds only attributes `T` reads: an object, or each
|
||||
/// object of a list. An unknown key is an error at `at.key` (or `at.key[i]`); other values are left
|
||||
/// to the style's own checks.
|
||||
fn known_keys<T: DeserializeOwned>(v: &Value, at: &str, key: &str) -> Result<()> {
|
||||
let names = attr_names::<T>();
|
||||
let check = |o: &Map<String, Value>, at: &str| match o.keys().find(|k| !names.contains(&k.as_str())) {
|
||||
Some(k) => {
|
||||
let k = crate::permissions::display_text(k, crate::permissions::MAX_SHOWN_FIELD);
|
||||
Err(out_err(at, format!("unknown key '{k}'")))
|
||||
}
|
||||
None => Ok(()),
|
||||
};
|
||||
match v.get(key) {
|
||||
Some(Value::Object(o)) => check(o, &format!("{at}.{key}")),
|
||||
Some(Value::Array(list)) => list.iter().enumerate().try_for_each(|(i, e)| match e {
|
||||
Value::Object(o) => check(o, &format!("{at}.{key}[{i}]")),
|
||||
_ => Ok(()),
|
||||
}),
|
||||
_ => Ok(()),
|
||||
}
|
||||
}
|
||||
|
||||
/// A transform from a filter's output: six finite numbers, each within ±[`MAX_COORD`], that don't
|
||||
/// flatten the object.
|
||||
fn decode_transform(v: &Value, at: &str) -> Result<Affine> {
|
||||
@@ -267,6 +289,7 @@ fn set_text(doc: &Document, t: &mut TextObject, threaded: bool, runs: Option<&Va
|
||||
let text = o.get(key).and_then(Value::as_str).ok_or_else(|| out_err(&at, format!("`{key}` must be a string")))?;
|
||||
let style_v = o.get("style").unwrap_or(&Value::Null);
|
||||
let mut style: CharStyle = strict(style_v, &at, "style")?;
|
||||
known_keys::<vectorcraft_doc::Dash>(style_v, &format!("{at}.style"), "stroke_dash")?;
|
||||
let inline = match o.get("inline") {
|
||||
None | Some(Value::Null) => None,
|
||||
Some(v) => Some(serde_json::from_value(v.clone()).map_err(|e| out_err(&format!("{at}.inline"), e))?),
|
||||
@@ -447,6 +470,10 @@ fn range_err(at: &str, e: &AttrRangeError) -> Error {
|
||||
fn para_style(doc: &Document, v: &Value, parent: &str, key: &str) -> Result<ParaStyle> {
|
||||
let mut st: ParaStyle = strict(v, parent, key)?;
|
||||
let at = format!("{parent}.{key}");
|
||||
known_keys::<vectorcraft_doc::HyphenationOptions>(v, &at, "hyphenation")?;
|
||||
known_keys::<JustificationOptions>(v, &at, "justification")?;
|
||||
known_keys::<vectorcraft_doc::TabStop>(v, &at, "tabs")?;
|
||||
known_keys::<vectorcraft_doc::text::ParaList>(v, &at, "list")?;
|
||||
let justification = match v.get("justification") {
|
||||
None | Some(Value::Null) => None,
|
||||
Some(j) => Some(serde_json::from_value::<JustificationOptions>(j.clone()).map_err(|e| out_err(&format!("{at}.justification"), e))?),
|
||||
|
||||
Reference in New Issue
Block a user