oparlint-suites 0.0.2

Lint suites that can be run by oparlint
Documentation
// SPDX-FileCopyrightText: Politik im Blick developers
// SPDX-FileCopyrightText: Wolfgang Silbermayr <wolfgang@silbermayr.at>
//
// SPDX-License-Identifier: AGPL-3.0-or-later OR EUPL-1.2

use oparlint_linting::{LintSuite, LintingResult};
use oparlint_lints::json::{BodyIsJsonLint, BodyIsJsonObjectLint};
use reqwest::Response;
use serde_json::{Map, Value};

#[derive(Debug)]
pub struct HttpJsonObjectSuite {
    response: Response,
}

impl HttpJsonObjectSuite {
    /// Create a instance of the lint suite.
    pub fn new(response: Response) -> Self {
        Self { response }
    }
}

#[async_trait::async_trait]
impl LintSuite for HttpJsonObjectSuite {
    type Output = Map<String, Value>;

    fn identifier(&self) -> String {
        "json-object".into()
    }

    fn title(&self) -> String {
        "HTTP response is a proper JSON object".into()
    }

    async fn execute<L: oparlint_linting::Linter + Send>(
        self,
        linter: &mut L,
    ) -> LintingResult<Self::Output> {
        let deserialized = self.response.json::<Value>().await;
        let json = linter.execute_lint(BodyIsJsonLint::new(deserialized))?;

        linter.execute_lint(BodyIsJsonObjectLint::new(json))
    }
}