liboxen 0.9.9-alpha

Oxen is a fast, unstructured data version control, to help version datasets, written in Rust.
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
//! Errors for the oxen library
//!
//! Enumeration for all errors that can occur in the oxen library
//!

use derive_more::{Display, Error};
use std::fmt::Debug;
use std::io;
use std::path::Path;

use crate::model::Schema;
use crate::model::{Commit, ParsedResource};
use crate::model::{Remote, RepoNew};

pub mod path_buf_error;
pub mod string_error;

pub use crate::error::path_buf_error::PathBufError;
pub use crate::error::string_error::StringError;
use polars::prelude::PolarsError;

pub const NO_REPO_FOUND: &str = "No oxen repository exists, looking for directory: .oxen";

pub const HEAD_NOT_FOUND: &str = "HEAD not found";

pub const EMAIL_AND_NAME_NOT_FOUND: &str =
    "oxen not configured, set email and name with:\n\noxen config --name YOUR_NAME --email YOUR_EMAIL\n";

pub const AUTH_TOKEN_NOT_FOUND: &str =
    "oxen authentication token not found, obtain one from your administrator and configure with:\n\noxen config --auth <HOST> <TOKEN>\n";

#[derive(Debug, Display, Error)]
pub enum OxenError {
    /// Internal Oxen Errors
    // User
    UserConfigNotFound(Box<StringError>),

    // Repo
    RepoNotFound(Box<RepoNew>),
    RepoAlreadyExists(Box<RepoNew>),

    // Remotes
    RemoteRepoNotFound(Box<Remote>),
    RemoteAheadOfLocal(StringError),
    IncompleteLocalHistory(StringError),
    RemoteBranchLocked(StringError),

    // Branches/Commits
    BranchNotFound(Box<StringError>),
    RevisionNotFound(Box<StringError>),
    RootCommitDoesNotMatch(Box<Commit>),
    NothingToCommit(StringError),

    // Resources (paths, uris, etc.)
    ResourceNotFound(StringError),
    PathDoesNotExist(Box<PathBufError>),
    ParsedResourceNotFound(Box<PathBufError>),

    // Versioning
    MigrationRequired(StringError),

    // Entry
    CommitEntryNotFound(StringError),

    // Schema
    InvalidSchema(Box<Schema>),
    InvalidFileType(StringError),

    // Generic
    ParsingError(Box<StringError>),

    // Metadata
    ImageMetadataParseError(StringError),

    // SQL
    SQLParseError(StringError),

    // CLI Interaction
    OperationCancelled(StringError),

    // External Library Errors
    IO(io::Error),
    Authentication(StringError),
    TomlSer(toml::ser::Error),
    TomlDe(toml::de::Error),
    URI(http::uri::InvalidUri),
    URL(url::ParseError),
    JSON(serde_json::Error),
    HTTP(reqwest::Error),
    Encoding(std::str::Utf8Error),
    DB(rocksdb::Error),
    DUCKDB(duckdb::Error),
    ENV(std::env::VarError),
    RedisError(redis::RedisError),
    R2D2Error(r2d2::Error),
    JwalkError(jwalk::Error),
    PatternError(glob::PatternError),
    GlobError(glob::GlobError),
    PolarsError(polars::prelude::PolarsError),

    // Fallback
    Basic(StringError),
}

impl OxenError {
    pub fn basic_str<T: AsRef<str>>(s: T) -> Self {
        OxenError::Basic(StringError::from(s.as_ref()))
    }

    pub fn authentication<T: AsRef<str>>(s: T) -> Self {
        OxenError::Authentication(StringError::from(s.as_ref()))
    }

    pub fn migration_required<T: AsRef<str>>(s: T) -> Self {
        OxenError::MigrationRequired(StringError::from(s.as_ref()))
    }

    pub fn user_config_not_found(value: StringError) -> Self {
        OxenError::UserConfigNotFound(Box::new(value))
    }

    pub fn repo_not_found(repo: RepoNew) -> Self {
        OxenError::RepoNotFound(Box::new(repo))
    }

    pub fn remote_not_set(name: &str) -> Self {
        OxenError::basic_str(
            format!("Remote not set, you can set a remote by running:\n\noxen config --set-remote {} <url>\n", name)
        )
    }

    pub fn remote_not_found(remote: Remote) -> Self {
        OxenError::RemoteRepoNotFound(Box::new(remote))
    }

    pub fn remote_ahead_of_local() -> Self {
        OxenError::RemoteAheadOfLocal(StringError::from(
            "\nRemote ahead of local, must pull changes. To fix run:\n\n  oxen pull\n",
        ))
    }

