hayagriva icon indicating copy to clipboard operation
hayagriva copied to clipboard

Serialization failure when PersonRole::Unknown is involved

Open chenlijun99 opened this issue 1 year ago • 5 comments

The following code

// path is path to the biblatex file
let contents = fs::read_to_string(path)?;
let bib = hayagriva_io::from_biblatex_str(&contents).unwrap();

Ok(serde_json::to_value(bib).unwrap())

errors out with:

called `Result::unwrap()` on an `Err` value: Error("the enum variant PersonRole::Unknown cannot be serialized", line: 0, column: 0)

I believe this is caused by a wrong use of #serde(skip) in the PersonRole enum. https://github.com/chenlijun99/hayagriva/blob/af5c146265b797a74edcb0c760e52e41834a037a/src/types/persons.rs#L83-L85

A similar issue was raised in serde: https://github.com/serde-rs/serde/issues/2217#issuecomment-1128080006. The gist seems that the value itself cannot decide to be ignored.

Can be reproduced using the following biblatex entry.

@video{wachowskiMatrix1999,
  type = {Action, Sci-Fi},
  entrysubtype = {film},
  title = {The {{Matrix}}},
  editor = {Wachowski, Lana and Wachowski, Lilly},
  editortype = {director},
  editora = {Wachowski, Lilly and Wachowski, Lana},
  editoratype = {scriptwriter},
  namea = {Reeves, Keanu and Fishburne, Laurence and Moss, Carrie-Anne},
  nameatype = {collaborator},
  date = {1999-03-31},
  publisher = {Warner Bros., Village Roadshow Pictures, Groucho Film Partnership},
  abstract = {When a beautiful stranger leads computer hacker Neo to a forbidding underworld, he discovers the shocking truth--the life he knows is the elaborate deception of an evil cyber-intelligence.},
  keywords = {artificial reality,dystopia,post apocalypse,simulated reality,war with machines},
  annotation = {IMDb ID: tt0133093\\
event-location: United States, Australia}
}

chenlijun99 avatar Jan 11 '25 10:01 chenlijun99

I think this was probably added to ensure persons with unknown roles wouldn't be serialized back into the hayagriva yaml format, but I think this might have been inappropriately handled. In theory, that bibliography cannot be turned into valid hayagriva, as there is a fixed list of allowed roles in the format specification, but maybe we could just flexibilize this restriction, at the cost of breaking some forwards-compatibility. Thoughts? cc @reknih

PgBiel avatar Feb 04 '25 00:02 PgBiel

I'd rather not allow any value in Hayagriva person roles. I think that the error message that your person was not in the list is valuable, because CSL styles will not select or format arbitrary roles. I also see the need for a generic, catch-all role. Hence, I'd propose changing PersonRole::Unknown(String) to PersonRole::Other and removing its #[serde(skip)]

reknih avatar Feb 04 '25 10:02 reknih

Well, that could be an initial improvement. However, I think the problem for this particular case might be different: script-writer is a valid CSL variable and would translate to PersonRole::Writer, but their scriptwriter isn't being interpreted as such - a quick search in interop.rs shows that there is no mention of Writer. And this makes sense as it's not defined in the biblatex standard, so I wonder if we would need to intervene there and consider some non-official translation. In any case, the problem in this issue is different from what we thought.

PgBiel avatar Feb 04 '25 20:02 PgBiel

I'm open to allow scriptwriter in interop - is there any prior art for this?

reknih avatar Feb 04 '25 21:02 reknih

Found this long thread here: https://github.com/retorquere/zotero-better-bibtex/issues/2802 Which links to some other places which also seem to be using scriptwriter.

However, what I can tell for this is that the real "standards" in reference management are very fragmented... and here this is mostly due to how Zotero exports things, if I understood correctly. Maybe we can add a quick hack around this in interop, even if temporarily. But, more generally, it seems that movie/film/video-related attributes are not very well specified by biblatex.

PgBiel avatar Feb 05 '25 01:02 PgBiel