tensorlake 0.5.157

A Rust SDK for interacting with Tensorlake Cloud APIs.
Documentation
use serde::{Deserialize, Serialize};

#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct MintGitTokenRequest {
    #[serde(skip_serializing_if = "Option::is_none")]
    pub repo: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct CreateRepoRequest {
    #[serde(rename = "default_branch")]
    pub default_branch: String,
    /// "repository" (default) or "filesystem". Omitted (not sent) when `None` so the request
    /// stays valid against servers that predate repo kinds (`deny_unknown_fields`).
    #[serde(skip_serializing_if = "Option::is_none")]
    pub kind: Option<String>,
    /// Immutable storage-network placement, supported only for filesystem creation.
    /// Omitted requests retain the server's legacy location.
    #[serde(skip_serializing_if = "Option::is_none")]
    pub region: Option<String>,
}

impl Default for CreateRepoRequest {
    fn default() -> Self {
        Self {
            default_branch: "main".to_string(),
            kind: None,
            region: None,
        }
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GitCredential {
    pub token: String,
    pub token_type: String,
    pub expires_at: String,
    pub git_username: String,
    pub repo_pattern: String,
    pub scopes: Vec<String>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Repo {
    pub name: String,
    pub full_name: String,
    pub default_branch: String,
    pub status: String,
    /// "repository" or "filesystem". Defaults to "repository" when the server predates repo
    /// kinds and omits the field.
    #[serde(default = "default_repo_kind")]
    pub kind: String,
}

fn default_repo_kind() -> String {
    REPO_KIND_REPOSITORY.to_string()
}

/// The wire names for repo kinds — defined once; every construction and comparison goes
/// through these (a typo'd bare literal would compile and silently misroute).
pub const REPO_KIND_REPOSITORY: &str = "repository";
pub const REPO_KIND_FILESYSTEM: &str = "filesystem";

impl Repo {
    pub fn is_filesystem(&self) -> bool {
        self.kind == REPO_KIND_FILESYSTEM
    }
}

/// `GET /project/{p}/repos/{r}/meta` — the authoritative point-read of one repo's product
/// identity. Never served from the listing cache (read-your-writes after create) and
/// answerable with a repo-scoped credential.
#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct RepoMetaInfo {
    pub name: String,
    pub full_name: String,
    pub default_branch: String,
    pub status: String,
    #[serde(default = "default_repo_kind")]
    pub kind: String,
    /// Storage-network placement. Older servers omit this; an explicit-region retry must
    /// never adopt an existing filesystem without matching placement evidence.
    #[serde(default, skip_serializing_if = "Option::is_none")]
    pub region: Option<String>,
}

impl RepoMetaInfo {
    pub fn is_filesystem(&self) -> bool {
        self.kind == REPO_KIND_FILESYSTEM
    }
    /// Response-loss recovery must not adopt a same-named filesystem in a different region.
    pub fn matches_filesystem_region(&self, requested: Option<&str>) -> bool {
        self.is_filesystem()
            && requested.is_none_or(|region| self.region.as_deref() == Some(region))
    }
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ListReposResponse {
    pub project: String,
    pub repos: Vec<Repo>,
    #[serde(default)]
    pub next_after: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct GitRef {
    pub name: String,
    pub oid: String,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ListRefsResponse {
    pub repo: String,
    pub refs: Vec<GitRef>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Branch {
    pub name: String,
    pub ref_name: String,
    pub oid: String,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ListBranchesResponse {
    pub repo: String,
    pub branches: Vec<Branch>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct RepoInfo {
    pub repo: String,
    pub url: String,
    pub branches: Vec<Branch>,
    pub refs: Vec<GitRef>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct OperationRef {
    pub name: String,
    pub old: Option<String>,
    pub new: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct Operation {
    pub op_id: String,
    pub repo: String,
    pub network: Option<String>,
    pub parent_op_id: Option<String>,
    pub actor: String,
    pub at_secs: u64,
    pub kind: String,
    pub result: String,
    pub refs: Vec<OperationRef>,
    pub pack_ids: Vec<String>,
    pub old_pack_ids: Vec<String>,
    pub related_repo: Option<String>,
    pub status: Option<String>,
    pub old_pack_count: u32,
    pub object_count: u32,
    pub pack_bytes: u64,
    /// The operation materialized merge conflicts (its commit carries a conflict record).
    /// Elided by the server when false; absent from pre-visibility servers.
    #[serde(default)]
    pub conflicted: bool,
    /// Native snapshot attributed to a verification or head-promotion operation. Absent on Git
    /// operations and on servers predating the native engine.
    #[serde(default)]
    pub native_snapshot: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct ListOperationsResponse {
    pub repo: String,
    pub operations: Vec<Operation>,
    #[serde(default)]
    pub next_after: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct NativeHeadResponse {
    pub snapshot_id: Option<String>,
    pub generation: u64,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct NativeFilesystemFileRead {
    pub data: Vec<u8>,
    pub content_id: String,
    pub full_size: u64,
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub(crate) struct NativeReadPlanPage {
    pub inline_base64: Option<String>,
    pub parts: Vec<NativeReadPart>,
    pub logical_len: u64,
    pub content_id: Option<String>,
    pub hash_algorithm: Option<String>,
    pub next_offset: Option<u64>,
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub(crate) struct NativeReadPart {
    pub offset: u64,
    pub stored_len: u64,
    pub logical_len: u64,
    pub compression: String,
    pub content_id: String,
    pub hash_algorithm: String,
    pub stored_checksum_sha256: Option<String>,
    pub url: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct NativeDirectUploadLeaseResponse {
    pub lease_id: String,
    pub expires_at_ms: u64,
    pub transport: String,
    pub checksum_algorithm: String,
    pub max_blob_bytes: u64,
    pub max_targets_per_request: usize,
    pub max_parts_per_file: usize,
    /// Targets for blobs the client declared in the lease request, folding the first target
    /// negotiation into this response. `None` from servers that predate the combined flow —
    /// the caller then issues the standalone targets request(s) exactly as before.
    #[serde(default)]
    pub targets: Option<Vec<NativeDirectBlobTarget>>,
}

#[derive(Clone, Debug, PartialEq, Eq, Serialize)]
pub struct NativeDirectBlobRequest {
    pub blob_id: String,
    pub logical_len: u64,
}

#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct NativeDirectBlobTargetsRequest {
    pub blobs: Vec<NativeDirectBlobRequest>,
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct NativeDirectBlobTarget {
    pub blob_id: String,
    pub logical_len: u64,
    pub already_present: bool,
    pub url: Option<String>,
    pub checksum_sha256: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct NativeDirectBlobTargetsResponse {
    pub targets: Vec<NativeDirectBlobTarget>,
}

#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct NativeDirectUploadReceipt {
    pub blob_id: String,
    pub logical_len: u64,
}

#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum NativeDirectMutation {
    Put {
        path: String,
        blobs: Vec<NativeDirectBlobRequest>,
    },
    Delete {
        path: String,
    },
    Move {
        from: String,
        to: String,
    },
    Copy {
        from: String,
        to: String,
    },
}

#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct NativeDirectPublishRequest {
    pub operation_id: String,
    pub message: String,
    pub expected_version_id: Option<String>,
    pub lease_id: String,
    pub uploads: Vec<NativeDirectUploadReceipt>,
    pub mutations: Vec<NativeDirectMutation>,
    pub retain_as_snapshot: bool,
}

#[derive(Clone, Debug, PartialEq, Serialize)]
#[serde(tag = "type", rename_all = "snake_case")]
pub enum NativeMetadataMutation {
    Delete { path: String },
    Move { from: String, to: String },
    Copy { from: String, to: String },
}

#[derive(Clone, Debug, PartialEq, Serialize)]
pub struct NativeMetadataPublishRequest {
    pub operation_id: String,
    pub message: String,
    pub mutations: Vec<NativeMetadataMutation>,
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct NativeDirectPublishResponse {
    pub version_id: String,
    pub previous_version_id: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct NativeFilesystemSnapshot {
    pub snapshot_id: String,
    pub created_at_ms: u64,
    pub message: String,
    pub snapshot_class: String,
    #[serde(default)]
    pub permanence_epoch: u64,
}

#[derive(Clone, Debug, PartialEq, Deserialize)]
pub struct NativeFilesystemSnapshotPage {
    pub snapshots: Vec<NativeFilesystemSnapshot>,
    pub next_after: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Serialize, Deserialize)]
pub struct NativeFilesystemSnapshotRetentionResponse {
    pub snapshot_id: String,
    pub snapshot_class: String,
    pub message: Option<String>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct NativeDirectFileWrite {
    pub path: String,
    pub data: Vec<u8>,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct NativeDirectFilePathWrite {
    pub path: String,
    pub source_path: std::path::PathBuf,
}

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct NativeDirectPathTransfer {
    pub from: String,
    pub to: String,
}

#[cfg(test)]
mod placement_tests {
    use super::*;

    #[test]
    fn filesystem_region_retry_requires_explicit_matching_placement() {
        let mut meta: RepoMetaInfo = serde_json::from_str(r#"{"name":"fs","full_name":"project/fs","default_branch":"main","status":"active","kind":"filesystem"}"#).unwrap();
        assert!(meta.matches_filesystem_region(None));
        assert!(!meta.matches_filesystem_region(Some("eu-central-1")));
        meta.region = Some("us-east-1".into());
        assert!(!meta.matches_filesystem_region(Some("eu-central-1")));
        meta.region = Some("eu-central-1".into());
        assert!(meta.matches_filesystem_region(Some("eu-central-1")));
        meta.kind = REPO_KIND_REPOSITORY.into();
        assert!(!meta.matches_filesystem_region(Some("eu-central-1")));
    }

    #[test]
    fn filesystem_region_wire_preserves_omission_and_explicit_placement() {
        let mut request = CreateRepoRequest {
            kind: Some(REPO_KIND_FILESYSTEM.into()),
            ..Default::default()
        };
        assert_eq!(
            serde_json::to_string(&request).unwrap(),
            r#"{"default_branch":"main","kind":"filesystem"}"#
        );
        request.region = Some("eu-central-1".into());
        assert_eq!(
            serde_json::to_string(&request).unwrap(),
            r#"{"default_branch":"main","kind":"filesystem","region":"eu-central-1"}"#
        );
    }
}