    pub fn incomplete_local_history() -> Self {
        OxenError::IncompleteLocalHistory(StringError::from(
            "\nCannot push to an empty repository with an incomplete local history. To fix, pull the complete history from your remote:\n\n  oxen pull <remote> <branch> --all\n",
        ))
    }

    pub fn remote_branch_locked() -> Self {
        OxenError::RemoteBranchLocked(StringError::from(
            "\nRemote branch is locked - another push is in progress. Wait a bit before pushing again, or try pushing to a new branch.\n",
        ))
    }

    pub fn operation_cancelled() -> Self {
        OxenError::OperationCancelled(StringError::from("\nOperation cancelled.\n"))
    }

    pub fn resource_not_found(value: impl AsRef<str>) -> Self {
        OxenError::ResourceNotFound(StringError::from(value.as_ref()))
    }

    pub fn path_does_not_exist(path: impl AsRef<Path>) -> Self {
        OxenError::PathDoesNotExist(Box::new(path.as_ref().into()))
    }

    pub fn image_metadata_error<T: AsRef<str>>(s: T) -> Self {
        OxenError::ImageMetadataParseError(StringError::from(s.as_ref()))
    }

    pub fn sql_parse_error<T: AsRef<str>>(s: T) -> Self {
        OxenError::SQLParseError(StringError::from(s.as_ref()))
    }

    pub fn parsed_resource_not_found(resource: ParsedResource) -> Self {
        OxenError::ParsedResourceNotFound(Box::new(resource.resource.into()))
    }

    pub fn repo_already_exists(repo: RepoNew) -> Self {
        OxenError::RepoAlreadyExists(Box::new(repo))
    }

    pub fn revision_not_found(value: StringError) -> Self {
        OxenError::RevisionNotFound(Box::new(value))
    }

    pub fn root_commit_does_not_match(commit: Commit) -> Self {
        OxenError::RootCommitDoesNotMatch(Box::new(commit))
    }

    pub fn local_repo_not_found() -> OxenError {
        OxenError::basic_str(NO_REPO_FOUND)
    }

    pub fn email_and_name_not_set() -> OxenError {
        OxenError::user_config_not_found(EMAIL_AND_NAME_NOT_FOUND.to_string().into())
    }

    pub fn auth_token_not_set() -> OxenError {
        OxenError::basic_str(AUTH_TOKEN_NOT_FOUND)
    }

    pub fn remote_repo_not_found<T: AsRef<str>>(url: T) -> OxenError {
        let err = format!("Remote repository does not exist {}", url.as_ref());
        OxenError::basic_str(err)
    }

    pub fn head_not_found() -> OxenError {
        OxenError::basic_str(HEAD_NOT_FOUND)
    }

    pub fn home_dir_not_found() -> OxenError {
        OxenError::basic_str("Home directory not found")
    }

    pub fn must_be_on_valid_branch() -> OxenError {
        OxenError::basic_str("Repository is in a detached HEAD state, checkout a valid branch to continue.\n\n  oxen checkout <branch>\n")
    }

    pub fn no_schemas_staged() -> OxenError {
        OxenError::basic_str(
            "No schemas staged\n\nAuto detect schema on file with:\n\n  oxen add path/to/file.csv\n\nOr manually add a schema override with:\n\n  oxen schemas add path/to/file.csv 'name:str, age:i32'\n",
        )
    }

    pub fn no_schemas_committed() -> OxenError {
        OxenError::basic_str(
            "No schemas committed\n\nAuto detect schema on file with:\n\n  oxen add path/to/file.csv\n\nOr manually add a schema override with:\n\n  oxen schemas add path/to/file.csv 'name:str, age:i32'\n\nThen commit the schema with:\n\n  oxen commit -m 'Adding schema for path/to/file.csv'\n",
        )
    }

    pub fn schema_does_not_exist_for_file<P: AsRef<Path>>(path: P) -> OxenError {
        let err = format!("Schema does not exist for file {:?}", path.as_ref());
        OxenError::basic_str(err)
    }

    pub fn schema_does_not_exist<S: AsRef<str>>(schema_ref: S) -> OxenError {
        let err = format!("Schema does not exist {:?}", schema_ref.as_ref());
        OxenError::basic_str(err)
    }

    pub fn schema_does_not_have_field<S: AsRef<str>>(field: S) -> OxenError {
        let err = format!("Schema does not have field {:?}", field.as_ref());
        OxenError::basic_str(err)
    }

    pub fn schema_has_changed(old_schema: Schema, current_schema: Schema) -> OxenError {
        let err =
            format!("\nSchema has changed\n\nOld\n{old_schema}\n\nCurrent\n{current_schema}\n");
        OxenError::basic_str(err)
    }

