openserve 2.0.3

A modern, high-performance, AI-enhanced file server built in Rust
Documentation
//! Models Module
//!
//! This module defines the primary data structures used throughout the application.
//! These models are used for serialization, database mapping, and API responses.

pub mod ai;
pub mod auth;
pub mod file;
pub mod search;

pub use ai::*;
pub use auth::*;

pub use search::*;

use serde::{Deserialize, Serialize};
use chrono::{DateTime, Utc};

/// Represents a file in the file system.
///
/// This struct contains metadata about a file that is used throughout
/// the application for API responses and internal operations.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct File {
    /// Name of the file
    pub name: String,
    /// Full path to the file
    pub path: String,
    /// Size of the file in bytes
    pub size: u64,
    /// Last modified timestamp
    pub modified: DateTime<Utc>,
    /// MIME type of the file
    pub mime_type: String,
}

/// Represents a directory in the file system.
///
/// This struct contains metadata about a directory and its contents,
/// used for directory listing operations.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct Directory {
    /// Name of the directory
    pub name: String,
    /// Full path to the directory
    pub path: String,
    /// Last modified timestamp
    pub modified: DateTime<Utc>,
    /// Directory entries (files and subdirectories)
    pub entries: Vec<crate::services::file::DirectoryEntry>,
}

/// Detailed metadata information about a file or directory.
///
/// This struct provides comprehensive metadata that extends beyond
/// basic file information to include permissions and timestamps.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct FileMetadata {
    /// Size of the file in bytes
    pub size: u64,
    /// Last modified timestamp
    pub modified: DateTime<Utc>,
    /// Creation timestamp
    pub created: DateTime<Utc>,
    /// Whether this is a file
    pub is_file: bool,
    /// Whether this is a directory
    pub is_directory: bool,
    /// File permissions as string
    pub permissions: String,
}

/// Represents a user account in the system.
///
/// This struct contains all the information needed to identify and
/// authorize a user within the application.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct User {
    /// Unique user identifier
    pub id: String,
    /// Username for authentication
    pub username: String,
    /// User email address
    pub email: String,
    /// User role for authorization
    pub role: UserRole,
    /// Account creation timestamp
    pub created_at: DateTime<Utc>,
    /// Last login timestamp
    pub last_login: Option<DateTime<Utc>>,
}

/// Defines the different user roles and their access levels.
///
/// This enum is used for authorization decisions throughout the application,
/// determining what operations a user is allowed to perform.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub enum UserRole {
    /// Administrator with full access
    Admin,
    /// Regular user with standard access
    User,
    /// Read-only user with limited access
    ReadOnly,
}

/// Represents a generic API response for success cases.
///
/// This structure is used to provide a consistent response format
/// for successful API calls that return a JSON object.
#[derive(Debug, Serialize, Deserialize)]
pub struct ApiResponse<T> {
    /// Indicates the success of the request.
    pub success: bool,
    /// The main data payload of the response.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub data: Option<T>,
    /// An optional message providing more information.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub message: Option<String>,
}

impl<T> ApiResponse<T> {
    /// Creates a new successful `ApiResponse` with data.
    pub fn new(data: T, message: Option<String>) -> Self {
        Self {
            success: true,
            data: Some(data),
            message,
        }
    }
}

/// Represents a generic API response for error cases.
///
/// This structure is used to provide a consistent response format
/// for failed API calls.
#[derive(Debug, Serialize, Deserialize)]
pub struct ErrorResponse {
    /// Indicates the failure of the request.
    pub success: bool,
    /// A descriptive error message.
    pub error: String,
}

/// WebSocket message types
#[derive(Debug, Clone, Serialize, Deserialize)]
#[serde(tag = "type")]
pub enum WebSocketMessage {
    /// File change notification
    FileChanged { 
        /// Path of the changed file
        path: String, 
        /// Type of change event
        event: String 
    },
    /// Directory change notification
    DirectoryChanged { 
        /// Path of the changed directory
        path: String, 
        /// Type of change event
        event: String 
    },
    /// Search results notification
    SearchResults { 
        /// Search query that was executed
        query: String, 
        /// List of search results
        results: Vec<SearchResult> 
    },
    /// Error notification
    Error { 
        /// Error message
        message: String 
    },
}

/// Request structure for file upload operations.
///
/// This struct defines the parameters needed to upload a file to the server,
/// including the target location and upload behavior.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UploadRequest {
    /// Target path for the upload
    pub path: String,
    /// Whether to overwrite existing files
    pub overwrite: bool,
}

/// Upload response
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct UploadResponse {
    /// Path where the file was uploaded
    pub path: String,
    /// Size of the uploaded file in bytes
    pub size: u64,
    /// Success message
    pub message: String,
}

/// Request structure for search operations.
///
/// This struct defines the parameters for performing searches across
/// the file system, including query terms and optional filters.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchRequest {
    /// Search query string
    pub query: String,
    /// Maximum number of results to return
    pub limit: Option<usize>,
    /// Optional search filters
    pub filters: Option<SearchFilters>,
}

/// Filtering options for search operations.
///
/// This struct allows users to narrow down search results based on
/// various file attributes like type, size, and modification date.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct SearchFilters {
    /// Filter by MIME types
    pub mime_types: Option<Vec<String>>,
    /// Minimum file size in bytes
    pub size_min: Option<u64>,
    /// Maximum file size in bytes
    pub size_max: Option<u64>,
    /// Filter files modified after this date
    pub modified_after: Option<DateTime<Utc>>,
    /// Filter files modified before this date
    pub modified_before: Option<DateTime<Utc>>,
}

/// Request structure for AI content analysis operations.
///
/// This struct defines the parameters needed to analyze file content
/// using AI services, including the target file and analysis options.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnalysisRequest {
    /// Path of the file to analyze
    pub path: String,
    /// Analysis options
    pub options: AnalysisOptions,
}

/// Configuration options for AI content analysis.
///
/// This struct allows users to specify which types of analysis
/// should be performed on the content.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct AnalysisOptions {
    /// Whether to generate a summary
    pub summarize: bool,
    /// Whether to extract entities
    pub extract_entities: bool,
    /// Whether to generate tags
    pub generate_tags: bool,
    /// Whether to detect language
    pub detect_language: bool,
}

impl Default for AnalysisOptions {
    fn default() -> Self {
        Self {
            summarize: true,
            extract_entities: false,
            generate_tags: false,
            detect_language: false,
        }
    }
}

/// Request structure for AI chat operations.
///
/// This struct defines the parameters for interacting with the AI
/// chat functionality, including the message and optional context.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatRequest {
    /// Chat message
    pub message: String,
    /// Optional context file paths
    pub context_paths: Option<Vec<String>>,
    /// Maximum context length
    pub max_context_length: Option<usize>,
}

/// Response structure for AI chat operations.
///
/// This struct contains the AI's response along with metadata about
/// the interaction, such as context used and token consumption.
#[derive(Debug, Clone, Serialize, Deserialize)]
pub struct ChatResponse {
    /// AI response message
    pub response: String,
    /// Context files that were used
    pub context_used: Vec<String>,
    /// Number of tokens used in the request
    pub tokens_used: Option<u32>,
}