Skip to content

Commit

Permalink
Move some constants from bridge to ffi
Browse files Browse the repository at this point in the history
  • Loading branch information
madadam committed Sep 6, 2023
1 parent 30ef4b8 commit d4a6049
Show file tree
Hide file tree
Showing 7 changed files with 47 additions and 46 deletions.
1 change: 1 addition & 0 deletions bindgen/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,4 @@ path = "src/main.rs"

[dependencies]
cbindgen = "0.20.0"
env_logger = "0.10.0"
2 changes: 2 additions & 0 deletions bindgen/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,8 @@ use std::path::Path;
fn main() -> Result<(), Box<dyn std::error::Error>> {
// Generate the C bindings header

env_logger::init();

let output_path = Path::new("target").join("bindings.h");

Builder::new()
Expand Down
25 changes: 0 additions & 25 deletions bridge/src/constants.rs
Original file line number Diff line number Diff line change
@@ -1,27 +1,2 @@
pub const ENTRY_TYPE_FILE: u8 = 1;
pub const ENTRY_TYPE_DIRECTORY: u8 = 2;

pub const ACCESS_MODE_BLIND: u8 = 0;
pub const ACCESS_MODE_READ: u8 = 1;
pub const ACCESS_MODE_WRITE: u8 = 2;

pub const NETWORK_EVENT_PROTOCOL_VERSION_MISMATCH: u8 = 0;
pub const NETWORK_EVENT_PEER_SET_CHANGE: u8 = 1;

#[cfg(test)]
mod tests {
use super::*;
use ouisync_lib::AccessMode;

#[test]
fn access_mode_constants() {
for (mode, num) in [
(AccessMode::Blind, ACCESS_MODE_BLIND),
(AccessMode::Read, ACCESS_MODE_READ),
(AccessMode::Write, ACCESS_MODE_WRITE),
] {
assert_eq!(u8::from(mode), num);
assert_eq!(AccessMode::try_from(num).unwrap(), mode);
}
}
}
33 changes: 33 additions & 0 deletions ffi/src/constants.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
use ouisync_lib::EntryType;

pub const ENTRY_TYPE_FILE: u8 = 1;
pub const ENTRY_TYPE_DIRECTORY: u8 = 2;

pub const ACCESS_MODE_BLIND: u8 = 0;
pub const ACCESS_MODE_READ: u8 = 1;
pub const ACCESS_MODE_WRITE: u8 = 2;

pub(crate) fn entry_type_to_num(entry_type: EntryType) -> u8 {
match entry_type {
EntryType::File => ENTRY_TYPE_FILE,
EntryType::Directory => ENTRY_TYPE_DIRECTORY,
}
}

#[cfg(test)]
mod tests {
use super::*;
use ouisync_lib::AccessMode;

#[test]
fn access_mode_constants() {
for (mode, num) in [
(AccessMode::Blind, ACCESS_MODE_BLIND),
(AccessMode::Read, ACCESS_MODE_READ),
(AccessMode::Write, ACCESS_MODE_WRITE),
] {
assert_eq!(u8::from(mode), num);
assert_eq!(AccessMode::try_from(num).unwrap(), mode);
}
}
}
8 changes: 2 additions & 6 deletions ffi/src/directory.rs
Original file line number Diff line number Diff line change
@@ -1,8 +1,4 @@
use crate::{
registry::Handle,
repository::{self, RepositoryHolder},
state::State,
};
use crate::{constants, registry::Handle, repository::RepositoryHolder, state::State};
use camino::Utf8PathBuf;
use serde::{Deserialize, Serialize};

Expand Down Expand Up @@ -42,7 +38,7 @@ pub(crate) async fn open(
.entries()
.map(|entry| DirEntry {
name: entry.unique_name().into_owned(),
entry_type: repository::entry_type_to_num(entry.entry_type()),
entry_type: constants::entry_type_to_num(entry.entry_type()),
})
.collect();

Expand Down
5 changes: 5 additions & 0 deletions ffi/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#[macro_use]
mod utils;
mod constants;
mod dart;
mod directory;
mod error;
Expand All @@ -16,6 +17,10 @@ mod state;
mod state_monitor;
mod transport;

pub use constants::{
ACCESS_MODE_BLIND, ACCESS_MODE_READ, ACCESS_MODE_WRITE, ENTRY_TYPE_DIRECTORY, ENTRY_TYPE_FILE,
};

use crate::{
dart::{Port, PortSender},
error::{ErrorCode, ToErrorCode},
Expand Down
19 changes: 4 additions & 15 deletions ffi/src/repository.rs
Original file line number Diff line number Diff line change
@@ -1,18 +1,14 @@
use crate::{
constants,
error::Error,
registry::Handle,
state::{State, SubscriptionHandle},
};
use camino::Utf8PathBuf;
use ouisync_bridge::{
constants::{ENTRY_TYPE_DIRECTORY, ENTRY_TYPE_FILE},
protocol::Notification,
repository,
transport::NotificationSender,
};
use ouisync_bridge::{protocol::Notification, repository, transport::NotificationSender};
use ouisync_lib::{
network::{self, Registration},
path, AccessMode, EntryType, Event, Payload, Progress, Repository, ShareToken,
path, AccessMode, Event, Payload, Progress, Repository, ShareToken,
};
use std::sync::Arc;
use tokio::sync::broadcast::error::RecvError;
Expand Down Expand Up @@ -228,7 +224,7 @@ pub(crate) async fn entry_type(
let holder = state.get_repository(handle);

match holder.repository.lookup_type(path).await {
Ok(entry_type) => Ok(Some(entry_type_to_num(entry_type))),
Ok(entry_type) => Ok(Some(constants::entry_type_to_num(entry_type))),
Err(ouisync_lib::Error::EntryNotFound) => Ok(None),
Err(error) => Err(error),
}
Expand Down Expand Up @@ -361,10 +357,3 @@ pub(crate) async fn mirror(state: &State, handle: Handle<RepositoryHolder>) -> R

Ok(())
}

pub(crate) fn entry_type_to_num(entry_type: EntryType) -> u8 {
match entry_type {
EntryType::File => ENTRY_TYPE_FILE,
EntryType::Directory => ENTRY_TYPE_DIRECTORY,
}
}

0 comments on commit d4a6049

Please sign in to comment.