    pub fn remote_branch_not_found<T: AsRef<str>>(name: T) -> OxenError {
        let err = format!("Remote branch '{}' not found", name.as_ref());
        log::warn!("{}", err);
        OxenError::BranchNotFound(Box::new(StringError::from(name.as_ref())))
    }

    pub fn local_branch_not_found<T: AsRef<str>>(name: T) -> OxenError {
        let err = format!("Branch '{}' not found", name.as_ref());
        log::warn!("{}", err);
        OxenError::BranchNotFound(Box::new(StringError::from(err)))
    }

    pub fn commit_db_corrupted<T: AsRef<str>>(commit_id: T) -> OxenError {
        let err = format!(
            "Commit db corrupted, could not find commit: {}",
            commit_id.as_ref()
        );
        OxenError::basic_str(err)
    }

    pub fn commit_id_does_not_exist<T: AsRef<str>>(commit_id: T) -> OxenError {
        let err = format!("Could not find commit: {}", commit_id.as_ref());
        OxenError::basic_str(err)
    }

    pub fn local_parent_link_broken<T: AsRef<str>>(commit_id: T) -> OxenError {
        let err = format!("Broken link to parent commit: {}", commit_id.as_ref());
        OxenError::basic_str(err)
    }

    pub fn entry_does_not_exist<T: AsRef<Path>>(path: T) -> OxenError {
        let err = format!("Entry does not exist: {:?}", path.as_ref());
        OxenError::basic_str(err)
    }

    pub fn file_error<T: AsRef<Path>>(path: T, error: std::io::Error) -> OxenError {
        let err = format!("File does not exist: {:?} error {:?}", path.as_ref(), error);
        OxenError::basic_str(err)
    }

    pub fn file_create_error<T: AsRef<Path>>(path: T, error: std::io::Error) -> OxenError {
        let err = format!(
            "Could not create file: {:?} error {:?}",
            path.as_ref(),
            error
        );
        OxenError::basic_str(err)
    }

    pub fn file_metadata_error<T: AsRef<Path>>(path: T, error: std::io::Error) -> OxenError {
        let err = format!(
            "Could not get file metadata: {:?} error {:?}",
            path.as_ref(),
            error
        );
        OxenError::basic_str(err)
    }

    pub fn file_copy_error(
        src: impl AsRef<Path>,
        dst: impl AsRef<Path>,
        err: impl Debug,
    ) -> OxenError {
        let err = format!(
            "File copy error: {err:?}\nCould not copy from `{:?}` to `{:?}`",
            src.as_ref(),
            dst.as_ref()
        );
        OxenError::basic_str(err)
    }

    pub fn file_rename_error(
        src: impl AsRef<Path>,
        dst: impl AsRef<Path>,
        err: impl Debug,
    ) -> OxenError {
        let err = format!(
            "File rename error: {err:?}\nCould not move from `{:?}` to `{:?}`",
            src.as_ref(),
            dst.as_ref()
        );
        OxenError::basic_str(err)
    }

    pub fn remote_add_file_not_in_repo(path: impl AsRef<Path>) -> OxenError {
        let err = format!(
            "File is outside of the repo {:?}\n\nYou must specify a path you would like to add the file at with the -p flag.\n\n  oxen remote add /path/to/file.png -p my-images/\n",
            path.as_ref()
        );
        OxenError::basic_str(err)
    }

    pub fn entry_does_not_exist_in_commit<P: AsRef<Path>, S: AsRef<str>>(
        path: P,
        commit_id: S,
    ) -> OxenError {
        let err = format!(
            "Entry {:?} does not exist in commit {}",
            path.as_ref(),
            commit_id.as_ref()
        );
        OxenError::CommitEntryNotFound(err.into())
    }

    pub fn file_has_no_parent<T: AsRef<Path>>(path: T) -> OxenError {
        let err = format!("File has no parent: {:?}", path.as_ref());
        OxenError::basic_str(err)
    }

    pub fn file_has_no_name<T: AsRef<Path>>(path: T) -> OxenError {
        let err = format!("File has no file_name: {:?}", path.as_ref());
        OxenError::basic_str(err)
    }

    pub fn could_not_convert_path_to_str<T: AsRef<Path>>(path: T) -> OxenError {
        let err = format!("File has no name: {:?}", path.as_ref());
        OxenError::basic_str(err)
    }

    pub fn local_revision_not_found<T: AsRef<str>>(name: T) -> OxenError {
        let err = format!(
            "Local branch or commit reference `{}` not found",
            name.as_ref()
        );
        OxenError::basic_str(err)
    }

    pub fn could_not_find_merge_conflict<P: AsRef<Path>>(path: P) -> OxenError {
        let err = format!(
            "Could not find merge conflict for path: {:?}",
            path.as_ref()
        );
        OxenError::basic_str(err)
    }

