Skip to content

Commit

Permalink
Merge pull request #117 from stefanw/upgrade-0.16
Browse files Browse the repository at this point in the history
Upgrade to yrs 0.16
  • Loading branch information
davidbrochart authored Oct 6, 2023
2 parents e0ca760 + a55c56d commit f5b2533
Show file tree
Hide file tree
Showing 15 changed files with 1,598 additions and 658 deletions.
4 changes: 2 additions & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -11,8 +11,8 @@ name = "y_py"
crate-type = ["cdylib"]

[dependencies]
lib0 = "0.12.2"
yrs = "0.12.2"
lib0 = "0.16.10"
yrs = "0.16.10"

[dependencies.pyo3]
version = "0.19.2"
Expand Down
1 change: 1 addition & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ pub fn y_py(_py: Python, m: &PyModule) -> PyResult<()> {
m.add_class::<y_map::YMap>()?;
m.add_class::<y_xml::YXmlText>()?;
m.add_class::<y_xml::YXmlElement>()?;
m.add_class::<y_xml::YXmlFragment>()?;
// Events
m.add_class::<y_text::YTextEvent>()?;
m.add_class::<y_array::YArrayEvent>()?;
Expand Down
80 changes: 67 additions & 13 deletions src/shared_types.rs
Original file line number Diff line number Diff line change
@@ -1,16 +1,22 @@
use crate::{
y_array::YArray,
y_doc::YDocInner,
y_map::YMap,
y_text::YText,
y_xml::{YXmlElement, YXmlText},
y_transaction::YTransactionInner,
y_xml::{YXmlElement, YXmlFragment, YXmlText},
};
use pyo3::create_exception;
use pyo3::types as pytypes;
use pyo3::{exceptions::PyException, prelude::*};
use std::fmt::Display;
use yrs::types::TYPE_REFS_XML_TEXT;
use yrs::types::{TypeRefs, TYPE_REFS_ARRAY, TYPE_REFS_MAP, TYPE_REFS_TEXT};
use yrs::{types::TYPE_REFS_XML_ELEMENT, SubscriptionId};
use std::{
cell::RefCell,
fmt::Display,
ops::{Deref, DerefMut},
rc::Rc,
};
use yrs::types::TypeRef;
use yrs::SubscriptionId;

// Common errors
create_exception!(y_py, PreliminaryObservationException, PyException, "Occurs when an observer is attached to a Y type that is not integrated into a YDoc. Y types can only be observed once they have been added to a YDoc.");
Expand Down Expand Up @@ -80,13 +86,15 @@ impl<I, P> SharedType<I, P> {
SharedType::Prelim(prelim)
}
}

#[derive(Clone)]
pub enum YPyType<'a> {
Text(&'a PyCell<YText>),
Array(&'a PyCell<YArray>),
Map(&'a PyCell<YMap>),
XmlElement(&'a PyCell<YXmlElement>),
XmlText(&'a PyCell<YXmlText>),
XmlFragment(&'a PyCell<YXmlFragment>),
}

impl<'a> YPyType<'a> {
Expand All @@ -95,21 +103,66 @@ impl<'a> YPyType<'a> {
YPyType::Text(v) => v.borrow().prelim(),
YPyType::Array(v) => v.borrow().prelim(),
YPyType::Map(v) => v.borrow().prelim(),
YPyType::XmlElement(_) | YPyType::XmlText(_) => false,
YPyType::XmlElement(_) | YPyType::XmlText(_) | YPyType::XmlFragment(_) => false,
}
}

pub fn type_ref(&self) -> TypeRefs {
match self {
YPyType::Text(_) => TYPE_REFS_TEXT,
YPyType::Array(_) => TYPE_REFS_ARRAY,
YPyType::Map(_) => TYPE_REFS_MAP,
YPyType::XmlElement(_) => TYPE_REFS_XML_ELEMENT,
YPyType::XmlText(_) => TYPE_REFS_XML_TEXT,
pub fn type_ref(&self) -> TypeRef {
match &self {
YPyType::Text(_) => TypeRef::Text,
YPyType::Array(_) => TypeRef::Array,
YPyType::Map(_) => TypeRef::Map,
YPyType::XmlElement(py_xml_element) => {
TypeRef::XmlElement(py_xml_element.borrow().0.tag().clone())
}
YPyType::XmlText(_) => TypeRef::XmlText,
YPyType::XmlFragment(_) => TypeRef::XmlFragment,
}
}
}

#[derive(Clone)]
pub struct TypeWithDoc<T> {
pub inner: T,
pub doc: Rc<RefCell<YDocInner>>,
}

impl<T> TypeWithDoc<T> {
pub fn new(inner: T, doc: Rc<RefCell<YDocInner>>) -> Self {
Self { inner, doc }
}

fn get_transaction(&self) -> Rc<RefCell<YTransactionInner>> {
let doc = self.doc.clone();
let txn = doc.borrow_mut().begin_transaction();
txn
}

pub fn with_transaction<F, R>(&self, f: F) -> R
where
F: FnOnce(&YTransactionInner) -> R,
{
let txn = self.get_transaction();
let mut txn = txn.borrow_mut();
f(&mut txn)
}
}

impl<T> Deref for TypeWithDoc<T> {
type Target = T;
#[inline(always)]
fn deref(&self) -> &T {
&self.inner
}
}

impl<T> DerefMut for TypeWithDoc<T> {
#[inline(always)]
fn deref_mut(&mut self) -> &mut T {
&mut self.inner
}
}

impl<'a> Display for YPyType<'a> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
let info = match self {
Expand All @@ -118,6 +171,7 @@ impl<'a> Display for YPyType<'a> {
YPyType::Map(m) => m.borrow().__str__(),
YPyType::XmlElement(xml) => xml.borrow().__str__(),
YPyType::XmlText(xml) => xml.borrow().__str__(),
YPyType::XmlFragment(xml) => xml.borrow().__str__(),
};
write!(f, "{}", info)
}
Expand Down
Loading

0 comments on commit f5b2533

Please sign in to comment.