rdfoothills_conversion/conversion/
rdfx.rs

1// SPDX-FileCopyrightText: 2024 Robin Vobruba <hoijui.quaero@gmail.com>
2//
3// SPDX-License-Identifier: AGPL-3.0-or-later
4
5use std::ffi::OsStr;
6
7#[cfg(feature = "async")]
8use async_trait::async_trait;
9
10use super::OntFile;
11use rdfoothills_mime as mime;
12
13#[derive(Debug, Default)]
14pub struct Converter;
15
16const CLI_CMD: &str = "rdfx";
17const CLI_CMD_DESC: &str = "RDF format conversion";
18
19impl Converter {
20    fn rdfx<I, S>(args: I) -> Result<(), super::Error>
21    where
22        I: IntoIterator<Item = S> + Send + Clone,
23        S: AsRef<OsStr>,
24    {
25        super::cli_cmd(CLI_CMD, CLI_CMD_DESC, args)
26    }
27
28    #[cfg(feature = "async")]
29    async fn rdfx_async<I, S>(args: I) -> Result<(), super::Error>
30    where
31        I: IntoIterator<Item = S> + Send + Clone,
32        S: AsRef<OsStr>,
33    {
34        super::cli_cmd_async(CLI_CMD, CLI_CMD_DESC, args).await
35    }
36
37    const fn supports_format(fmt: mime::Type) -> bool {
38        match fmt {
39            mime::Type::N3
40            | mime::Type::JsonLd
41            | mime::Type::NTriples
42            | mime::Type::OwlXml
43            | mime::Type::RdfXml
44            | mime::Type::Turtle => true,
45            mime::Type::BinaryRdf
46            | mime::Type::Csvw
47            | mime::Type::Hdt
48            | mime::Type::HexTuples
49            | mime::Type::Html
50            | mime::Type::Manchester
51            | mime::Type::Microdata
52            | mime::Type::NdJsonLd
53            | mime::Type::NQuads
54            | mime::Type::NQuadsStar
55            | mime::Type::NTriplesStar
56            | mime::Type::OwlFunctional
57            | mime::Type::RdfA
58            | mime::Type::RdfJson
59            | mime::Type::TriG
60            | mime::Type::TriGStar
61            | mime::Type::TriX
62            | mime::Type::Tsvw
63            | mime::Type::TurtleStar
64            | mime::Type::YamlLd => false,
65        }
66    }
67}
68
69macro_rules! convert_args {
70    ($from:expr, $to:expr) => {
71        &[
72            OsStr::new("convert"),
73            OsStr::new("--format"),
74            OsStr::new(
75                super::to_rdflib_format($to.mime_type)
76                    .expect("rdfx called with an invalid (-> unsupported by RDFlib) target type"),
77            ),
78            OsStr::new("--output"),
79            $to.file.as_os_str(),
80            $from.file.as_os_str(),
81        ]
82    };
83}
84
85#[cfg_attr(feature = "async", async_trait)]
86impl super::Converter for Converter {
87    fn info(&self) -> super::Info {
88        super::Info {
89            quality: super::Quality::Data,
90            priority: super::Priority::Low,
91            typ: super::Type::Cli,
92            name: "rdfx",
93        }
94    }
95
96    fn is_available(&self) -> bool {
97        super::is_cli_cmd_available(CLI_CMD)
98    }
99
100    fn supports(&self, from: mime::Type, to: mime::Type) -> bool {
101        Self::supports_format(from) && Self::supports_format(to)
102    }
103
104    fn convert(&self, from: &OntFile, to: &OntFile) -> Result<(), super::Error> {
105        Self::rdfx(convert_args!(from, to))
106    }
107
108    #[cfg(feature = "async")]
109    async fn convert_async(&self, from: &OntFile, to: &OntFile) -> Result<(), super::Error> {
110        Self::rdfx_async(convert_args!(from, to)).await
111    }
112}