    pub fn could_not_decode_value_for_key_error<S: AsRef<str>>(key: S) -> OxenError {
        let err = format!("Could not decode value for key: {:?}", key.as_ref());
        OxenError::basic_str(err)
    }

    pub fn invalid_agg_query<S: AsRef<str>>(query: S) -> OxenError {
        let err = format!("Invalid aggregate opt: {:?}", query.as_ref());
        OxenError::basic_str(err)
    }

    pub fn invalid_set_remote_url<S: AsRef<str>>(url: S) -> OxenError {
        let err = format!("\nRemote invalid, must be fully qualified URL, got: {:?}\n\n  oxen config --set-remote origin https://hub.oxen.ai/<namespace>/<reponame>\n", url.as_ref());
        OxenError::basic_str(err)
    }

    pub fn invalid_file_type<S: AsRef<str>>(file_type: S) -> OxenError {
        let err = format!("Invalid file type: {:?}", file_type.as_ref());
        OxenError::InvalidFileType(StringError::from(err))
    }

    pub fn parse_error<S: AsRef<str>>(value: S) -> OxenError {
        let err = format!("Parse error: {:?}", value.as_ref());
        OxenError::basic_str(err)
    }

    pub fn unknown_agg_fn<S: AsRef<str>>(name: S) -> OxenError {
        let err = format!("Unknown aggregation function: {:?}", name.as_ref());
        OxenError::basic_str(err)
    }

    pub fn repo_is_shallow() -> OxenError {
        let err = r"
Repo is in a shallow clone state. You can only perform operations remotely.

To fetch data from the remote, run:

    oxen pull origin main

Or you can interact with the remote directly with the `oxen remote` subcommand:

    oxen remote status
    oxen remote add path/to/image.jpg
    oxen remote commit -m 'Committing data to remote without ever pulling it locally'
";
        OxenError::basic_str(err)
    }
}

// if you do not want to call .map_err, implement the std::convert::From trait
impl From<io::Error> for OxenError {
    fn from(error: io::Error) -> Self {
        OxenError::IO(error)
    }
}

impl From<String> for OxenError {
    fn from(error: String) -> Self {
        OxenError::Basic(StringError::from(error))
    }
}

impl From<toml::ser::Error> for OxenError {
    fn from(error: toml::ser::Error) -> Self {
        OxenError::TomlSer(error)
    }
}

impl From<toml::de::Error> for OxenError {
    fn from(error: toml::de::Error) -> Self {
        OxenError::TomlDe(error)
    }
}

impl From<http::uri::InvalidUri> for OxenError {
    fn from(error: http::uri::InvalidUri) -> Self {
        OxenError::URI(error)
    }
}

impl From<url::ParseError> for OxenError {
    fn from(error: url::ParseError) -> Self {
        OxenError::URL(error)
    }
}

impl From<serde_json::Error> for OxenError {
    fn from(error: serde_json::Error) -> Self {
        OxenError::JSON(error)
    }
}

impl From<std::str::Utf8Error> for OxenError {
    fn from(error: std::str::Utf8Error) -> Self {
        OxenError::Encoding(error)
    }
}

impl From<r2d2::Error> for OxenError {
    fn from(error: r2d2::Error) -> Self {
        OxenError::R2D2Error(error)
    }
}

impl From<jwalk::Error> for OxenError {
    fn from(error: jwalk::Error) -> Self {
        OxenError::JwalkError(error)
    }
}

impl From<redis::RedisError> for OxenError {
    fn from(error: redis::RedisError) -> Self {
        OxenError::RedisError(error)
    }
}

impl From<glob::PatternError> for OxenError {
    fn from(error: glob::PatternError) -> Self {
        OxenError::PatternError(error)
    }
}

impl From<PolarsError> for OxenError {
    fn from(err: PolarsError) -> Self {
        OxenError::PolarsError(err)
    }
}

impl From<glob::GlobError> for OxenError {
    fn from(error: glob::GlobError) -> Self {
        OxenError::GlobError(error)
    }
}

impl From<reqwest::Error> for OxenError {
    fn from(error: reqwest::Error) -> Self {
        OxenError::HTTP(error)
    }
}

impl From<rocksdb::Error> for OxenError {
    fn from(error: rocksdb::Error) -> Self {
        OxenError::DB(error)
    }
}

impl From<duckdb::Error> for OxenError {
    fn from(error: duckdb::Error) -> Self {
        OxenError::DUCKDB(error)
    }
}

impl From<std::env::VarError> for OxenError {
    fn from(error: std::env::VarError) -> Self {
        OxenError::ENV(error)
    }
}