nextcloud-client-api 0.1.0

implementation of the socket API for the NextCloud client
Documentation
use core::{
    fmt::{Display, Formatter, Result as FmtResult},
    result::Result as StdResult,
};

use thiserror::Error as ThisError;

#[derive(Debug)]
#[non_exhaustive]
pub enum FileStatus {
    /// `Ok` indicates a synced and clean status.
    Ok,
    OkSwm,
    /// `Sync` indicates, that the folder or file is currently synced.
    /// That could mean, that it is only checked, but there might be
    /// some data transmission in progress.
    Sync,
    /// `Ignore` is the status of files, that are not synced to/from a
    /// backend instance. E.g. configuration of the client.
    Ignore,
    /// `Nop` is a state, when the client is in offline mode.
    Nop,
}

#[derive(Debug, ThisError)]
#[non_exhaustive]
pub enum Error {
    #[error("failed to parse status: {0}")]
    ParsingFailed(String),
}

impl TryFrom<&str> for FileStatus {
    type Error = Error;

    fn try_from(value: &str) -> StdResult<Self, Self::Error> {
        Ok(match value {
            "OK" => Self::Ok,
            "OK+SWM" => Self::OkSwm,
            "SYNC" => Self::Sync,
            "IGNORE" => Self::Ignore,
            "NOP" => Self::Nop,
            _ => return Err(Error::ParsingFailed(format!("invalid status: {value}"))),
        })
    }
}

impl Display for FileStatus {
    #[expect(clippy::min_ident_chars, reason = "defined by trait")]
    fn fmt(&self, f: &mut Formatter<'_>) -> FmtResult {
        write!(
            f,
            "{}",
            match *self {
                FileStatus::Ok => "Ok",
                FileStatus::OkSwm => "Ok+SWM",
                FileStatus::Sync => "Sync",
                FileStatus::Ignore => "Ignore",
                FileStatus::Nop => "Nop",
            }
        )
    }
}